Adding style to CakePHP automatically created an input div

CakePHP creates a div that automatically wraps any of its input tags that are created using formhelper as follows:

$this->formhelper->input('something');

Thus, the output is as follows:

<div class='input'>
    <input />
</div>

I know there is a way to add classes to the input tag ie

$this->formhelper->input('text', array('class' => 'some_css'));

But how would you add style to the div that CakePHP automatically creates. It may be something where the kernel needs to be hacked, but I want to know if there is a better way to do this so that I get something as follows:

<div class='input other_class_I_want_here'>
    <input />
</div>

Thanks to everyone who can help.

+4
source share
3 answers

Just add a new class to the div.

$this->formhelper->input('text', array('div'=>array('class'=>'divClass'),'class' => 'some_css'));

must really infer

<div class='input divClass'>
    <input class='other_class_I_want_here' />
</div>
+9

, , . , ( ) .

, , , div (: , , auto-styles) MUCH. , FormHelper.

  • App/View/Helper "MySuperCoolFormHelper.php"

  • :

    App::uses('FormHelper', 'View/Helper');

    class MySuperCoolFormHelper extends FormHelper {

        protected function _divOptions($options) {
            if(isset($options['div'])
                $options['div'] .= ' class1 class2 class3'; //note the prefixing space
            else
                $options['div'] = 'class1 class2 class3';

            return parent::_divOptions($options);
        }
    }
  1. , AppController:
    public $helpers = array(
        'Form' => array(
            'className' => 'MySuperCoolFormHelper'
        )
        //The rest of your helper inits
    );

... BLAMMO, !

+5

CakePHP 3: 'form-group' DIV 'form-control'

<?= 
$this->Form->control('year', [
    'type' => 'select', 
    'value' => $year, 
    'options' => $years, 
    'label' => false, 
    'class' => 'form-control', 
    'templates' => ['inputContainer' => '<div class="form-group">{{content}}</div>']
    ]); 
?>
0

All Articles