CodeIgniter Forums
validation library in models - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: validation library in models (/showthread.php?tid=49850)



validation library in models - El Forum - 03-05-2012

[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?


validation library in models - El Forum - 03-05-2012

[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.




validation library in models - El Forum - 03-05-2012

[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>


validation library in models - El Forum - 03-05-2012

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


validation library in models - El Forum - 03-05-2012

[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.


validation library in models - El Forum - 03-05-2012

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


validation library in models - El Forum - 03-06-2012

[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"');


validation library in models - El Forum - 03-06-2012

[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.