Welcome Guest, Not a member yet? Register   Sign In
Captcha helper integration with form_validation?
#1

[eluser]piddleton[/eluser]
I'm just about finished implementing the CI captcha helper on my form. So far, so good, but as I near the end and test validation for not only captcha, but the other input fields, a question came up with regard to validation.

I use a controller to process the form input variables:
Code:
$this->form_validation->set_rules('user', 'Name', 'trim|required');
.
.
if($this->form_validation->run() == FALSE)
{
  $data = $this->contact_model->get_captcha_word();
  $this->load->view('contact',$data);
}

Then if there are validation errors, my view has this code to display the errors above the form:

Code:
echo validation_errors('<p class="error">');

My issue is that the validation for the captcha processing (I used the code sample from the user guide to validate) takes place outside of form_validation->set_rules. So as of now, if there are errors on the 'user' field AND errors on the captcha match, the view only shows the 'user' error message.

I can't seem to get both error messages to appear above the form if there are both errors on the 'user' and captcha fields.

Is the best way to do this to create a custom callback function for the captcha validation? Or could I put the captcha validation process before form_validation->run() executes and somehow append the captcha error to validation_errors?

#2

[eluser]Aken[/eluser]
You could do it either of those ways, though I would recommend creating a callback, or extending the form validation library with a custom rule if you intend on using the captcha and subsequent code on more than one page. Doing one of these methods will automatically integrate any error into your validation_errors() function without any additional work (right now there's no real easy way to toss an error message by itself into the library).
#3

[eluser]piddleton[/eluser]
[quote author="Aken" date="1338354859"]You could do it either of those ways, though I would recommend creating a callback, or extending the form validation library with a custom rule if you intend on using the captcha and subsequent code on more than one page. Doing one of these methods will automatically integrate any error into your validation_errors() function without any additional work (right now there's no real easy way to toss an error message by itself into the library).[/quote]

Ok, thanks. I'm going to try a callback.
#4

[eluser]Reneesh T K[/eluser]
I have the same problem and have used session and callback method for showing the captcha. You can find the functions here

http://myphplibrary.blogspot.in/2012/06/codeigniter-captcha-verification-using.html
#5

[eluser]piddleton[/eluser]
The callback function worked like a charm. I forgot to post an update last week. All set now.




Theme © iAndrew 2016 - Forum software by © MyBB