CodeIgniter Forums
Validation Question - 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: Validation Question (/showthread.php?tid=11864)



Validation Question - El Forum - 09-25-2008

[eluser]LifeSteala[/eluser]
Hey guys - trying to validate a registration form which works but what I want it to do does not. The form accepts a home phone and a cell phone. Either one of those fields must be filled so I've created a callback function but I always get "Field is required!".

Code:
// Set Validation Rules
$rules['phone']      = "trim|required|callback__phoneCheck";
$this->validation->set_rules($rules);

// Callback Function
// Function: phoneCheck - Private
// Check to see if phone or mobile is entered
//-----------------------------------------------------------
function _phoneCheck($number)
{
if ( $this->input->post('phone') != "" && $this->input->post('mobile') != "" )
return false;
else
return true;
}

I figured if both values are empty then return false.. Any ideas?

Thanks!


Validation Question - El Forum - 09-25-2008

[eluser]Colin Williams[/eluser]
Quote:Either one of those fields must be filled

So, requirement is conditional. Sounds like you need a conditional IF construct:

Code:
if ( ! $this->input->post('cell'))
{
  $rules['phone'] = "trim|required|callback__phoneCheck";
}
else
{
  $rules['cell'] = "trim|required|callback__phoneCheck";
}



Validation Question - El Forum - 09-28-2008

[eluser]LifeSteala[/eluser]
Gosh - I don't know what I was thinking the other day! I should have thought of that. Anyway, thanks Colin, worked like wonders! Smile