Pregunta

i have a registration form where i am validating the form client side with the following code, the problem is though i enter the same passwords its displaying that the fields do not match. and i am also checking for existting email address which is also not working pls find my code snippet and tell me where i m doing it wrong.

$('#UserSignupForm')
   .validate({
        rules : "data[User][firstname]" : "required",
                "data[User][lastname]" : "required",
                "data[User][email]" : {
                            required : true,
                            email : true,
                            remote : baseUrl + '/checkEmail'
                },  
                "data[User][password]" : {
                            required : true,
                            minlength : 5
                },
                "data[User][confirm_password]" : {
                            required : true,
                            minlength : 5,
                                            //equalTo : "#UserPassword"
                }
                },

                messages : {
                    "data[User][firstname]" : "firstname is missing*",
                    "data[User][lastname]" : "lastname is missing*",
                    "data[User][email]" : {
                        remote : "This email is already taken",
                        email : "Please enter a valid email address"
                    },
                    "data[User][password]" : {
                        required : "Please provide a password",
                        minlength : "missing*"
                    },
                    "data[User][confirm_password]" : {
                        required : "Please provide a password",
                        minlength : "Your password must be at least 5 characters long",
                    }
                },

to check for unique username i m doing the below code in my controller

public function checkEmail(){
   $this->autoRender = false;
   $this->loadModel('UsersEntity');
   $email = $this -> request->data['User']['email'];

   $sql = $this->User->query("Select  username FROM users_entity WHERE username = {$email}");
   if($sql){
       return true;
   }
}
¿Fue útil?

Solución

You have a syntax error... You're missing the opening brace within rules.

$('#UserSignupForm').validate({
    rules : {                     // <-- opening brace was missing
        "data[User][firstname]" : "required",
        "data[User][lastname]" : "required",
        "data[User][email]" : {
            required : true,
            email : true,
            remote : baseUrl + '/checkEmail'
        },  
        "data[User][password]" : {
            required : true,
            minlength : 5
        },
        "data[User][confirm_password]" : {
            required : true,
            minlength : 5,
            //equalTo : "#UserPassword"
        }
    },
    messages : {
        "data[User][firstname]" : "firstname is missing*",
        "data[User][lastname]" : "lastname is missing*",
        "data[User][email]" : {
            remote : "This email is already taken",
            email : "Please enter a valid email address"
        },
        "data[User][password]" : {
            required : "Please provide a password",
            minlength : "missing*"
        },
        "data[User][confirm_password]" : {
            required : "Please provide a password",
            minlength : "Your password must be at least 5 characters long",
        }
    }  // <-- remove trailing comma
});    // <-- closing braces for '.validate()'

Otros consejos

Your sql query should return with error. Because you have to add quote around $email variable.

$sql = $this->User->query("Select  username FROM users_entity WHERE username = {$email}");
to 
$sql = $this->User->query("Select  username FROM users_entity WHERE username = '$email'");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top