Welcome Guest, Not a member yet? Register   Sign In
New form called on submit leads to form_validation errors showing
#1

[eluser]neomech[/eluser]
I'm working on my user registration system (using Ion Auth). Everything is going pretty smoothly so far but I've run into a slightly annoying problem.

I have a user profile page that shows the information about the user. At the bottom of the page is a single line form that only contains one input, a submit button that takes the person to a "change password" page. I wanted to use a form submit button instead of a link for stylistic reasons, and didn't want to use the <button> class with onclick in case javascript was disabled.

However, when I get to the change password page, it has the form validation errors showing up at the top as if the form had already been submitted with empty fields. So the form shows up but it has "Old password field required", "New password field required", etc. even though the page has just loaded and has not been submitted yet.

This does not happen if the profile page uses a link to the change password page instead of a submit button. I surmise that because I'm going from one form to another form, the form_validation rules are being invoked for some reason.

I should note that the profile page and change password page are being handled by the same controller (although using different functions).

Does my problem make sense, and is there a solution that would allow me to continue to use a form submit button to get from one page to the next without making all the errors show up?

Thanks!
#2

[eluser]neomech[/eluser]
So since no one has replied I thought I'd better post some code! Wink

This is the bit that calls the "change password" page from my user's profile page:

Code:
&lt;?php echo form_open("profile/change_password");?&gt;
        <p>&lt;input type="hidden" name="referred" value="yes"&gt;&lt;?php echo form_submit('submit', 'Change Password');?&gt;</p>
        &lt;?php echo form_close();?&gt;

I added in the hidden form field in order to avoid the problem described above. Here's the relevant code from the "change_password" function:

Code:
if ($this->form_validation->run() == false) { //display the form
            //set the flash data error message if there is one
                if ($this->input->post('referred') <> 'yes'){ //had to add this if/then statement to prevent error messages showing up when form first loads
                    $data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
                }
                else{
                    $data['message'] = '';
                }

            $data['old_password']           = array('name'    => 'old',
                                                             'id'      => 'old',
                                                            'type'    => 'password',
                                                                  'value'   => $this->form_validation->set_value('old_password'),
                                                         );
            $data['new_password']           = array('name'    => 'new',
                                                             'id'      => 'new',
                                                            'type'    => 'password',
                                                         );
            $data['new_password_confirm']   = array('name'    => 'new_confirm',
                                                            'id'      => 'new_confirm',
                                                            'type'    => 'password',
                                                         );
            $data['user_id']                = array('name'    => 'user_id',
                                                            'id'      => 'user_id',
                                                            'type'    => 'hidden',
                                  'value'   => $user->user_id,
                                                         );

            //render
            $this->view('profile/change_password', $data);

This seems inelegant and I feel like I'm missing something obvious. Is there an easier way to avoid this problem, where the form_validation is running the first time I load the page? I realize this is happening because I'm calling the form from a form on another page, or at least, I think that's why!
#3

[eluser]bretticus[/eluser]
Code:
if ($this->form_validation->run() == false) { //display the form

That line will always look for POST data and run validation. You could always look for a specific post variable before you run validation however.

Code:
if ($this->input->post('old_password') !== FALSE) {
  if ($this->form_validation->run() == false) { //display the form
    //...
  }
}

[quote author="neomech" date="1285319095"]At the bottom of the page is a single line form that only contains one input, a submit button that takes the person to a "change password" page. I wanted to use a form submit button instead of a link for stylistic reasons, and didn't want to use the <button> class with onclick in case javascript was disabled.[/quote]

I agree with the javascript statement, but you can easily style a link to look exactly like your buttons with css and this is pretty much the standard way this type of thing is done these days. In fact, using a form and sending a POST verb to the server for the sake of layout is ill-advised. I'd use what the HTML gods gave us: anchor tag.
#4

[eluser]neomech[/eluser]
Thanks a lot for the advice Smile

That got me into a whole realm of different possibilities with css, button elements, buttons that are styled to look like links, etc. The "sliding door" technique for making links into buttons is very neat!




Theme © iAndrew 2016 - Forum software by © MyBB