CakePHP 2.3.1 deactivate form validation in certain views
Asked Answered
T

4

5

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

Teufert answered 19/3, 2013 at 16:36 Comment(6)
Do you force validation to be on/true in your controller's action?Bibliography
How does this validation error occur? in the view as html5 validation? or in the backend code via validation rules of the model?Vanward
If you're referring to the fields on the search form having the appearance of required fields (and not being actually required to perform the search), this could be coming from your CSS file; CakePHP wraps fields in a div with the required class by default (when fields are required, of course). You can change that behaviour by setting the div option to false when calling the $this->Form->input methods. I can't imagine a situation where the fields would actually be required, unless you're calling the model's save method on the 'search' action (highly unlikely).Igneous
@bowlerae: I don't do anything in the controller about validation. I just have the array in the modelTeufert
@Thiago What happens is when I try to submit the form without entries in the necessary fields I get the fields marked red and the rectangular bubble telling me to please fill this field (But with a general message, not with the message defined in the model.). with the search method as you guessed i don't call the save method.Teufert
@mark: since it is the general error message I guess the validation doesn't use the model validationTeufert
T
7

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

Teufert answered 20/3, 2013 at 13:15 Comment(0)
N
1

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"]
Nedry answered 14/9, 2013 at 2:20 Comment(0)
B
1

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));
Bridegroom answered 12/10, 2013 at 5:33 Comment(0)
H
0

In my case I used button for submitting the form. This allowed me more flexibility. In that case I used then property 'formnovalidate' pass in the array of options for the button. The form would look something like the following:

<?php
echo $this->Form->create('yourCtrllerName',array('action'=>'actionInYourCtrller'));
echo $this->Form->input('your_field_pass',array('label'=>'pass','type'=>'password'));
....  other Form fields .....
echo $this->Form->button('Button Caption',
                          array('type'=>'submit',
                                'name'=>'keyInsideTheDataArrayForButtonData',
                                'formnovalidate' => true,
                                'value'=>'valueOfTheAboveKeyInTheDataArray',
                                'style'=>'<style you want to apply to button>',
                                ... other options if needed...
                                )
                         );
echo $this->Form->end();
Hiers answered 7/4, 2014 at 2:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.