CodeIgniter Forums
Validation Thread - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Validation Thread (/showthread.php?tid=4815)



Validation Thread - El Forum - 12-17-2007

[eluser]cinewbie81[/eluser]
Hi all,

Can i have something like this for my validation:
Code:
If Dropdown menu select item == '0' then
   $rules['email_name'] = "required|valid_email"
else
   $rules['email_name'] = ""

Anyone ?


Validation Thread - El Forum - 12-17-2007

[eluser]Developer13[/eluser]
Have you tried?


Validation Thread - El Forum - 12-17-2007

[eluser]Nick Husher[/eluser]
The answer is yes; at least I'm 99% sure it is. Smile


Validation Thread - El Forum - 12-17-2007

[eluser]Developer13[/eluser]
I think the question would be better answered if the author posted some specific code along with the specific problems they have with that code.


Validation Thread - El Forum - 12-17-2007

[eluser]cinewbie81[/eluser]
Ignored my post above, and look at the following code:

I have a select box with following value:
Code:
<select name="gender" id="gender">
<option value="0">Please Select a gender type</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>

In my controller i want something like this:
Code:
function validation()
{
    // if (selectbox value = 0) prompt error message ("Gender must be either male or female)

    // else if value == 1 (Male) or value == 2 (Female) => Validation PASSED
}
How can i do it ?
what validation rules should i put for gender select box ?? I dont think rules['gender'] = 'required' is the correct one .. anyone ?


Validation Thread - El Forum - 12-17-2007

[eluser]Nick Husher[/eluser]
The way I'd do it (and there might be a better way) is to create a simple callback that checks that:

Code:
...
$rules['gender'] = 'callback__check_gender';

...

function _check_gender($gender) {
  if($gender == 0) {
    $this->validation->set_error_message('_check_gender', 'Gender must be set.');
    return false;
  } else { return true; }
}



Validation Thread - El Forum - 12-17-2007

[eluser]cinewbie81[/eluser]
Hi Nick Husher,

That's works ..
But do u have any idea how to re-populate the gender value when there's a error in validation ??

When i choose the gender = male, but for other field i type in invalid format data. After the validation error message display, gender will reset back to 'Please select a gender type' instead of 'Male'. I know how to do it for input type, but for select box i have no idea


Validation Thread - El Forum - 12-18-2007

[eluser]Nick Husher[/eluser]
In your view, set up the following code:
Code:
&lt;?php
  $options = array( '0' => 'Select your gender', '1' => 'Male', '2' => 'Female' );
  echo form_dropdown('gender', $options, $form->validation->gender);
?&gt;

To do this you need to include the form helper and set your validation library up for field repopulation.

Form Helper - scroll to the 'form_dropdown' section
Validation - scroll to the 'repopulating the form' section.


Validation Thread - El Forum - 12-20-2007

[eluser]cinewbie81[/eluser]
Validation question again:

What's the validation rules for decimal points in CI??