Welcome Guest, Not a member yet? Register   Sign In
form validation for not mandatory field
#1

[eluser]pallavi312[/eluser]
hello,
i m facing problem while doing form validation.
My query is i have field say "phone no" which is not manadatory. but if user enters value in phone field it should perform validation. how this can be achived with form validation in codeigniter?

Please send me reply.....
#2

[eluser]umefarooq[/eluser]
hi check the CI user guide for form validation where you can find small tutorial.

http://ellislab.com/codeigniter/user-gui...ation.html
#3

[eluser]pallavi312[/eluser]
hello
i have checked user_guide. then i write following code in controller file

$field_rule=array(
array('field'=>'firstname','label'=>'First name','rules'=>'trim|required|xss_clean|alpha|max_length[20]'),
array('field'=>'lastname','label'=>'Last name','rules'=>'trim|required|xss_clean|alpha|max_length[20]'),
array('field'=>'company','label'=>'Company name','rules'=>'trim|required|xss_clean|max_length[20]'),
array('field'=>'email','label'=>'Email','rules'=>'trim|required|valid_email|xss_clean'),
array('field'=>'phone','label'=>'Phone','rules'=>'trim|callback_phone_check'),
('field'=>'fax','label'=>'fax','rules'=>'trim|required|alpha_dash|callback_phone_check'),
array('field'=>'spnootate','label'=>'Special notation','rules'=>'trim|required')
);

$this->form_validation->set_rules($field_rule);



& function callback like this

function phone_check($no)
{
echo "number:".$no;

if(!preg_match("/(^\d{10}$)|(^\d{3}-\d{3}-\d{4})$/",$no))
{
$this->form_validation->set_message('phone_check','Please enter %s number in format 1234567890 or 123-123-1234');
return FALSE;
}

else
{
return TRUE;
}

}


but when i run form on clicking submit without entering phone no field value it give me error message for phone no field. but as this field is not mandatory. it should give me error meg only when i entered value of phone field in wrong format.... Please advise me how to do this?
#4

[eluser]verynewtothis[/eluser]
Try this format instead, for setting up your validation rule.

e.g's

Code:
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');

Then for the phone validation do this:
Code:
if ($this->input->post('phone')) {
$this->form_validation->set_rules('phone', 'Phone', 'trim|callback_phone_check');
}

If the above "if" condition doesn't work then try this instead:
Code:
if (trim(strcmp($this->input->post('phone'),''))) {
$this->form_validation->set_rules('phone', 'Phone', 'trim|callback_phone_check');
}

I think one more solution could have been to add a check at the start of your callback function which would return false if the field is empty:

Code:
function phone_check($no)
  {
if (!$this->input->post('phone')){
return FALSE;
}      
echo “number:”.$no;
    
      if(!preg_match(”/(^\d{10}$)|(^\d{3}-\d{3}-\d{4})$/”,$no))
      {
          $this->form_validation->set_message(‘phone_check’,‘Please enter %s number in format 1234567890 or 123-123-1234’);
          return FALSE;
      }
      
      else
      {
      return TRUE;
      }
#5

[eluser]tomcode[/eluser]
Your callback phone_check() is called every time the post array has a field 'phone'. So You need to add an empty check to it :

[code]
function phone_check($no)
{
if(empty($no)) return true;

// Your code ..
}
#6

[eluser]pallavi312[/eluser]
Hello,

First of all thanks for the solution...
i have tried with solu 1 & its worked..Thnks...

Initially i was tring with 2nd solution but it gives me error message like this
"Unable to access an error message corresponding to your field name." when i entered phone no value & submit form...
as i used this function phone_check() for phone no as well as fax no. where fax no is mandatory.
like this

array('field'=>'phone','label'=>'Phone','rules'=>'trim||alpha_dash|callback_phone_check'),
array('field'=>'fax','label'=>'fax','rules'=>'trim|required|alpha_dash|callback_phone_check'),

what is error cause?
#7

[eluser]tomcode[/eluser]
Before returning false in an validation callback You must set an error message.




Theme © iAndrew 2016 - Forum software by © MyBB