Welcome Guest, Not a member yet? Register   Sign In
Validation rule required on multiple select field always fails
#1

(This post was last modified: 06-18-2016, 02:56 PM by Nikos_Kontis.)

Hello everyone, 

I don't know if this is a bug or something that I missed in the upgrade guide from CI2 to CI3, but here goes...
My problem is related to validating with the "required" rule a SELECT field that can capture multiple values.

The code I was using until I migrated CI3 seemed to work properly, but after I migrated to CI3 it fails to pass an if no data are being send send from the field. If I remove the required validation rule, the whole logic works properly and the data are saved/updated properly in the database.

HTML:

Code:
<select id="groups" name="groups[]" multiple="multiple" >
   <option value="1">Group A</option>
   <option value="2">Group B</option>
   <option value="3">Group C</option>
   <option value="4">Group D</option>
</select>

and the validation rule in the PHP controller:
PHP Code:
$rules = [        
    [
'field' => 'groups''label' => 'groups''rules' => 'required']
];
$this->form_validation->set_rules($rules);
$validation_result $this->form_validation->run(); 

I appreciate any help or guidance, thanks.
Reply
#2

unless i can't find it - there is no ci validation rule called "required_select" . and if that is true, and it was working before - then somewhere in your previous app there was custom code. i googled that term and there are references to it in stackoverflow. is it possible you created a My_form_validation in CI 2 and then didn't bring it over? anyways google it and you will see what i'm talking about. the rule for required is just 'required' . and you might need to put brackets on the field name like 'field' => 'groups[]' but i've never worked with a multiple select field so i'm not sure if that is correct.
Reply
#3

My bad, I meant 'rules' => 'required' in the example PHP code (I edited my initial question).
Any how I found the solution to my problem, I had to change the name of the field in the rules. So instead of usings 'groups'

Code:
$rules = [        
   ['field' => 'groups', 'label' => 'groups', 'rules' => 'required']
];

is used 'groups[]'

PHP Code:
$rules = [        
    
['field' => 'groups[]''label' => 'groups''rules' => 'required']
]; 
and it worked. The strange thing using 'groups' seemed to worked for CI2.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB