CakePHP 2.3.1 deactivate form validation in certain views - forms

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

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

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"]

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));

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();

Related

yii2 validate empty dropdownlist, checkbox

i have certain fields as required in the model and they are dropdownlist on the form.
if i try to submit the form without selecting anything no validation error occurs on the view the way a textinput would.
model
[['name', 'survey_type','country_id'], 'required',
'isEmpty' => function ($value) {
return empty($value);
}
],
display empty red validation
<?= $form->field($model, 'name')->textInput(['maxlength' => 255]) ?>
does not display any validation
<?= $form->field($model, 'survey_type')->dropDownList([$surveyTypeList],['prompt'=>'Select Survey Type','id'=>'Survey_Type_dropdown']) ?>
This is because you overrided id for drop-down list:
'id' => 'Survey_Type_dropdown'
You shouldn't do that If you want client validation working for this field.
Ids generated automatically and then used in javascript related with ActiveForm.
Remove it and everything should be fine:
<?= $form->field($model, 'survey_type')->dropDownList([$surveyTypeList],['prompt' => 'Select Survey Type']) ?>
If you want to do something with this element in javascript, use auto generated id or assign custom class.
I think this is because when the value is empty you return the result of empty($value) so the the attribute is set to true or false, meaning it is not empty.
Try removing the isEmpty part.
The accepted answer works, however, it isn't the correct answer. You may need to change the ID of any given input field and just telling people not to do it because they will not retrain client validation isn't true.
As per the Yii2 API docs it states that if you change the ID of an input field you must also update the input property "selector" as well to match the new ID you specified in the "options" property of the input field.
You do this as follows:
<?= $form->field($model, 'survey_type', ['selectors' => ['input' => '#Survey_Type_dropdown']]))->dropDownList([$surveyTypeList],['prompt'=>'Select Survey Type','id'=>'Survey_Type_dropdown']) ?>
Creating the input like above will allow you to change the ID of an input field while retaining client validation.

cakePHP: form with related models not automagic updating

I struggle with getting a form to work in the way I want it to behave.
I have a Regions and Properties model, one region can have many properties and so on...
I created a form to select the Region and then a Property in that region!
The form is having both lists but I struggle to have the second list [Property] updating automagic with only the properties in the region you have selected from the first list [Regions]
When you select a different region in the list, it should update automagic the property list, so you only see the properties for that region! Sorry for my bad explaining, but not sure how to explain this any better.
This is the code in my controller:
// Retrieve the region list
$this->set('regions', $this->Region->find('list', array(
'fields' => array('Region.id', 'Region.regionname'),
'order' => 'regionname',
)));
// Retrieve Property list for the regions.
$this->set('properties', $this->Region->Property->find('list', array(
'conditions' => array('Property.live' => true ),
'fields' => array('Property.id','Property.description'),
'order' => 'id',
)));
This is part of my form.
<?php echo $this->Form->create('Upload', array('action' => 'add', 'type' => 'file')); ?>
<?php echo $this->Form->input('region_id', array('label' => 'Select Region:')); ?>
<?php echo $this->Form->input('property_id', array('label' => 'Select Property:')); ?>
<?php echo $this->Form->file('file'); ?>
I have spend a lot of time looking around here and on youtube, but can't find it :-(
There's no automagic way to do what you're asking. Since all the data for dropdowns have already beed displayed on load, the only way to change the second dropdown depending on the first select is via javascript.
If you search for "dropdown on select" or something similar for cake, you'll find solutions to do it with ajax or plain js. I leave you one reference here. That one is done with ajax and a new action. But you could also do it with just js, doing a find for Regions and Properties and setting them in a json variable in js to be manipulated.

Write hyperlink inside the Zend Form and use routes?

