Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2.3.1 deactivate form validation in certain views

The Cookbook introduces for version 2.3 the possibility to deactivate the forced valiadation for forms. Or at least I understood it like that: Quote: from http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

" New in version 2.3.

Since 2.3 the HTML5 required attribute will also be added to the input based on validation rules. You can explicitly set required key in options array to override it for a field. To skip browser validation triggering for the whole form you can set option 'formnovalidate' => true for the input button you generate using FormHelper::submit() or set 'novalidate' => true in options for FormHelper::create()."

In my case I have a search from for this model and of course the user does not need to fill in all mandatory fields like for adding a dataset. So I want to deactivate the validation for my search form.

I tried all three variations and see no results: Still the mandatory fields for create are mandatory in my search form.

Those attempts I made:

first try:

echo $this->Form->create('Partner', array('action' => 'search', 'novalidate' => true));

second try:

echo $this->Form->input('name', 
array('required' => false, 'value' => $this->Session->read('Searchparameter.name'))
);

third try:

 $this->Form->submit('Submit', array('formnovalidate' => true));
    echo $this->Form->end();

variation:

echo $this->Form->end(__('Submit'), array('formnovalidate' => true));

What did I understand wrong? btw: I did deactivate caching, so that should not be the problem.

Of course I could still use the old workaround for this validation, but when 2.3 is offering this option, I would gladly use it.

Calamity Jane

like image 853
Calamity Jane Avatar asked Mar 19 '13 16:03

Calamity Jane


3 Answers

So I guess I found the problem and at least got one varation working:

What I am using now is:

echo $this->Form->create('Partner', array('action' => 'search', 'novalidate' => true));

I guess what I expected was that the fields wouldn't be marked with the fat label and the asterisk. Those are still there, but regardless you don't have to fill them in anymore. And the times I tested with really submittig the form I guess I had one of the 99 varations, which was really wrong.

If that makes me happy is mine to decide, but obviously I can switch off the HTML5 validation by that. If I would want to have the labels not bold & asterisk, is there an option, too?

Calamity Jane

like image 68
Calamity Jane Avatar answered Nov 14 '22 03:11

Calamity Jane


The solution is actually a lot simpler. If you would like to disable validation in specific views you actually only have to refer to a non-existing model when you create the form. You could for example do something like

echo $this->Form->create('PartnerSearch');

In your controller you can access the form fields through:

$this->request->data["PartnerSearch"]["field"] 

instead of the usual way:

$this->request->data["Partner"]["field"]
like image 33
bicycle Avatar answered Nov 14 '22 02:11

bicycle


For me, to skip the browser validation, yes, array('novalidate' => true) does work.

<?php echo $this->Form->create('MyModelName', array('novalidate' => true)); ?>

To have the label not bold & asterisk,

<?php echo $this->Form->input('myinput', array('required' => false));
like image 44
Sithu Avatar answered Nov 14 '22 01:11

Sithu