sfValidatorChoice not working on multiple selection element - php

sfValidatorChoice is not working on multiple select element, my code
$this->form=new MyTestForm();
$options_array=array("php","python","java");
$widgetSchema["my_select"] =new sfWidgetFormChoice(array('choices' => $options_array,'multiple' => true,'expanded' => true ));
$validatorSchema["my_select"] = new sfValidatorChoice(array("choices" =>array_keys($options_array)));
Note: i have also tried using array_keys and by directly passing the array to sfValidatorChoice.
when i submit, it gives me Invalid error(when checked) and Required(when unchecked).
is there any error in parameters or is bug?

Firstly, you need to enable "multiple" in the validator as well as the widget:
"multiple" => true
To make having any selection optional, you need to set required to false:
"required" => false
Finally, I can't exactly remember how to use sfValidatorChoice (it's been a while), but I think it's best to make the values readable, so I'd do:
$options_array=array('php'=>'php','python'=>'python','java'=>'java');
I'm not certain this will fix the problem, but it may well do.

Related

My Laravel required_without_all validation is not working as expected

I am working on a project, where I have three fields 1. csv file upload(email) 2. comma separated string(emails) 3. dynamic input field(email), any one of these fields is required.
My approach for validation:
'comma_separated' => ['nullable','required_without_all:email.*,file'],
'file' => ['nullable','required_without_all:email.*,comma_separated', 'mimes:csv'],
'email.*' => ['nullable','required_without_all:comma_separated,file','email'],
If I enter email in dynamic field, its asking me for file and comma_separated fields.
Any help will be appriciated.
Thank You.
It seems that there's an issue with the validation rules you've defined. The 'required_without_all' validation rule is used to check if a given input field is required if all other specified fields are not present.
In your case, it seems like you want to validate that at least one of the three fields is present, not that all of the fields are absent. To achieve this, you can use the 'required_with' validation rule instead.
Try the following validation rules:
'comma_separated' => ['nullable','required_with:email.*,file'],
'file' => ['nullable','required_with:email.*,comma_separated', 'mimes:csv'],
'email.*' => ['nullable','required_with:comma_separated,file','email'],
This should validate that at least one of the fields is present, and the other fields can be left empty.

Check for posted data inside the config file or form_validation.php in CodeIgniter

I want to check for a posted data if present so that I can set a form field as required or not, without having to create a custom rule.
Is it possible to use/load/call CodeIgniter's input library inside the config file (specifically the form_validation.php config file) and get user posted data without having to use the native $_POST variable?
my code goes something like this...
...
array(
'field' => 'dob_day',
'label' => 'Day',
'rules' => (($this->ci->input->post('include_person') === 'yes') ? 'required|' : '') . 'integer|max_length[2]|greater_than[0]|less_than[32]|valid_birth_date'
),
...
Obviously, my approach does not work. hehe. If there's an alternative efficient codeigniter way of doing what i'm thinking, please let me know.
Thanks for all your help! :)
See this:
MANUAL
try this example, in this mode you can at runtime change your rules:
$this->form_validation->set_rules('dob_day', 'Day', 'your new rules');
Okay, I ended up with putting the logic in the controller, and just using inline validation. But to have inline validation work without having to set all the rules, I used the code from this website to allow both inline and config rules to mix.
http://brettic.us/2010/09/13/codeigniter-form-validation-mix-rule/
Cheers!

How to use: sonata_type_model_list

