a custom callback to check if the value is empty |
[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){ Code: if ( ! isset($_POST[$field]) OR $_POST[$field] == '') 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] == '') 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
[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 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?
[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 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
[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.
[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'].
[eluser]xwero[/eluser]
A hackish solution would be to change the error message for the error array Code: if(!this->validation->run())
[eluser]xwero[/eluser]
Instead of adding it to the controller you could extend the Validation library Code: class MY_Validation extends CI_Validation The method should be called in the controller when the validation run method is false.
[eluser]dedenf[/eluser]
wow, thanks guys! it's more than i expected *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 |
Welcome Guest, Not a member yet? Register Sign In |