Skip to content

Commit

Permalink
BUGFIX: Do not add form-control to wrapping holder div, only field it…
Browse files Browse the repository at this point in the history
…self
  • Loading branch information
Uncle Cheese committed Oct 20, 2015
1 parent 3409575 commit 2c62387
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
38 changes: 23 additions & 15 deletions code/BootstrapFieldList.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
<?php


class BootstrapFieldList extends Extension {

class BootstrapFieldList extends Extension {

/**
* A list of ignored fields that should not take on Bootstrap transforms
* @var array
*/
protected $ignores = array ();


public function bootstrapify() {
$inline_fields = Config::inst()->get('BootstrapForm','inline_fields');

/**
* Transforms all fields in the FieldList to use Bootstrap templates
* @return FieldList
*/
public function bootstrapify() {
foreach($this->owner as $f) {


if(isset($this->ignores[$f->getName()])) continue;

if($f instanceof CompositeField) {
$f->getChildren()->bootstrapify();
continue;
// If we have a Tabset, bootstrapify all Tabs
if($f instanceof TabSet) {
$f->Tabs()->bootstrapify();
}

if(!in_array($f->class, $inline_fields )) {
$f->addExtraClass('form-control');
// If we have a Tab, bootstrapify all its Fields
if($f instanceof Tab) {
$f->Fields()->bootstrapify();
}

$template = "Bootstrap{$f->class}_holder";
Expand Down Expand Up @@ -49,14 +54,17 @@ public function bootstrapify() {
}

return $this->owner;

}



/**
* Adds this field as ignored. Should not take on boostrap transformation
*
* @param string $field The name of the form field
* @return FieldList
*/
public function bootstrapIgnore($field) {
$this->ignores[$field] = true;

return $this->owner;
}
}
}
17 changes: 17 additions & 0 deletions code/BootstrapFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,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
*/
public function onBeforeRender (FormField $field) {
$inline_fields = Config::inst()->get('BootstrapForm','inline_fields');

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


}

1 comment on commit 2c62387

@nzphoenix
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the adverse affect of applying 'form-control' to LabelField (which styles labels to look like inputs)

Please sign in to comment.