Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to define cakephp inputDefaults at site level

Is there a way to define options['inputDefaults'] at site level than that at each form

like image 243
aWebDeveloper Avatar asked Sep 02 '12 10:09

aWebDeveloper


1 Answers

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.

like image 142
Dave Avatar answered Oct 22 '22 01:10

Dave