I am outputting a list of checkboxes, with multiple selection, in a CakePHP view. My code looks like this:
<?php echo $this->Form->input('Category', array('multiple' => 'checkbox', 'div' => 'image-checkbox clearfix', 'label' => false, 'data-ima开发者_StackOverflow中文版ge' => 'TEXT')); ?>
I want to add the input's label in the data-image
attribute, replacing the TEXT
value. How can I do this?
Thank you very much!
The way to do this is with your own helper.
- Create this helper in the views/helpers folder as my_app.php
- Add helper to controller
- Use as $this->MyApp->input('Category', array('multiple' => 'checkbox', 'div' => 'image-checkbox clearfix', 'label' => false));
Here:
<?php
class MyAppHelper extends AppHelper {
var $helpers = array( 'Form');
function input( $fieldName, $options = array() ) {
$newOptions['data-image'] = Inflector::humanize( $fieldName );
$options = array_merge(
$options,
$newOptions
);
return $this->Form->input( $fieldName, $options );
}
}
?>
精彩评论