I am having a Log-in form and want to add a "Lost Password" Link inside. I found out that you can use the description to do so. But I have now started to change everything to work with routes and would like to use this also for the Forgot Password Link. is there any chance to do this? I can't find a solution, anyone of you who knows how to do it?
$password = new Zend_Form_Element_Password('login_password', array(
'label' => 'Password',
'description' => 'Forgot Password ?',
'required' => true,
));
$password->getDecorator('description')->setOptions(array('escape' => false, 'placement' => 'APPEND'));
I've faced the same problem before, and got answer at
Write hyperlink inside the Zend Form?
May help you also...
When creating a Zend_Form, you don't have access to View_Helpers, as a form does not require a view instance. Therefore you either have to fetch a view in your form's init-method or add the description later (I prefer the latter).
When doing it the first way, you have to fetch the Zend_Controller_Front-instance and then its view, finally call a view helper, e.g. Zend_View_Helper_Url from that view.
The latter can be achieved by adding the description later on, e.g. when you passed the form to your view (or in your controller before passing it to the view):
<?php
$description = '<a href="' . $this->url([...]); . '"'>forgot password?</a>
$this->form->getElement('login_password')->setDescription($description);
echo $this->form;
?>

CakePHP Form in an Element Causing Errors

I think this may be an easy solution, but I've spent an hour now investigating to no avail.
I have a registration form in an element that is being used in views belonging to different controllers. Using the "url" attribute, I've told it to submit to /users/register, but for some reason, the fields aren't submitting to the database.
Instead, there are errors for "undefined index" and a MySQL error for an undefined secondary key that I set (it's empty because it's not being submitted). Strangely, the form works fine if I include the element somewhere in the users views. Does anyone know why this is happening?
UPDATE - Here's the relevant code, sorry:
<?php
echo $form->create(array(
'id' => 'signupform',
'url' => array(
'controller' => 'users',
'action' => 'register')));
?>
The form fields are all correct, since the element works in the user controller's views anywhere. Do I need to include any other information in the creation of the form to point it more directly?
Specify the model 'User' as the first parameter to the $form->create() method.
<?php
echo $form->create('User', array(
'id' => 'signupform',
'url' => array(
'controller' => 'users',
'action' => 'register')
)
);
?>
This will ensure that the form fields are named as data[User][field_name], and prevent you from seeing that undefined index error.
Hope this works for you!
Without seeing the code, it sounds like Cake is magically assuming that the Model is the one for the controller that controls the current view. When that controller is Users, it works correctly. When it is, say, Articles, it will be trying (and failing) to fit the form fields to the Article model.
Without seeing any code, it is impossible to offer any more help.
EDIT:
If the form contains mixed models, e.g. User and Article, you must prefix the fieldnames like this:
$form->input('User.username');
$form->input('Article.title');
etc.
If you don't, the controller will assume they all belong to its own model.

Drupal 6 - How to customise form layout using theme

I have create a new content type and added new form items using CCK. I need to customise the layout of the form which I've partially managed using css and moving items around and adding custom markup in the form_alter hook. However, this still isn't enough as the weightings don't appear to be doing exactly what I want them to do.
Is there a way I can do this using a theme.tpl.php file?
Thanks
Steve
once in a time I was in the same situation and found a quite easy solution.
I needed a highly customized registration form and form_alter was a mess. So I used a template for the form and printed each field into the html output.
First register a new template for the form in template.php of you theme.
<?php
function your-theme-name_theme() {
return array(
'user_register' => array(
'arguments' => array('form' => NULL),
'template' => 'user-register',
),
);
}
?>
Now add a new template to your theme. In my case "user_register.tpl.php". Add the following lines to it, so that the form still works.
<?php
print drupal_render($form['form_build_id']);
print drupal_render($form['form_id']);
print drupal_render($form['form_token']);
?>
From there on you can add as much html as you want and put the form fields where ever you need them. Here are examples for the gender and birthday field. As you can see you have to use the internal CCK names.
<?php print drupal_render($form['field_profile_gender']); ?>
<?php print drupal_render($form['field_profile_birthday']); ?>
I have not tested this for any other form than user_register, but I guess it should work as long as the template name equals the name of the form.
I hope this will help you.
Regards
Mike