Is there a way to define options['inputDefaults']
at site level than that at each form
TLDR:
Paste the 2 chunks of code below in their respective spots, then change the $defaultOptions
array to whatever you want - voila. It doesn't alter any of the FormHelper's functions except adds defaults to the Form->create
's inputDefaults
.
Explanation & Code:
You can extend the FormHelper (easier than it sounds) by making your own custom MyFormHelper
:
<?php
//create this file called 'MyFormHelper.php' in your View/Helper folder
App::uses('FormHelper', 'View/Helper');
class MyFormHelper extends FormHelper {
public function create($model = null, $options = array()) {
$defaultOptions = array(
'inputDefaults' => array(
'div' => false,
'label' => false
)
);
if(!empty($options['inputDefaults'])) {
$options = array_merge($defaultOptions['inputDefaults'], $options['inputDefaults']);
} else {
$options = array_merge($defaultOptions, $options);
}
return parent::create($model, $options);
}
}
Then, in your AppController, include the Form helper in the following way (if you already have a $helpers variable, just add 'Form' => ...
to it):
public $helpers = array(
'Form' => array(
'className' => 'MyForm'
)
);
This makes it so whenever you call $this->Form
, it actually calls your custom 'MyFormHelper' - and the only thing it does is set the inputDefaults
if they're not specified, then continue on to do the normal logic found in Cake's FormHelper.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With