CodeIgniter Forums
Form Validation and password change? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Form Validation and password change? (/thread-16713.html)



Form Validation and password change? - El Forum - 03-14-2009

[eluser]Treeda[/eluser]
Hi Folks,

I've liked the new form validation class very much, but with the time i stumble over more and more problems.

I'm facing currently a small problem and curious about if anyone has a good solution.

Imagine a form displaying the users profile which allows to change the password.

so you have the following 3 fields (addiotional to all the other fields of the profile):
Current password
New password
New password (repeat)

how would you setup the rules for that?
I need to tell, the new password should only be set IF the user enteres something into it. As long as the user lets "current password" empty, nothing happens.

Any ideas?


Form Validation and password change? - El Forum - 03-14-2009

[eluser]Edmundas KondraĊĦovas[/eluser]
I'm guessing you need a simple IF statement:

Code:
if(isset($_POST['password']) AND isset($_POST['password2']))
{
    // add validation rules
}

// The rest of the code...



Form Validation and password change? - El Forum - 03-14-2009

[eluser]Treeda[/eluser]
doh....

ok not exactly that but the idea.... :-)

I never thought of the possibility to adding rules depending on the input situation.
I always assumed i set my rules and have to live with it... i don't know if this is the best solution but it offers great possibilities to me .-)

Thx for the hint....

Again a proof that sometimes you don't see the forest due to too many trees.


Form Validation and password change? - El Forum - 03-14-2009

[eluser]jedd[/eluser]
Well, you could also have normal validation rules such as min_length[x].

And you should probably refer to $this->input->post('password1') rather than $_POST directly.


Form Validation and password change? - El Forum - 03-14-2009

[eluser]Treeda[/eluser]
Hi Jedd,

i think with normal rules you can't do that.

Sure i didn't used post directly.

heres a snippet what i currently doing (see the addtional required flag)

Code:
$this->form_validation->set_rules('pass1', 'Neues Passwort', 'trim|min_length[6]|max_length[20]|xss_clean');
            $this->form_validation->set_rules('pass2', 'Neues Passwort (Wiederholung)', 'trim|min_length[6]|max_length[20]|matches[pass1]|xss_clean');

            $this->form_validation->set_rules('email', 'E-Mail', 'trim|valid_email|callback_check_email|xss_clean');
        }

        //there is a special situation!!!
        //if the user enters a new password there has to be some different rules
        if ( $this->input->post("pass1") ){
            $this->form_validation->set_rules('pass', 'Aktuelles Passwort', 'trim|required|min_length[6]|max_length[20]|callback_check_password|xss_clean');
            $this->form_validation->set_rules('pass2', 'Neues Passwort (Wiederholung)', 'trim|required|min_length[6]|max_length[20]|matches[pass1]|xss_clean');
        }