开发者

CakePHP: get current input's label while outputting it in view

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.

  1. Create this helper in the views/helpers folder as my_app.php
  2. Add helper to controller
  3. 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 );
    }

}

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