Skip to content

Commit

Permalink
Merge pull request #52 from mi32dogs/master
Browse files Browse the repository at this point in the history
Bootstrap 3 support
  • Loading branch information
Aaron Carlino committed Nov 12, 2016
2 parents 524b7e2 + c6c946d commit 8ecd2d0
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 38 deletions.
76 changes: 71 additions & 5 deletions code/BootstrapForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ class BootstrapForm extends Form {
protected $formLayout = "vertical";


/**
* The label grid class for the bootstrap 3 horizontal form
*
* @var String
*/
protected $gridLabelClass = "col-sm-3";


/**
* The input grid class for the bootstrap 3 horizontal form
*
* @var String
*/
protected $gridInputClass = "col-sm-9";

/**
* The Action grid class for the bootstrap 3 horizontal form
*
* @var String
*/
protected $gridActionClass = "col-sm-offset-3 col-sm-9";



/**
Expand Down Expand Up @@ -97,6 +119,50 @@ public function setLayout($layout) {
}


/**
* Sets the desired label Grid Class of the form. Options include:
* - "col-sm-3" (default)
* - "col-sm-4"
* etc..
*
* @param String
* @return BootstrapForm
*/
public function setGridLabelClass($class) {
$this->gridLabelClass = strtolower($class);
return $this;
}


/**
* Sets the desired Input Grid Class of the form. Options include:
* - "col-sm-9" (default)
* - "col-sm-8"
* etc..
*
* @param String
* @return BootstrapForm
*/
public function setGridInputClass($class) {
$this->gridInputClass = strtolower($class);
return $this;
}


/**
* Sets the desired Action Grid Class of the form. Options include:
* - "col-sm-offset-3 col-sm-9" (default)
* - "col-sm-offset-2 col-sm-10"
* etc..
*
* @param String
* @return BootstrapForm
*/
public function setGridActionClass($class) {
$this->gridActionClass = strtolower($class);
return $this;
}


/**
* Adds a "well," or sunken background and border, to the form
Expand All @@ -116,18 +182,18 @@ public function addWell() {
* @return string
*/
public function forTemplate() {
if(!$this->stat('bootstrap_included')) {
Requirements::css(BOOTSTRAP_FORMS_DIR.'/css/bootstrap.css');
if($this->stat('bootstrap_included')!=false) {
Requirements::css(BOOTSTRAP_FORMS_DIR.'/css/bootstrap.css');
}
if(!$this->stat('jquery_included')) {
if($this->stat('jquery_included')!=false) {
Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
}
if(!$this->stat('bootstrap_form_included')) {
if(!$this->stat('bootstrap_form_included')!=false) {
Requirements::javascript(BOOTSTRAP_FORMS_DIR."/javascript/bootstrap_forms.js");
}
$this->addExtraClass("form-{$this->formLayout}");

$this->applyBootstrap();
$this->applyBootstrap();

return parent::forTemplate();
}
Expand Down
88 changes: 79 additions & 9 deletions code/BootstrapFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ class BootstrapFormField extends DataExtension {
);


/**
* The label grid class for the bootstrap 3 horizontal form
* overrides form setting
*
* @var String
*/
protected $gridLabelClass = '';

/**
* The input grid class for the bootstrap 3 horizontal form
* overrides form setting
*
* @var String
*/
protected $gridInputClass = '';


/**
* Adds a HTML5 placeholder attribute to the form field
*
Expand Down Expand Up @@ -166,6 +183,59 @@ public function HolderClasses() {
return implode(" ",$this->holderClasses);
}


/**
* returns the bootstrap Layout to be used in templates
*
* @return string
*/
public function BootstrapLayout() {
return $this->owner->form->formLayout;
}

/**
* Allows setting custom Grid label class to override from grid label class
*
* @param string $class
*
* @return BootstrapFormField
*/
public function setGridLabelClass($class) {
$this->gridLabelClass = $class;
return $this->owner;
}

/**
* Allows setting custom Grid input class to override from grid label class
*
* @param string $class
*
* @return BootstrapFormField
*/
public function setGridInputClass($class) {
$this->gridInputClass = $class;
return $this->owner;
}

/**
* returns Grid label class to be used in templates
*
* @return integer
*/
public function GridLabelClass() {
return (trim($this->gridLabelClass)!='')?$this->gridLabelClass:$this->owner->form->gridLabelClass;
}

/**
* returns Grid input class to be used in templates
*
* @return integer
*/
public function GridInputClass() {
return (trim($this->gridInputClass)!='')?$this->gridInputClass:$this->owner->form->gridInputClass;
}


/**
* checks for error messages in owner form field
* adds error class to holder and loads error message as helptext
Expand All @@ -178,22 +248,22 @@ private function loadErrorMessage() {
$this->addHelpText($this->owner->message);
}
}

/**
* Adds the form-control class to *just* the formfield, not the holder.
* This seems a bit of a hack, but addExtraClass() affects both the holder
* and the field, so that's not a realistic option. We can't have form-control
* on the wrapping div.
*
* @param FormField $field
*
* @param FormField $field
*/
public function onBeforeRender (FormField $field) {
$inline_fields = Config::inst()->get('BootstrapForm','inline_fields');
public function onBeforeRender (FormField $field) {
$inline_fields = Config::inst()->get('BootstrapForm','inline_fields');

if(!in_array($field->class, $inline_fields )) {
$field->addExtraClass('form-control');
}
if(!in_array($field->class, $inline_fields )) {
$field->addExtraClass('form-control');
}
}


}
}
9 changes: 5 additions & 4 deletions templates/BootstrapFieldHolder.ss
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<div id="$Name" class="$HolderClasses" $HolderAttributes>
<label for="$ID" class="$LabelClasses">$Title</label>
$Field
<label for="$ID" class="$LabelClasses<% if $BootstrapLayout=='horizontal' %> control-label $GridLabelClass<% end_if %>">$Title</label>
<% if $BootstrapLayout=='horizontal' %><div class="$GridInputClass"><% end_if %>
$Field
<% if $HelpText %>
<p class="help-block">$HelpText</p>
<% end_if %>
<% if $InlineHelpText %>
<span class="help-inline">$InlineHelpText</span>
<% end_if %>
<% end_if %>
<% if $BootstrapLayout=='horizontal' %></div><% end_if %>
</div>

29 changes: 20 additions & 9 deletions templates/BootstrapForm.ss
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,38 @@
<% if $Message %>
<% if $MessageType == "good" %>
<div id="{$FormName}_error" class="alert alert-success" role="alert">$Message</div>
<% else_if MessageType == "info" %>
<% else_if $MessageType == "info" %>
<div id="{$FormName}_error" class="alert alert-info" role="alert">$Message</div>
<% else_if MessageType == "bad" %>
<% else_if $MessageType == "bad" %>
<div id="{$FormName}_error" class="alert alert-danger" role="alert">$Message</div>
<% end_if %>
<% end_if %>

<fieldset>
<% if $Legend %><legend>$Legend</legend><% end_if %>
<% if $Legend %><legend>$Legend</legend><% end_if %>
<% loop $Fields %>
$FieldHolder
<% end_loop %>
<div class="clear"><!-- --></div>
</fieldset>

<% if $Actions %>
<div class="form-actions">
<% loop $Actions %>
$Field
<% end_loop %>
</div>
<% if $formLayout=='horizontal' %>
<div class="form-group">
<div class="$gridActionClass">
<% else %>
<div class="form-actions">
<% end_if %>

<% loop $Actions %>
$Field
<% end_loop %>

<% if $formLayout=='horizontal' %>
</div>
<% end_if %>

</div>
<% end_if %>
<% if $IncludeFormTag %>
</form>
Expand Down
28 changes: 17 additions & 11 deletions templates/BootstrapTextField_holder.ss
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
<div id="$Name" class="<% if $AppendedText || $PrependedText %>input-group<% end_if %> $HolderClasses" $HolderAttributes>
<label for="$ID" class="$LabelClasses">$Title</label>
<div id="$Name" class="$HolderClasses" $HolderAttributes>
<label for="$ID" class="$LabelClasses<% if $BootstrapLayout=='horizontal' %> control-label $GridLabelClass<% end_if %>">$Title</label>

<% if $BootstrapLayout=='horizontal' %><div class="$GridInputClass "><% end_if %>

<% if $AppendedText || $PrependedText %><div class="input-group"><% end_if %>

<% if $PrependedText %>
<span class="input-group-addon">$PrependedText</span>
<% end_if %>
$Field

$Field

<% if $AppendedText %>
<span class="input-group-addon">$AppendedText</span>
<span class="input-group-addon">$AppendedText</span>
<% end_if %>

<% if HelpText %>
<p class="help-block">$HelpText</p>
<% end_if %>
<% if InlineHelpText %>
<span class="help-inline">$InlineHelpText</span>
<% end_if %>
<% if $HelpText %><p class="help-block">$HelpText</p><% end_if %>

<% if $InlineHelpText %><span class="help-inline">$InlineHelpText</span><% end_if %>

<% if $AppendedText || $PrependedText %></div><% end_if %>

<% if $BootstrapLayout=='horizontal' %></div><% end_if %>
</div>

0 comments on commit 8ecd2d0

Please sign in to comment.