Welcome Guest, Not a member yet? Register   Sign In
Field validation
#1

[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');
}
#2

[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
}

}




Theme © iAndrew 2016 - Forum software by © MyBB