CodeIgniter Forums
Field validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Field validation (/showthread.php?tid=30702)



Field validation - El Forum - 05-23-2010

[eluser]elmne[/eluser]
If i need to validate and ensure that atleast the user must fill in one field out of 3, how do i do that?

There is an example of how to do it with two fields, but what if a user must enter atleast one of the 3 or more fields? How would that be configured?

Below is the example shown for checking that atleast one of two fields is input, but it doesn't work for 3 fields.


Code:
// If no email address, phone is required
if ( ! $this->input->post('email'))
{
$this->form_validation->set_rules('phone', 'Phone Number', 'required');
}
else
{
$this->form_validation->set_rules('phone', 'Phone Number', '');
}

// If no phone number, email is required
if ( ! $this->input->post('phone'))
{
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
}
else
{
$this->form_validation->set_rules('email', 'Email Address', 'valid_email');
}



Field validation - El Forum - 05-24-2010

[eluser]benurv[/eluser]
use a config array to set your validation rules and leave the required rule empty or just an array with the fields

$array = array('field1','field2','field3');

$foreach ($array AS $a)
{

if($this->input->post($a))
{
...code here
}

}