Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 2.1.3 extension: Get form validation errors and field data after a redirect.
#1

[eluser]Harold Villacorte[/eluser]
A common problem is getting form validation error data after a redirect. If all you need are the error messages you can just set them to flashdata but if you need the field data as well you can try this extension to the Form Validation class. If you feel this functionality should be in the core please post.
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Extends the Codeigniter Form Validation class.
*/
class MY_Form_validation extends CI_Form_validation
{

    /**
     * Extends the Form Validation constructor.
     */
    function __construct()
    {
        parent::__construct();

        // Check if the session class is loaded.
        if (!method_exists($this->CI, 'userdata'))
        {
            $this->CI->load->library('session');
        }

        // Get the _field_data from the session.
        if ($this->CI->session->userdata('validation_errors'))
        {
            // Get the array validation_errors from the session.
            $validation_errors = $this->CI->session->userdata('validation_errors');

            // Set the field data from session.
            $this->_field_data = $validation_errors['_field_data'];

            // Set the error data from session.
            $this->_error_array = $validation_errors['_error_array'];

            // Unset validation errors array in sesssion.
            $this->CI->session->unset_userdata('validation_errors');
        }
    }

    /**
     * Use to save form validation errors and field data before a redirect.
     *
     * @param string $url Url to redirect user to.
     */
    public function redirect($url = NULL)
    {
        if (validation_errors())
        {
            // Set the userdata array.
            $array = array(
                'validation_errors' => array(
                    '_field_data'  => $this->_field_data,
                    '_error_array' => $this->_error_array,
                ),
            );

            // Save form valdation properties to the session.
            $this->CI->session->set_userdata($array);
        }

        // Check if Url helper is loaded.
        if (!function_exists('redirect'))
        {
            $this->CI->load->helper('url');
        }

        // Redirect the user.  Current url is default if the url parameter is NULL.
        $redirect = ($url) ? $url : current_url();
        redirect($redirect);
    }

}

/* End of file MY_Form_validation.php */
/* Location: ./application/libraries/MY_Form_validation.php */
To redirect to the current url:
Code:
if ($this->form_validation->run() == FALSE)
{
    $this->form_validation->redirect();
}
To redirect to a specified url:
Code:
if ($this->form_validation->run() == FALSE)
{
    $url = 'define_url_here';
    $this->form_validation->redirect($url);
}
#2

[eluser]TheFuzzy0ne[/eluser]
Sorry if I sound a bit dense here, but why would you need to redirect if validation failed?

I've had absolutely no trouble working with the library as-is, and I redirect after validation passes.

When a form is submitted, I validate the $_POST data that's been submitted. It makes no sense to me to redirect, thus unsetting the $_POST array, only to perform validation somewhere else. Surely that would be like taking your car to a car wash, but throwing everything into a bucket and taking it home with you to wash your car at home.

I'd be interested in seeing an example where this would be necessary.
#3

[eluser]Harold Villacorte[/eluser]
[quote author="TheFuzzy0ne" date="1363746856"]Sorry if I sound a bit dense here, but why would you need to redirect if validation failed?[/quote]

99.9% of the time the library will do as is, just load the views again as normal. The problem I have run into is when you have a form that needs a parameter passed to it, you basically have to redirect to that form with the parameter in the url.

[quote author="TheFuzzy0ne" date="1363746856"]When a form is submitted, I validate the $_POST data that’s been submitted. It makes no sense to me to redirect, thus unsetting the $_POST array, only to perform validation somewhere else. [/quote]

The point here is that this redirect method will repopulate the $_field_data array in addition to the error messages and all your set_value()'s and set_checkbox()'s or what have you will run and the form data will get repopulated and all the form error data will be there because the arrays are set.
#4

[eluser]TheFuzzy0ne[/eluser]
[quote author="Harold Villacorte" date="1363748321"]The problem I have run into is when you have a form that needs a parameter passed to it, you basically have to redirect to that form with the parameter in the url.
[/quote]

Can you elaborate on that? What kind of parameter are we talking about? Personally, I've never come across this problem.

I can't help feeling that your idea is flawed. Let's say I have a blog, and I write a very lengthy blog entry (we'll say 10K, which is certainly not impossible). So I submit the form to the server, and upload 10K, which you then put into a cookie, and send back to me, which I then send back to the server again? And on top of that, chances are you've just broken my cookie, because most cookies only hold about 4KB of data.
#5

[eluser]Harold Villacorte[/eluser]
Quote:Let’s say I have a blog, and I write a very lengthy blog entry (we’ll say 10K, which is certainly not impossible). So I submit the form to the server, and upload 10K, which you then put into a cookie, and send back to me, which I then send back to the server again? And on top of that, chances are you’ve just broken my cookie, because most cookies only hold about 4KB of data.

That is definitely something to consider before using this redirect method. If the form has a textarea that can potentially exceed 4kb then this redirect method should not be used. That issue is addressed in the documentation explaining the Session class.

Where I myself have absolutely needed this is on the administrator form for a navigation system. It needs the menu id then gets and displays the menu data. Then takes the menu id and gets each link associated with that menu and generates a form for each link in a table format so you have the menu data and a link edit form for each link all in one interface. Complicated but I got working with full form validation and error handling for each individual link form on a single page. Why this method was needed is that after form validation it needs the parameter to generate the interface for that particular menu id.
#6

[eluser]Aken[/eluser]
If you use routes, your URI parameters can be dynamic, but all POST data can end up to the same controller method. Still seems unnecessary.
#7

[eluser]Harold Villacorte[/eluser]
[quote author="Aken" date="1363758452"]If you use routes, your URI parameters can be dynamic, but all POST data can end up to the same controller method. Still seems unnecessary.[/quote]

The thing is that the issue is not routing it is the "action" attribute in the <form> open tag. That is where the form gets submitted to and that is where the validation code needs to be. But I whole heartedly agree with you both that redirecting is not a great solution but I wrote this extension because it seemed like the only solution.

But you both got me scratching my head wondering how it is that this is not necessary and I just realized that I have been overlooking the simplest solution there is - just include the parameter in the from open tag:

Code:
echo form_open('controller/method/' . $parameter);

So I just updated my code and the module is working now without the redirection so as of now I am not using this method although there still might be some situation where it might be needed, maybe.

So I put up a poll to get some feedback and if there is not enough interest I will not bother the Reactor team with it.




Theme © iAndrew 2016 - Forum software by © MyBB