I'm trying to run this code:
if(!$this->isChild()) {
$formMapper->add('post', 'sonata_type_model', array(), array('edit' => 'list'));
From this tutorial: http://sonata-project.org/bundles/doctrine-orm-admin/2-1/doc/tutorial/creating_your_first_admin_class/defining_admin_class.html
I'm aware that you have to use sonata_type_model_list as of 2.1
sonata_type_model_list : this type replaces the option edit = list provided as a 4th argument on the sonata_type_model
The problem is that I have absolutely no idea how to do that. I have found ZERO examples anywhere after a whole day of google searches. All I want to do is replace the edit=>list with sonata_type_model_list.
Can you please tell me how to do that in the code above?
This is how I used it in my code. However it's not working in all browsers. When I select the taget entity the form value in the parent view doesn't get updated (FireFox and IE).
$formMapper->
...
->add('image', 'sonata_type_model_list',
array(
'compound' => true,
'by_reference' => true
)
)
...
I also find it very hard to find some tutorials/examples on how to use this type. Best thing you can do is to go through their source code. Which is awful time consuming.
One way I found out how to configure these form types is to provide a wrong argument.
e.g. 'my_compound' => true,
This will result in an error telling you that 'my_compound' isn't a valid parameter and also will show you a list of valid parameters.
Hope this helps!

Zend_Validate_inArray - ErrorMessage: non empty value

We're using Zend_Filter_Input to validate a Dojo From at Backend. There is "option" Input Element where we have to verify that the submitted value is allowed.
Problem: If nothing is selected the Zend_Validate_inArray Validator returns
" you must provide an non Empty value"
thats fine, but we have to change the message. I cant find the proper way to do this..
'FIELD' => array(new Zend_Validate_InArray($allowedValues),
'messages' => 'MESSAGE_WRONG_VALUE',
'default' => ''
),
Does someone know how to change the "isEmpty" Message?
It is Zend Framework 1.11.12
Try this:
$element->setRequired(true)->addErrorMessage('Your message');

CakePHP Unfilled radio button changed to an unwanted value on submit

I've got this form element:
$form->input('ChecklistResponseGovernmentInfo.driversLicenseIsOnline', array('type'=>'radio', 'empty'=> true, 'options'=>array(0 => 'No', 1 => 'Yes')))
This is the validation rule for it:
'driversLicenseIsOnline' => array(
'boolean' => array(
'rule' => array('boolean'),
'allowEmpty' => false,
),
),
And this is the database field for it (MySQL):
`driversLicenseIsOnline` tinyint(1) unsigned NOT NULL
When I first load a fresh copy of my form, the radio button set is unselected. If I submit the form without doing anything else, when the form reloads, the radio button is filled in as "No" and the validation flash message says "This field cannot be left blank".
The problem goes away when I stop using zero (0) as the value for "No", but I want Cake to store this value as a boolean, not as some other value that I would have to manually translate back and forth to boolean.
How do I stop Cake from automatically filling in a value for this element when it's submitted with no radio selected?
Stumbled onto this question while searching for the answer myself.
This nasty hack fixed it for me (in CakePHP 2.1 - should work in 1.3):
After you fail to validate, unset the value in $this->request->data if it's empty:
if ($this->Model->save($this->request->data) {
// data saved
}
else {
// data failed to save
// unset radio if empty
if ($this->request->data['Model']['radio_field'] == "") {
unset($this->request->data['Model']['radio_field'];
}
}
Ugh.
The way you have your radio buttons set up, there are actually 3 choices:
Nothing selected
'No' selected
'Yes' selected
Radio buttons should never have no choice selected (see point #9); (1) above shouldn't happen. So rather than answer your question directly, I'm going to say you should consider using a checkbox instead. Checkboxes are a natural fit for boolean values, and likely more usable in this case.
And Cake will assume you want a checkbox if the database field is tinyint(1), so you could then get rid of the options array in your FormHelper call.
Edit: ok, I don't think Cake automatically fill that value with 0, most likely it's a browser behavior. You can test that by debug($this->data) when the saving fails. (Just in case it does, you can unset that value there)
So I had a similar problem with a questionnaire, where it would be somewhat semantically correct for radio buttons to remain empty - when a yes or no question remains unanswered. In this case it doesn't seem right like this:
Do you smoke? [yes?]
And this is better:
Do you smoke? (yes)(no)
The way I solved this problem was 'options'=>array(1 => 'No', 2 => 'Yes')

Categories