CodeIgniter Forums

Full Version: CI-Validation + Recaptcha
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]awpti[/eluser]
Have any of you implimented validation+Recaptcha?

I can't even fathom how to go about it without an absolute, horrid kludge of code.

I can't figure where in this process the recaptcha needs to be checked and when it does, how it get it to gracefully fail out the validation.

I know that recaptcha has 2 POST variables but I still don't see how I'd force validation to fail if they fail the captcha check.

Mainly, I'm just looking for a little help getting these two working together. I've worked very little with form validation and not at all before with captcha methods.

Code:
function index()
        {
                $rules = array
                (
                        'location'      => 'required|xss_clean',
                        'company'       => 'xss_clean',
                        'email'         => 'required|valid_email',
                        'job_title'     => 'required|xss_clean',
                        'description'   => 'required|xss_clean',
                        'to_apply'      => 'required|prep_url|xss_clean',
                        'website'       => 'required|prep_url|xss_clean'
                );

                $fields = array
                (
                        'location'      => 'Location',
                        'company'       => 'Company',
                        'email'         => 'E-Mail',
                        'job_title'     => 'Job Title',
                        'description'   => 'Description',
                        'to_apply'      => 'How to Apply',
                        'website'       => 'Website'
                );

                $this->validation->set_rules($rules);
                $this->validation->set_fields($fields);

                if ($this->validation->run() == TRUE)
                {
                        $this->view->load('add_job');
                }
                else
                {
                        $data = array
                        (
                                'location'      => $this->input->post('location'),
                                'company'       => $this->input->post('company'),
                                'email'         => $this->input->post('email'),
                                'job_title'     => $this->input->post('job_title'),
                                'description'   => $this->input->post('description'),
                                'to_apply'      => $this->input->post('to_apply'),
                                'website'       => $this->input->post('website'),
                                'is_validated'  => 0,
                                'date_created'  => date('Y-m-d H:i:s'),
                                'edit_key'      => md5(date('Y-m-d H:i:s').$this->input->post('company'))
                        );
                        if($this->addjob->add_job($data) === FALSE)
                        {
                                echo 'An error has occured. You should not see this. Sorry.';
                        }
                        else
                        {
                                redirect('/notices/post_accepted', 'header');
                        }
                }
        }

El Forum

[eluser]awpti[/eluser]
Wow, was this a tough one to wrap my head around, but I finally got the logic.

Code posted below, so if anyone else ever runs into validation+recaptcha!

Code:
function index()
        {
                $rules = array
                (
                        'location'      => 'required|xss_clean',
                        'company'       => 'xss_clean',
                        'email'         => 'required|valid_email',
                        'job_title'     => 'required|xss_clean',
                        'description'   => 'required|xss_clean',
                        'to_apply'      => 'required|prep_url|xss_clean',
                        'website'       => 'required|prep_url|xss_clean'
                );

                $fields = array
                (
                        'location'      => 'Location',
                        'company'       => 'Company',
                        'email'         => 'E-Mail',
                        'job_title'     => 'Job Title',
                        'description'   => 'Description',
                        'to_apply'      => 'How to Apply',
                        'website'       => 'Website'
                );
                $this->view->set('error', FALSE);
                $this->validation->set_rules($rules);
                $this->validation->set_fields($fields);

                if ($this->validation->run() == FALSE)
                {
                        $this->view->load('add_job');
                }
                else
                {

                        if ($this->input->post('recaptcha_response_field'))
                        {
                                $resp = recaptcha_check_answer
                                (
                                        '************ PRIVATE KEY **************',
                                        $_SERVER["REMOTE_ADDR"],
                                        $this->input->post('recaptcha_challenge_field'),
                                        $this->input->post('recaptcha_response_field')
                                );

                                if ($resp->is_valid == FALSE) {
                                        $this->view->set('error', $resp->error);
                                        $this->view->load('add_job');
                                } else {
                                        $data = array
                                        (
                                                'location'      => $this->input->post('location'),
                                                'company'       => $this->input->post('company'),
                                                'email'         => $this->input->post('email'),
                                                'job_title'     => $this->input->post('job_title'),
                                                'description'   => $this->input->post('description'),
                                                'to_apply'      => $this->input->post('to_apply'),
                                                'website'       => $this->input->post('website'),
                                                'is_validated'  => 0,
                                                'date_created'  => date('Y-m-d H:i:s'),
                                                'edit_key'      => md5(date('Y-m-d H:i:s').$this->input->post('company'))
                                        );
                                        if($this->addjob->add_job($data) === FALSE)
                                        {
                                                echo 'An error has occured. You should not see this. Sorry.';
                                        }
                                        else
                                        {
                                                redirect('/notices/accepted', 'header');
                                        }
                                }
                        }
                        else
                        {
                                $this->view->load('add_job');
                        }
                }
        }

El Forum

[eluser]plainas[/eluser]
I'll give this a try.

awpti, did you use the library in the wiki:
http://codeigniter.com/wiki/ReCAPTCHA/
?

I don't understand what the recaptcha view is for. Isn't all the recaptcha HTML denerated by recaptcha_get_html()?

El Forum

[eluser]awpti[/eluser]
Nope, just pulled the code from recaptcha and turned it into a helper.

Worked great.

El Forum

[eluser]plainas[/eluser]
Thank you, I created a helper, and followed the instructions in recaptcha website, it worked like a charm.
I had to come up with a workaround to display the error message when the recaptcha doesn't match. Is there anyway to set the validation error message without assigning it to an specific validation field/rule?