Search code examples
cakephp-3.xcakephp-3.6

Cakephp 3 : How to add a css class with all input field


I am trying to add a css class "form-control" with my all input field using Cakephp Helper.

I already created a Helper

class BootstrapFormHelper extends Helper
{

    protected $_defaultConfig = [];

    public function control($fieldName, array $options = []){
        $options['class'] = 'form-control';
        return parent::control($fieldName, $options);
    }

}

I also called it in appView

public function initialize()
{
   $this->loadhelper('BootstrapForm');
}

But no any class added in my form input fields. How can I add a css class with my all input fields ?


Solution

  • You can try it like

    class BootstrapFormHelper extends Helper
    {
        public $helpers = ['Form'];
        public function control( $name, $options = [] )
        {
            if( !isset( $options['class'] ) ) {
                $options['class'] = 'form-control';
            }
            return $this->Form->control( $name, $options );
        } 
    }
    

    After call it in View/appView.php

    You can use it in your view like

    <?= $this->BootstrapForm->control('username') ?>
    

    output :

    <input type="text" name="username" class="form-control" id="username">