2

My form has a cellphone and a phone field. I want the user to fill either one of the fields, or both, but not neither.

I've seen ways to do it in other languages, but could I have some advice on how to do it with Codeigniter?

1
  • use callback to compare phone with cellphone Commented Nov 14, 2011 at 2:53

3 Answers 3

7

You can do it as:

$this->form_validation->set_rules('phone', 'Your validation message.', 'callback__phone_check');

And make a function:

function _phone_check() {
   //check for phone and cellphone field.
  //make sure one field is not empty and return accordingly
}

Hope that helps

Sign up to request clarification or add additional context in comments.

2 Comments

Where should I do this ? In view , controller?
@jQuery.PHP.Magento.com that should be in your controller..! Read more here:: ellislab.com/codeIgniter/user-guide/libraries/…
5

Just wanted to throw some quick code snippets your way. I wanted to accomplish this and this question was one of the first entries in google when I did my search. I took inspiration from the other answers and here's what I did which worked for me (YMMV):

$this->form_validation->set_message('contactVerify', 'Either Phone or Email is required');
...
$this->form_validation->set_rules('phone', 'Phone', 'trim|callback_contactVerify[email]|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|callback_contactVerify[phone]|valid_email|xss_clean');
...
public function contactVerify($contact, $otherField) {
  return ($contact != '' || $this->input->post($otherField) != '');
}

Since this is a very simple validation function, I pre-set the error message so I don't have to set it inside validation function. I pass in the other field I want to check as the second argument via the square brackets.

Hope this is useful.

1 Comment

Sadly, I don't think this works anymore. I think if "required" is not present in the rules, no other rules are checked (for efficiency, I presume).
-5

Just do a JS validation on client-side, and PHP validation on server-side (or use CI's form helper)... Anyway, PHP should look something like this:

if ($_POST['cellphone'] == '' OR $_POST['phone' == '') {
// do stuff like make an notification, alert, etc... and take users back to the form
}

3 Comments

I have created an array of all validation in the config folder. so what should i do for this type of validation. @Shomz
we need a rule not core php
@Darshanambaliya you can ask another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.