Welcome Guest, Not a member yet? Register   Sign In
Passing form validation errors to an include file
#1

[eluser]opel[/eluser]
I have a newsletter sign up for that I want to include on my site on different pages.

I have created a method "newsletter" in my contact controller like so :

Code:
function newsletter()
    {
        if ($this->input->post('submit'))
        {
            $this->form_validation->set_rules('newsletter_name', 'name', 'trim|required');
            $this->form_validation->set_rules('newsletter_email', 'email', 'trim|required|valid_email');
            
            if ($this->form_validation->run() !== FALSE)
            {
                
                $this->load->library('Mailinteractive');
      
                $custom_fields = array("21" => ($this->input->post('newsletter_name')));
                $mailing_list = 913;
                   $this->mailinteractive->subscribe($this->input->post('newsletter_email'), $mailing_list, $custom_fields);
                
                $this->session->set_flashdata('message', 'Your email has been succsefully added to our mailing list.');
                redirect($this->input->post('newsletter_url'));    
            }
            else
            {
                redirect($this->input->post('newsletter_url'));
            }
        }
    }

If a form is successful it redirects back to the page the form was posted from. The problem I am having is to get the validation error messages to post back to the form.

I have included the validation errors function at the top of my include file so that it appears with the form.

Any ideas on how I can get the validation errors to post back please ?
#2

[eluser]Dam1an[/eluser]
The reason you're not getting the errors displayed is cause you're redirecting, which ends this request and throws away the error data

Normally, you'd just load a view and display the errors there

So you'll either need to store the errors in a session var and manually display them, or load the view instead of a redirect
#3

[eluser]opel[/eluser]
Ok I'll try that cheers
#4

[eluser]opel[/eluser]
I couldn't get it working ended up putting it in the controller.

I have a Base_Controller that all my other front end controllers extend from. I tried putting the above function in there but the form was loading in the wrong place. I even tried assigning it to a variable with no luck.

Any suggestions how you have or would get round the problem ?

Cheers
#5

[eluser]TheFuzzy0ne[/eluser]
Please show your code as it is now.

Usually, your controller method will do something like this:

Code:
$this->load->library('form_validation');

if ($this->input->post('submit'))
{
    # Set the validation rules
    if ($this->form_validation->run())
    {
        redirect('wherever'); # Only redirect if validation passes.
    }
}

# Else show the view by default.
$this->load->view('some_view');

Hope this helps.
#6

[eluser]opel[/eluser]
I am using the "simple template library". So the above works I just couldn't get it in the correct place. I will try again later but deadline approaches Sad




Theme © iAndrew 2016 - Forum software by © MyBB