Calling index() on form validation failure |
Hello, I'm doing something like this in my controller:
PHP Code: if ($this->form_validation->run() == FALSE) { Basically, if form fails, send back to form, if good, then send to "Thank you view". For testing I have added only one set_rules() rule: PHP Code: $this->form_validation->set_rules('f_eventname', 'Event Name', 'required|trim|min_length[3]|max_length[100]|callback_customAlpha'); The action goes to index as expected, but f_eventname always seems to fail, no matter what I put in there. callback_customAlpha is in a helper file that is loaded in the construct function. Anyway, I thought I'd post here to see what you all think as my eyes are "end of day eyes" right now. Thanks, Donovan
You cannot use callback if your "customAlpha" is in a helper.
As far as I know a callback as to be in the same class as the validation, but not 100% sure as the documentation says controller. In your case you need to use the method describe here: Callable: Use anything as a rule A good decision is based on knowledge and not on numbers. - Plato
Well jump'n gee willikers. I guess I'll have to replicate that function in my various controllers.. as I don't feel like rewriting all the set_rules that are already in place. Would be nice if I could define that function in a global helper via callback_.
Thanks for the comments! Donovan
10-26-2016, 07:08 AM
(This post was last modified: 10-26-2016, 08:18 AM by salain. Edit Reason: Syntax error )
I have tried with a validation function in a helper
You just have change the call to your function. From this PHP Code: $this->form_validation->set_rules('f_eventname', 'Event Name', 'required|trim|min_length[3]|max_length[100]|callback_customAlpha'); to PHP Code: $this->form_validation->set_rules('f_eventname', 'Event Name', Your custom validation function is in an array to allow to set the error message. A good decision is based on knowledge and not on numbers. - Plato
Salain, that seems to be working for me.. you are missing one ')' I think, but thanks much! <reputation props as well> :-)
Cool.
I know about the missing ) , a wee bit harder to type on a mobile. A good decision is based on knowledge and not on numbers. - Plato
Hmmm, one catch. the array method seems to create a required-like attribute (where as callback_ does not).
For example, if I have this: PHP Code: $this->form_validation->set_rules('f_orgcity', 'Organization City', array('trim','max_length[50]', array('customAlpha','customAlpha'))); And I don't put anything in the 'f_orgcity' input field.. It still generates the error. I took another look at the docs and did not see a solution.. is this a bug? Thanks, Donovan
Hi,
I always use an array for the validation rules and I have not add this issue before. You are using the latest version of CI ? What is the error message ? You could add a error message array to the set_rules for "customAlpha" to see if it is failing. PHP Code: $this->form_validation->set_rules('f_orgcity', 'Organization City', Can you post your customAlpha function. A good decision is based on knowledge and not on numbers. - Plato
|
Welcome Guest, Not a member yet? Register Sign In |