Welcome Guest, Not a member yet? Register   Sign In
a custom callback to check if the value is empty
#1

[eluser]dedenf[/eluser]
Hi,
first of all i'm sorry if this kind of question is already posted, i've been search and not found any result that match my case.

i make a custom callback function to check if the value is empty or not, and to make a custom message for it
Code:
function number_check($val){
if(empty($str)){
   $this->validation->set_message('number_check', '%s cannot be empty');
   return FALSE;
  }
}
and its not run by CI, when i look to the Validation Libraries(Validation.php), i found this on line 200
Code:
if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
{
   continue;
}
CI will always skip my callback if that $str(means the value of the field) doesn't have any value, and my callback function won't be read by validation library.
the validation rules, only check if the field value is not empty. but when i commented the "continue;"
Code:
if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
{
   //continue;
}
it works, my callback is not skipped by the CI Validation, even with the "required" rules on the other field rules.
i wonder, is that the purpose of the validation library? that i can't use the callback if the value is empty? i know that i can use required rules to check whether its empty or not, but in my case, i just want to make a custom message (in my language) on the different field.

Thanks
ps: i use CI 1.6.1
#2

[eluser]KeyStroke[/eluser]
I just read the comment above the code you've posted and it said:
Code:
// Is the field required?  If not, if the field is blank  we'll move on to the next test
So, yea, that's what it's probably doing.

What other rules are you using in your validation? did you try using required in your validation rules so it doesn't accept an empty value?
#3

[eluser]xwero[/eluser]
the full snippet is
Code:
// Is the field required?  If not, if the field is blank  we'll move on to the next test
            if ( ! in_array('required', $ex, TRUE))
            {
                if ( ! isset($_POST[$field]) OR $_POST[$field] == '')
                {
                    continue;
                }
            }
As you can see in the comment if the field isn't required then the check is made to see if the field is non existent or an empty string.

It's true the validation library should have an option to overwrite the default error message. This is one of the things i changed in my validation library : Validate
#4

[eluser]dedenf[/eluser]
thanks for the reply.
yes i'm aware of that comment/purpose of the code block, the point is, why CI "not allowed" me to create a custom callback for my custom message just to check if my field has an empty value or not and pull out my custom message.
and i know i can use the "required" rules to check field for my case, but again i just want to create a custom message.
#5

[eluser]KeyStroke[/eluser]
Probably because it already has the functionality to check for empty fields. If you don't like the message CI returns for empty fields by default, then you can change it by going to: System > Language > English > validation_lang.php

Then modify the string assigned to $lang['required'].
#6

[eluser]xwero[/eluser]
A hackish solution would be to change the error message for the error array
Code:
if(!this->validation->run())
{
   $this->validation->error_string = '';
   foreach($this->validation->_error_array as $key => $error)
   {
       $default_error = sprintf($this->lang->line('required'),'fieldname'); // or the friendly name
       if($default_error == $error)
       {
           $error = sprintf($this->lang->line('custom_required'),'fieldname');
           $this->validation->error_string .= $this->validation->_error_prefix.$error.$this->validation->_error_suffix;
       }
       else
       {
           $this->validation->error_string .= $this->validation->_error_prefix.$error.$this->validation->_error_suffix;
       }
   }
}
#7

[eluser]xwero[/eluser]
Instead of adding it to the controller you could extend the Validation library
Code:
class MY_Validation extends CI_Validation
{
    function set_custom_message($field,$rule,$custom)
    {
       $this->validation->error_string = '';
       foreach($this->_error_array as $key => $error)
       {
          
          $fieldname = (in_array($field,$this->_fields)?$this->_fields[$field]:$field;
          
          $default_error = sprintf($this->lang->line($rule),$fieldname);
          
          if($default_error == $error)
          {
              $error = sprintf($this->lang->line($custom),$fieldname);
              $this->validation->error_string .= $this->_error_prefix.$error.$this->_error_suffix;
              if(in_array($field,$this->_fields)
              {
                  $sa_error = $fieldname.'_error';
                  $this->$sa_error = $this->_error_prefix.$error.$this->_error_suffix
              }
          }
          else
          {
             $this->validation->error_string .= $this->_error_prefix.$error.$this->_error_suffix;
          }
       }
    }
}
This function checks if the friendly name is set with the set_fields function and generates an individual error message.

The method should be called in the controller when the validation run method is false.
#8

[eluser]dedenf[/eluser]
wow, thanks guys!
it's more than i expected Smile *xwero even wrote a hackish solution, i'm on it*
i think the conclusion is, by default CI never allowing user to create a custom callback to check the value is empty.
thank you guys




Theme © iAndrew 2016 - Forum software by © MyBB