0

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 ?

Sehdev
  • 5,486
  • 3
  • 11
  • 34
Satu Sultana
  • 527
  • 1
  • 11
  • 23

1 Answers1

2

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">
Greg Schmidt
  • 5,010
  • 2
  • 14
  • 35
Alimon Karim
  • 4,354
  • 10
  • 43
  • 68