Welcome Guest, Not a member yet? Register   Sign In
form validation (must enter something in at least one field)
#1

[eluser]andychurchill[/eluser]
I had a form on another site where the requirement was to enter something in an email address field, or in the phone number field, or both. I wondered how I would incorporate this kind of validation into a codeigniter site using the form validation library?
#2

[eluser]überfuzz[/eluser]
Check out the user guide.
#3

[eluser]Aken[/eluser]
1) Use the non-deprecated Form Validation library, not the one Uberfuzz linked.

2) There's no rule in the Form Validation library that checks against whether one of two or more fields is filled in. You'd have to create your own callback function rule that checks the forms and makes sure one is filled in.
#4

[eluser]überfuzz[/eluser]
[quote author="Aken" date="1259462896"]1) Use the non-deprecated Form Validation library, not the one Uberfuzz linked.
...[/quote]
Ops my bad!
#5

[eluser]Colin Williams[/eluser]
Just set the rules conditionally, since the rules change depending on the condition of another field.
#6

[eluser]andychurchill[/eluser]
[quote author="Colin Williams" date="1259520884"]Just set the rules conditionally, since the rules change depending on the condition of another field.[/quote]

Not sure I understand here. I need to check if a field is empty before checking the other field? or alternatively run a callback function on the first field, which says if the field is empty and field2 is empty then set a message and return false?

In either case, how do I get the value of a form field, using the form validation library? reading the documentation for the form validation library, I thought I might be able to get the values using set_value('email'); and set_value('phone'), but I don't think this works. I'd have thought there is a "proper" way.

$this->form_validation->set_rules('email', 'Email', 'callback_emailphone_check');

function emailphone_check($str)
{
if(empty($str) && empty(set_value('phone')))
{
$this->form_validation->set_message('emailphone_check', 'You must supply either a phone or an email address.');
return FALSE;
}
else
{
return TRUE;
}
}
#7

[eluser]Colin Williams[/eluser]
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');
}

You could streamline it a bit more, but that's the general idea
#8

[eluser]andychurchill[/eluser]
cheers, input->post was the trick to it, makes perfect sense now - I had expected it to be part of form_validation, but I guess it makes more sense to be using input->post because the field may not need validating Smile
#9

[eluser]Unknown[/eluser]
Try to use arrays in field names.
Code:
$this->load->library('form_validation');

$this->form_validation->set_rules('contacts', 'Contacts', 'required');

$this->form_validation->set_rules('contacts[email]', 'E-mail', 'valid_email|xss_clean');
$this->form_validation->set_rules('contacts[phone]', 'Phone', 'xss_clean');




Theme © iAndrew 2016 - Forum software by © MyBB