Use your own style for a form

When you create a new form with CakePHP, it sets automatically some style around your inputs. We will see here how to get rid of it and use our own style.

echo $this->Form->create('Post', array('inputDefaults' => 
array('div' => false, 'label' => false, 'error' => false)));

We create a form, the first parameter is the name of your model, the second is an array of parameters but I will talk only of the key “inputDefaults” which deals with all of your inputs in this file.

  • div => (true or false), sets or not the div wrapper
  • label => (true or false), sets or not the label
  • error => (true or false), display errors or not

And then for one input, the title input for example.

echo $this->Form->input('title', array('class' => 'span3'));
echo $this->Form->error('title', null, 
array('class' => 'help-block alert alert-error', 'wrap' => 'p'));

The first line we set the input “title” with the class “span3”.

The second line is there to print the error if one occurs after the validation process, and of course, we set some classes and a wrapper element, here a paragraph, but you can put a span, a div. And voilà !