Tuesday, February 15, 2022

[FIXED] How can I override FormHelper in CakePHP 3.X?

Issue

Customised form helper per my need

<?php    
namespace App\View\Helper;    
use Cake\View\Helper;

class IecFormHelper extends Helper
{  
    public $helpers = ['Form'];
    public $iecFormConfig = [        
        'templates' => [            
            'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} onBlur=update_data(this.id) />',
        ]
    ];

    public function date($fieldName, array $options = [])
    {
        $options += [
            'empty' => true,
            'value' => null,
            'monthNames' => true,
            'minYear' => 1950,
            'maxYear' => date('Y') + 50,
            'orderYear' => 'desc',
        ];
        $options['hour'] = $options['minute'] = false;
        $options['meridian'] = $options['second'] = false;

        $options = $this->_initInputField($fieldName, $options);
        $options = $this->_datetimeOptions($options);

        return $this->widget('datetime', $options);
    }
}

I have this helper class under "src/View/Helper" which basically have template rule for to call javascript function on blur, and also I want to change the minYear and maxYear values for all date dropdown.

My question is how and where do I use this helper so that it overrides rules in the main FormHelper.


Solution

You can just load it and use your Helper instead of form helper.
If you want to overwrite Formhelper you should extend FormHelper in your IecFormHelper



Answered By - Tolga

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.