[jQuery] help: Zend Framework and JQuery validation plugin
Hi!
I try to use Zend Framework with JQuery validation plugin. Standard
validation of forms works good but when I want to remote validate (f.e
check a login in database) remote function dosen't work. Below I write
a code with method from controller and views listing:
Method from controller:
function checkloginAction(){
$request = trim(strtolower($_REQUEST['login'])); //take data
from global REQUEST array
$users = new Users(); //make a new class with users
$select = $users->select(); //select users where
the login
$select->where('login = ?', $request); //is the same like from
REQUEST array
$login = $users->fetchAll($select);
if ( $login->login == '' ) { //if login is empty
$valid = 'true'; //make valid true
}else{ //else
$valid = 'false'; //make valid false
}
$this->view->valid = $valid; //pass the valid to view
}
Checklogin view (checklogin.phtml):
<?php
echo $this->valid; //show result of valid
?>
Registration form view:
...
<script type="text/javascript" src="<?php echo $this->baseUrl;?>/
public/scripts/jquery.js"></script>
<script type="text/javascript" src="<?php echo $this->baseUrl;?>/
public/scripts/jquery.validate.js"></script>
...
<script type="text/javascript">
$(document).ready(function() {
// validate signup form on keyup and submit
var validator = $("#signupform").validate({
rules: {
login: {
required: true,
minlength: 2,
remote: "checklogin" <-- dosen't work
},
pass: {
required: true,
minlength: 5
},
pass_confirm: {
required: true,
minlength: 5,
equalTo: "#pass"
}
},
messages: {
login: {
required: "Enter login",
minlength: jQuery.format("Enter at least {0} characters"),
remote: jQuery.format("{0} is already in use")
},
pass: {
required: "Enter password",
minlength: jQuery.format("Enter at least {0} characters")
},
pass_confirm: {
required: "Repeat password",
minlength: jQuery.format("Enter at least {0} characters"),
equalTo: "Powtórzone hasło jest nieprawidłowe"
}
},
// set this class to error-labels to indicate valid fields
success: function(label) {
// set as text for IE
label.html(" ").addClass("checked");
}
});
});
</script>
...
<form action="" id="signupform" autocomplete="off" method="post">
<label for="login">Login*:</label><br />
<input id="login" type="text" name="login" /><span
class="status"></span><br />
<label for="pass">Password*:</label><br />
<input id="pass" type="password" name="pass" /><span
class="status"></span><br />
<label for="pass">Confirm password*:</label><br />
<input id="pass_confirm" type="password" name="pass_confirm" /