Welcome Guest, Not a member yet? Register   Sign In
Calling index() on form validation failure
#1

Hello, I'm doing something like this in my controller:

PHP Code:
       if ($this->form_validation->run() == FALSE) {
 
           $this->index();
 
       } else {
 
           $this->load->view('template/meta-head'$custom);
 
           $this->load->view('template/header');
 
           $this->load->view('fastauditthx');
 
           $this->load->view('template/footer'$custom);
 
       

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
Reply
#2

return $this->index();
Reply
#3

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

Reply
#4

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
Reply
#5

(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'
 
         array('required','other validation', array('customAlpha''customAlpha')); 

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

Reply
#6

Thanks Salain, I'll give that a shot!
Reply
#7

Salain, that seems to be working for me.. you are missing one ')' I think, but thanks much! <reputation props as well> :-)
Reply
#8

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

Reply
#9

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
Reply
#10

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'
       array(
'trim','max_length[50]', array('customAlpha','customAlpha')),
       array(
'customAlpha'=> 'customAlpha validation error message')); 

Can you post your customAlpha function.
A good decision is based on knowledge and not on numbers. - Plato

Reply




Theme © iAndrew 2016 - Forum software by © MyBB