In CakePHP is it possible to set the options you pass to the Form helper create method globally?
Since I want a specific form layout to be used on all my forms I am currently having to do this when I create every form.
<?php
echo $this->Form->create('User', array(
'class' => 'form-horizontal',
'inputDefaults' => array(
'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
'between' => '<div class="controls">',
'after' => '</div>',
'div' => 'control-group',
'error' => array(
'attributes' => array('wrap' => 'span', 'class' => 'help-inline')
)
)
));
?>
I was wondering if there was a way to specifiy this globally so I didn't need do it with every create call.
The answer of starlocke is ok but I would not even want to write these three lines all over the place. :) Neither I think this is really "config data". So here is what I would do:
MyFormHelper extends FormHelper {
public function create($model, $options) {
$defaults = array(/* YOUR DEFAULT OPTIONS*/);
$options = Set::merge($defaults, $options);
//...
}
}
Then simply call it:
$this->MyForm->create('Profile');
or call it with a single option in the 2nd param you want to change somewhere.
Make a configuration somewhere (ie: app/config/core.php
-- or a similarly included file if you've expanded your configuration system)
// [...the rest of the config is above...]
Configure::write('MyGlobalFormOptions', array(
'class' => 'form-horizontal',
'inputDefaults' => array(
'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
'between' => '<div class="controls">',
'after' => '</div>',
'div' => 'control-group',
'error' => array(
'attributes' => array('wrap' => 'span', 'class' => 'help-inline')
)
)
));
Using it looks like this...
<?php
echo $this->Form->create('User', Configure::read('MyGlobalFormOptions'));
?>
If you need to get more specific for certain special forms...
<?php
$more_options = array('class'=>'form-vertical');
$options = array_merge(Configure::read('MyGlobalFormOptions'), $more_options);
echo $this->Form->create('Profile', $options);
?>