Welcome Guest, Not a member yet? Register   Sign In
Problem with setting a default zero in form validation rule
#1

[eluser]jprateragg[/eluser]
In my controller, I'm using this form validation rule to clean my input field:

Code:
$this->form_validation->set_rules('adjusted', 'Adjusted Value', 'trim|clean_int|default_zero');

I'm using a helper function (default_zero) to set any values to 0 if they are empty or null.

Code:
function default_zero($input) {
if($input == '' or $input == null) {
  return 0;
} else {
  return $input;
}
}

However, whenever the I try to submit an empty value, it never comes back 0. If I enter 0 into the field, a 0 gets returned. If I enter a number, that number gets returned. But when I leave the field blank, nothing gets returned when I was expecting a 0 to be returned. Is this a bug or am I not doing something right? Thanks!
#2

[eluser]Mirge[/eluser]
Show where you are re-populating the form. Make sure you're using set_value('form_field_name');
#3

[eluser]jprateragg[/eluser]
Yes, I was repopulating the form:

Code:
<input type="text" name="adjusted" id="adjusted" value="<?php echo set_value('adjusted'); ?>" />
#4

[eluser]Mirge[/eluser]
I set it up on my local dev environment... it looks like the helper default_zero() isn't being called if a value isn't specified. To get around this, you'll need to use a callback.

Here's an example:

Code:
$this->form_validation->set_rules('adjusted', 'Adjusted Value', 'trim|clean_int|callback__default_zero'); // 2 underscores
...
...
...
public function _default_zero($input)
{
return ($input == '' || $input == NULL) ? 0 : $input;
}

The downside to this is that the method is now in your controller -- you might try extending the Form_validation library by creating your own MY_Form_validation.php in libraries/.
#5

[eluser]jprateragg[/eluser]
I can't use a callback because a callback can only return true/false. Undecided I had tried putting it in MY_Form_validation (because I have another function in there) but it doesn't work there either.
#6

[eluser]Mirge[/eluser]
[quote author="jprateragg" date="1353528747"]I can't use a callback because a callback can only return true/false. Undecided I had tried putting it in MY_Form_validation (because I have another function in there) but it doesn't work there either.[/quote]

You can return more than just true/false from a callback.

http://ellislab.com/codeigniter/user-gui...#callbacks

If you return FALSE from your callback, you should set an error message first and then return false.

If you return NOTHING, the value is unchanged.

If you return anything other than FALSE, then the value you return is what that field's value becomes. In my example code (which is tested) if I don't specify a value in the form field, then it re-populates with "0" because I returned 0. Try it.
#7

[eluser]tpetrone[/eluser]
I don't think you should be using the validation functions as a way to "set" data if a NULL condition is detected.

Validation is just testing the current values against a set of rules..

If you need to modify the $_POST/$this->input->post data, either do it before you validate or after you validate.

IF your going to use a callback in your validation then the callback should probably return either a TRUE/FALSE response.


Code:
function validateValue($value){

  if(empty($value) || is_null($value)){
  
    $this->form_validation->set_message('some_value', 'This field cannot be empty');
     return(FALSE);  // The value is NULL

   }else{

     return(TRUE);
  
   }


}
#8

[eluser]Mirge[/eluser]
There's nothing wrong with adjusting the value in a callback, but checking before your form validation is certainly a viable alternative.

BTW, if you return TRUE from your callback then you can't alter the value. See straight from documentation:

Code:
Note: You can also process the form data that is passed to your callback and return it. If your callback returns anything other than a boolean TRUE/FALSE it is assumed that the data is your newly processed form data.
#9

[eluser]jprateragg[/eluser]
@Mirge: "If your callback returns anything other than a boolean TRUE/FALSE it is assumed that the data is your newly processed form data." -- I don't know how I missed that part. Probably from reading all the documentation in a short period of time.

I'm using CI 2.1.3. I think I found the problem in system/libraries/Form_validation.php in "public function run()" on line 335:

Code:
if (isset($_POST[$field]) AND $_POST[$field] != "")
{
$this->_field_data[$field]['postdata'] = $_POST[$field];
}

$this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);

The if statement is looking to see if the data in the post field is empty, and if it's not, cache it in the $_field_data array. I may have to modify this function so it adds empty values. I don't know if this will have any negative effects though.
#10

[eluser]Mirge[/eluser]
Modifying core is highly recommended AGAINST. Extend the method instead and modify it there.

You'll run into issues with this though. Use a callback, or modify the value BEFORE form validation takes place.




Theme © iAndrew 2016 - Forum software by © MyBB