Welcome Guest, Not a member yet? Register   Sign In
Multiple Forms in the Same Request
#1

[eluser]awells527[/eluser]
Hi all, I'm attempting to process multiple forms in the same request. Basically, sections of the form may succeed while other parts may fail. I'm doing it like this:

Code:
/* changing the email address */

if ($this->input->post('email_confirm') != '')
{
    $this->form_validation->set_rules('email', 'Email', 'trim|required|max_length[128]');
    $this->form_validation->set_rules('email_confirm', 'Email Confirm', 'trim|required|max_length[128]|matches[email]');

    if ($this->form_validation->run() != FALSE)
    {
        /* process code */
    }
    else
    {
        /* show error message, etc */
    }
}

/* changing the password */

if ($this->input->post('password') != '' || $this->input->post('password_confirm') != '')
{
    $this->form_validation->set_rules('password', 'Password', 'trim|required|max_length[128]|sha1');
    $this->form_validation->set_rules('password_confirm', 'Password Confirm', 'trim|required|max_length[128]|matches[password]');

    if ($this->form_validation->run() != FALSE)
    {
        /* process code */
    }
    else
    {
        /* show error message, etc */
    }
}

So there are two different sections where form data is processed. On the form, there are places to enter an email address and a new password. They may fill out both, one, or neither. The problem is that if they mess up the email but validate a new password, the second $this->form_validation->run() fails because it still has the email rules loaded.

So the question is, can I clear out the rules in the same request to form a new validation?
#2

[eluser]Mr. Pickle[/eluser]
First of all, are the fields located in the same form?
The way you describe the situation is that you have separate forms, but I think the case is that all fields are under the same <form> | </form>
#3

[eluser]awells527[/eluser]
Yes, they are all part of the same form.
#4

[eluser]Mr. Pickle[/eluser]
In my opinion you should first have all checks done and depending on the result show the errors (and possibily the same form). If checks show no erros you can run your code (which might contain the same if else to determine which fields are set).

Code:
if ($this->input->post('email_confirm') != '') {
    $this->form_validation->set_rules('email', 'Email', 'trim|required|max_length[128]');
    $this->form_validation->set_rules('email_confirm', 'Email Confirm', 'trim|required|max_length[128]|matches[email]');
}

if ($this->input->post('password') != '' || $this->input->post('password_confirm') != '') {
    $this->form_validation->set_rules('password', 'Password', 'trim|required|max_length[128]|sha1');
    $this->form_validation->set_rules('password_confirm', 'Password Confirm', 'trim|required|max_length[128]|matches[password]');
}

if ($this->form_validation->run() != FALSE) {
    /* process code */
}
else {
    /* show error message, etc */
}




Theme © iAndrew 2016 - Forum software by © MyBB