CakePHP validation rule automatically adding a required attribute to the field

15,968

Solution 1

Set required to false and allowEmpty to true, that should do it for you.

'tv_price'=>array(        
    'check'=>array(
        'rule'=>array('check_for_tv_price'),
        'message'=>'Please enter the television pricing information.',
        'required' => false,
        'allowEmpty' => true
    ),
)

Hope this helps.

Solution 2

These seems like 2.3's new HTML5 magic stuff.

Try adding 'formnovalidate' => true to the $this->FormHelper->input() options in the view.

ref:
http://book.cakephp.org/2.0/en/appendices/2-3-migration-guide.html#formhelper
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::input

Share:
15,968
CSamp
Author by

CSamp

Hey there! I am a PHP and JavaScript programmer who is dabbling in Objective-C. I I've been in a love/hate relationship with CakePHP for over 2 years now, and like most long-term relationships with frameworks it is easy to get blinded by their automagic tendencies and ignore the bulky inner-workings that lurk just below the surface... In recent times I've jumped on the Wordpress wagon, sharpened my Angular skills, SASSied up my life with Compass, and began a new love/hate relationship with Magento. I really like a MEAN stack in theory, but I find it easier to find my way with LAMP.

Updated on June 11, 2022

Comments

  • CSamp
    CSamp almost 2 years

    I am using a custom validation rule in CakePHP to be sure a value is entered into a field when a corresponding checkbox is marked:

    When the "Purchasing TV" checkbox is marked, there needs to be a value in the "Enter TV Price" field.

    Here's the validation rule within my model's validation array...

    'tv_price'=>array(        
        'check'=>array(
            'rule'=>array('check_for_tv_price'),
            'message'=>'Please enter the television pricing information.',
        ),
    )
    

    ...and here's my really simple custom validation function:

    public function check_for_tv_price($check) {
        if($this->data['Client']['tv']==1&&$this->data['Client']['tv_price']=="") {
            return false;
        }
        if($this->data['Client']['tv']==1&&$this->data['Client']['tv_price']!="") {
            return true;
        }
        if($this->data['Client']['tv']==0) {
            return true;
        }
    
    }
    

    I've tried adding 'required'=>false and 'allowEmpty'=>true at different points in the validation array for my tv_price field, but they always override my custom rule! As a result, a user can not submit the form because the browser prevents it (due to the required attribute).

    For reference, the browser spits out the following HTML:

    <input id="ClientTvPrice" type="text" required="required" maxlength="255" minyear="2013" maxyear="2018" name="data[Client][tv_price]"></input>
    

    (Note the minyear and maxyear attributes are from the form defaults.)

    Has anyone found a way to prevent the automatic insertion of the required attribute when using custom validation rules?

    Any guidance would be much appreciated.

    Thanks!

    Chris