linkyndy
linkyndy

Reputation: 17928

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-image' => '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!

Upvotes: 0

Views: 419

Answers (1)

Michael Bourque
Michael Bourque

Reputation: 26

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 );
    }

}

?>

Upvotes: 1

Related Questions