tags:

views:

45

answers:

2

Hay all, I am trying to validate a select in kohana 3.0 and I am using the necessary rules. However the validation does no "kick in" when the user does not make a selection.

<select id="discipline" name="discipline" >
<option value="0"> -- Select One -- </option>
<option value="-2">Information Technology and Engineering</option>
<option value="4">Business and Training Seminars</option>
</select>

That was my select, now i have applied these rules to the post array before i check for validation errors.

$post = Validate::factory($_POST)
                ->rule('discipline', 'not_empty')
                ->rule('discipline', 'numeric');

When I submit the form without making a selection, the form submits and the rules should stop it.

Any ideas ?

+2  A: 

Your still putting a value for the first one, as 0. Leave the value as value="". 0 is numeric and considered not empty.

mikelbring
or remove `value` attribute from the first option
biakaveron
If you remove value, then the value will become -- Select One -- Which will not work
mikelbring
Yes, of course! *confused*
biakaveron
+1  A: 
  1. Replace 0 with blank string
  2. Add ->rule('discipline', 'in_array', array(array(-2, 4))); to check that selected discipline is within valid collection.
zerkms

related questions