Source of file BootstrapFormField.php
Size: 6,016 Bytes - Last Modified: 2021-12-23T10:56:03+00:00
/var/www/docs.ssmods.com/process/src/code/BootstrapFormField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 | <?php /** * The base class for creating a {@link FormField} object * that is compatible with the Twitter Bootstrap CSS framework. * * @author Uncle Cheese <unclecheese@leftandmain.com> * @package bootstrap_forms */ class BootstrapFormField extends DataExtension { /** * @var array Attributes and values for the holder tag of the form field */ protected $holderAttributes = array(); protected $holderClasses = array( "form-group" ); protected $labelClasses = array( ); protected $inputClasses = array( ); /** * 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 * * @param $text the placeholder text to add * @return BootstrapFormField */ public function addPlaceholder($text) { return $this->owner->setAttribute("placeholder",$text); } /** * Adds a block of help text to the form field. (HTML safe). * By default, this text appears below a field and its label. * * @param string $text The text to add * @return BootstrapFormField */ public function addHelpText($text) { $this->owner->HelpText = $text; return $this->owner; } /** * Adds a line of inline help text to a form field (HTML safe). * By default, this text appears to the right of a form field. * * @param string $text The text to add * @return BootstrapFormField */ public function addInlineHelpText($text) { $this->owner->InlineHelpText = $text; return $this->owner; } /** * Sets an attribute on the wrapper <div> for the formfield * * @param string $key The attribute name * @param string $val The value for the attribute * * @return BootstrapFormField */ public function setHolderAttribute($key, $val) { $this->holderAttributes[$key] = $val; return $this->owner; } /** * Returns the list of attributes suitable for an HTML tag * * @return string */ public function HolderAttributes() { $ret = ""; foreach($this->holderAttributes as $k => $v) { $ret .= "$k=\"".Convert::raw2att($v)."\" "; } return $ret; } /** * Allows adding custom classes to the input * * @param string $class the class * * @return BootstrapFormField */ public function addInputClass($class) { $this->inputClasses[] = $class; return $this->owner; } /** * returns the input classes to be used in templates * also triggers checking for error messages * * @return string of classes */ public function InputClasses() { $this->loadErrorMessage(); return implode(" ",$this->inputClasses); } /** * Allows adding custom classes to the label * * @param string $class the class * * @return BootstrapFormField */ public function addLabelClass($class) { $this->labelClasses[] = $class; return $this->owner; } /** * returns the label classes to be used in templates * also triggers checking for error messages * * @return string of classes */ public function LabelClasses() { $this->loadErrorMessage(); return implode(" ",$this->labelClasses); } /** * Allows adding custom classes to the holder * * @param string $class the class * * @return BootstrapFormField */ public function addHolderClass($class) { $this->holderClasses[] = $class; return $this->owner; } /** * returns the holder classes to be used in templates * also triggers checking for error messages * * @return string of classes */ public function HolderClasses() { $this->loadErrorMessage(); 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 * * @todo allow setting error message as inline */ private function loadErrorMessage() { if($this->owner->message) { $this->addHolderClass("error"); $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'); } } } |