Welcome Guest, Not a member yet? Register   Sign In
Validating POST data that is NULL or EMPTY
#1

[eluser]sentinel[/eluser]
Hi, I'm sure the following problem is quite simple, but just can't seem to figure it out.

In one of my forms, I call several callback functions for validation. Some of my form elements are mandatory, but some are optional (i.e. ensuring input is either null or a numeric value).

In this case, the first check is for a field that is required and must be numeric.

I use the following rule within my rules array:

Code:
array('field' => 'policyLoan', 'label' => 'Policy Loan Amount', 'rules' => 'required|trim|callback_policyLoan_check'),

When I try and submit the form, if the input is left empty, no errors are returned and nothing seems to happen. However, the form is being submitted and a view returned, because I just checked it on the error log.

If I insert text instead of a number, the validation runs OK and flags up the errors. But if the input is left blank, the validation also does not run on that input, but also does not run the validation on the other form inputs.

The callback function code is:

Code:
function policyLoan_check($str) {

if(empty($str) || is_null($str) || !is_numeric($str)) {
   $this->form_validation->set_message('policyLoan_check', 'The amount of the policy loan must be entered in numeric format.');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
  }

I can't see anything obviously wrong here. Where could I be going wrong ?? I'm baffled.

Thanks
Neil.
#2

[eluser]Otemu[/eluser]
Hi,

When you use 'required' this 'Returns FALSE if the form element is empty.'
However in your callback you are already checking if the string is empty or null so 'required' is not needed.

Can you not just use 'numeric' ??? There is even is_numeric but its not listed in the guide.
#3

[eluser]sentinel[/eluser]
Actually I was doing two checks, firstly to check for an empty string or a non-numeric string.

I think the problem was I was running seperate validation checks because in this specific instance, you can add or delete items on the same form and I had to run seperate validation checks depending upon what was submitted.

As usual, as soon I post an issue after staring at the same code and banging my head against the wall for a day or so, the answer suddenly comes to me.

The validation rules themselves were OK. I just had to add an additional hidden field so that the posted variables could be redirected to the correct validation checks when submitted. Duh !!!




Theme © iAndrew 2016 - Forum software by © MyBB