Welcome Guest, Not a member yet? Register   Sign In
validation library in models
#1

[eluser]Riley123[/eluser]
I would like to have my validation code in my model. It's working fine except custom callbacks are ignored.

How do i add custom callbacks so they work within a model?
#2

[eluser]Molchy[/eluser]
[quote author="Riley123" date="1330965495"]I would like to have my validation code in my model. It's working fine except custom callbacks are ignored.

How do i add custom callbacks so they work within a model?[/quote]

U can't:
U have to first create controller function with name of the callback:

callback_valid_user -> U have to create then controller function valid_user which calls model and returns TRUE or FALSE

If u use HMVC dont forget to put in validation run function "$this" else callback doesn't work.

#3

[eluser]Riley123[/eluser]
ah thanks. So the validation library will call the controller. I added the validation to my controller and it's all good. One weird thing is it loses the field name so I've placed in __FUNCTION__ which works but looks tacky.

<code>
public function username_check($str)
{
if ($str == 'admin')
{
$this->form_validation->set_message(__FUNCTION__, 'The %s field can not be the word "admin"');
return FALSE;
}else{
return TRUE;
}
}
</code>
#4

[eluser]CroNiX[/eluser]
__FUNCTION__ should be the name of the callback function, so it would be 'username_check' in your case.
#5

[eluser]Aken[/eluser]
All your set_message() calls should be in the same area as your set_rules() functions. It only sets what the error text should be, not if an error actually exists. Then your function can return true or false as it should.
#6

[eluser]skunkbad[/eluser]
search the forum for external_callbacks. You can put callbacks anywhere you want.
#7

[eluser]Molchy[/eluser]
[quote author="Aken" date="1330993877"]All your set_message() calls should be in the same area as your set_rules() functions. It only sets what the error text should be, not if an error actually exists. Then your function can return true or false as it should.[/quote]

Must correct u Wink set message can be also in callback Wink:

$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
#8

[eluser]Aken[/eluser]
Yes, you can - but just because you can doesn't mean you should. When you set rules, you're basically configuring your validation. Why wouldn't you continue to configure the error messages at the same time? Like I said, it doesn't actually trigger an error message, so there's no reason it can't be done early along with your other settings. And it eliminates potential errors/confusion like this user was experiencing.




Theme © iAndrew 2016 - Forum software by © MyBB