Welcome Guest, Not a member yet? Register   Sign In
Help with form validation
#1

[eluser]RecoilUK[/eluser]
Hi guys

I have an issue with the form validation class, and was wondering on the best way to proceed.

The problem is this ...

I have 2 fields - postcode1 + postcode2

These fields will ultimately be combined in postcode.

The form works fine, but is there a manual way of injecting postcode into the class so I can still use the form validation functions on it, instead of checking both postcode1 and postcode2 and displaying both error messages? I only want the one error message.

Thanks guys.
#2

[eluser]LuckyFella73[/eluser]
When you set up the rules you could set a rule for
only one of the fields. As rule set a callback function
that make the complete validation. That way you can
set just one error message.
#3

[eluser]RecoilUK[/eluser]
Hi

Thanks for the reply.

But I found another way ...

Code:
$this->form_validation->set_rules('postcode', 'Postcode', 'required|alpha_numeric_space|min_length[8]|max_length[9]');
  $this->form_validation->set_rules('postcode1', 'Postcode', 'required|alpha_numeric_space|min_length[3]|max_length[4]');
  $this->form_validation->set_rules('postcode2', 'Postcode', 'required|alpha_numeric_space|min_length[3]|max_length[4]');

and then ...

Code:
if(isset($_POST['submit'])) {
   $_POST['postcode'] = $_POST['postcode1'] . ' ' . $_POST['postcode2'];
  }

But you only echo the one error message for postcode. You need the other two rules otherwise the fields don't get re populated.

What do you think?
#4

[eluser]RecoilUK[/eluser]
Hi guys

Actually, I decided that's not very good, so am using the callback functionality after all, but have another problem, here is my callback function ...

Code:
function _check_postcode() {
  
  $this->load->library('form_validation');
  $postcode1 = $this->input->post('postcode1');
  $postcode2 = $this->input->post('postcode2');
  
  if($postcode1 == '' && $postcode2 == '') {
   $this->form_validation->set_message('_check_postcode', 'This is a required field!.');
   return $postcode1;
  }
  
  if($postcode1 == '' || $postcode2 == '') {
   $this->form_validation->set_message('_check_postcode', 'Both field\'s are required!.');
   return FALSE;
  }
  
  if(strlen($postcode1) < (3)) {
   $this->form_validation->set_message('_check_postcode', 'Part 1 of this field must contain 3 characters!.');
   return FALSE;
  }
  
  if(strlen($postcode1) > (4)) {
   $this->form_validation->set_message('_check_postcode', 'Part 1 of this field must contain no more than 4 characters!.');
   return FALSE;
  }
  
  if(strlen($postcode2) < (3)) {
   $this->form_validation->set_message('_check_postcode', 'Part 2 of this field must contain 3 characters!.');
   return FALSE;
  }
  
  if(strlen($postcode2) > (4)) {
   $this->form_validation->set_message('_check_postcode', 'Part 2 of this field must contain no more than 4 characters!.');
   return FALSE;
  }
  
  if(! preg_match("/^([a-z0-9])+$/i", $postcode1 . $postcode2)) {
   $this->form_validation->set_message('_check_postcode', 'This field can only contain alphanumeric characters!.');
   return FALSE;
  }
}

Now when the field passes all checks, its works as it should and does not display an error message, the problem is, when other fields fail there checks, the postcode field does not get repopulated.

Any ideas?
#5

[eluser]RecoilUK[/eluser]
Actually ignore that question, I worked it out, just needed an ...

Code:
return TRUE;

at the end.

#6

[eluser]RecoilUK[/eluser]
I,m always doing that, asking a question, and as soon as I've asked it, its like a lightbulb appears over my head, and I have the answer, does that happen to any one else?




Theme © iAndrew 2016 - Forum software by © MyBB