JS Vaildator
3 : JsvalidHelper in Action
This helper generates JavaScript validation based on the model's validate array, and also offers a few extra features.
These features include:
Server: a Ajax script to validate one field server-side (this was originally designed to make sure a field is unique on the table ie username or email)
Confirm: to check that two or more fields are equal (designed for password fields or to confirm email accounts)
Custom required fields: if you don't want to use strictly the model's validate array or if you want to append a few fields that may not actually appear in the array
Currently the validator uses a JavaScript alert to let user know what changes need to be made, but I am going to make it optional to have the results returned in a div tag of your choice as a unordered list. Until I finish that this will get you started.
Please let me know if this helps anyone, or how I could improve it.
These features include:
Server: a Ajax script to validate one field server-side (this was originally designed to make sure a field is unique on the table ie username or email)
Confirm: to check that two or more fields are equal (designed for password fields or to confirm email accounts)
Custom required fields: if you don't want to use strictly the model's validate array or if you want to append a few fields that may not actually appear in the array
Currently the validator uses a JavaScript alert to let user know what changes need to be made, but I am going to make it optional to have the results returned in a div tag of your choice as a unordered list. Until I finish that this will get you started.
Please let me know if this helps anyone, or how I could improve it.
I am sure you will have more creative uses for the helper, but here is something basic to get you started. The example is a simple add user action.
Here is the Users model app/models/user.php.
The $jsFeedback array contains the strings that the user will see if their information does not validate for that field. Make sure that they keys in the $jsFeedback match the keys in $validate. It is not required to use this array; there is a place on the helper to specify what you want the feedback to be if you don't want to do it in the model.
Here is the controller example. Make sure to have the var $helpers with all the helpers listed here as all are used in this example. This controller is app/controllers/users_controller.php
Now lets look at the add view app/views/users/add.thtml
Ok lets step through this a bit. The first chunk of code makes sure that the javascript helper is enabled, and then links the two libraries need for ajax. This is only necessary if you are going to use the server function. Get the libraries from http://script.aculo.us/ and put them in the app/webroot/js folder.
Next, the style tag pertains to the change class function that I made to change the color of the label of the field that has problems after the validate. If you don't want to change anything, don't put any changes in the errors class. You can call the class whatever you want.
or you can also specify the feedback for that field (bypassing the jsFeedback array) by using a key value array
or mix the two
Ok this is to check that two or more fields are equal. The first parameter the first field that you will be checking the others with. next the array is for fieldnames => feedback to check.
This returns the javascript code block so echo it out wherever you want on the page.
The next bit of code uses some of the jsvalid helpers tidy functions. They combine the HTML, and Form helpers. Most of them make a label, and a input of some sort.
This returns a starting form tag, and it contains the execution of the javascript function.
This is kind of a cool extra, but it doesn't really tie into the rest of the functionality of the helper, so I guess it could be used alone. This makes a label, a text input tag, a button, an empty div tag, and some simple ajax. It makes a function that when the button is pressed it sends the data in the input tag to a url and returns the results into the empty div tag. It was originally designed for users to be able to check if a username is already used. The parameters are field name, label text, and the url where it will be sent.
This returns a label and input pair. In this case it is a password input, but the text input is the same syntax. The first parameter is the field name, the second is the label text.
and that is pretty much it for this view.
If you aren't planning on using the server function you should be go to go, if you are lets keep going.
Lets review the validator action in the users controller.
Since the server function puts the content of this page in the div we need to make sure that it doesn't use a layout, not even the default.
Next this is just some sql to check to see if the email is already in the database. It sets the results of the query to the variable user
app/views/users/validator.thtml
This is just a simple if else to tell the user if the email address was already in the database or not.
Here is the Users model app/models/user.php.
Model Class:
<?php
class User extends AppModel {
var $name = 'User';
var $displayField = 'fname';
var $validate = array(
'fname' => VALID_NOT_EMPTY,
'lname' => VALID_NOT_EMPTY,
'email' => '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/',
'password' => '/^[_a-z0-9-][_a-z0-9-][_a-z0-9-][_a-z0-9-][_a-z0-9-][_a-z0-9-]+$/'
);
var $jsFeedback = array(
'fname' => 'Enter a first name',
'lname' => 'Enter a last name',
'email' => 'Enter a valid email',
'password' => 'Your password must be at least 6 characters'
);
}
?>
The $jsFeedback array contains the strings that the user will see if their information does not validate for that field. Make sure that they keys in the $jsFeedback match the keys in $validate. It is not required to use this array; there is a place on the helper to specify what you want the feedback to be if you don't want to do it in the model.
Here is the controller example. Make sure to have the var $helpers with all the helpers listed here as all are used in this example. This controller is app/controllers/users_controller.php
Controller Class:
<?php
class UsersController extends AppController
{
var $name = 'Users';
var $helpers = array('Html', 'Javascript', 'Ajax','Form','Jsvalid');
function add(){
}
function validator(){
$this->layout = '';
$this->set('user',$this->User->query("SELECT * FROM `users` WHERE `email` = '{$this->data['User']['email']}'"));
}
}
?>
Now lets look at the add view app/views/users/add.thtml
View Template:
<?php
if(isset($javascript))
{
echo $javascript->link('prototype');
echo $javascript->link('scriptaculous.js');
}
?>
<style>
.errors{
color:#F00;
}
</style>
<? $jsvalid->setModel('User'); ?>
<? $jsvalid->required();?>
<? $jsvalid->confirm('User/password',array('User/confirm'=>'The password and confirm do not match'));?>
<? echo $jsvalid->returnScript(); ?>
<? echo $jsvalid->form('/users/validator')?>
<? echo $jsvalid->server('User/email', 'Email', '/users/validator'); ?>
<? echo $jsvalid->password('User/password','Password');?>
<? echo $jsvalid->password('User/confirm','Confrim Password');?>
<? echo $jsvalid->input('User/fname','First Name');?>
<? echo $jsvalid->input('User/lname','Last Name');?>
<? echo $html->submit();?>
</form>
Ok lets step through this a bit. The first chunk of code makes sure that the javascript helper is enabled, and then links the two libraries need for ajax. This is only necessary if you are going to use the server function. Get the libraries from http://script.aculo.us/ and put them in the app/webroot/js folder.
Next, the style tag pertains to the change class function that I made to change the color of the label of the field that has problems after the validate. If you don't want to change anything, don't put any changes in the errors class. You can call the class whatever you want.
$jsvalid->setModel('User');
This is essential. This is how the helper gets the validate array from the model, and the jsFeedback array.
$jsvalid->required();
This is the most useful function. if you don't put in any parameters it will just require all the fields in the validate array but you have the option to put in an array to specify what fields you want to require. here are a couple examples:
$jsvalid->required(array('User/email','User/password');
or you can also specify the feedback for that field (bypassing the jsFeedback array) by using a key value array
$jsvalid->required(array('User/email'=>'Hey smart guy, put in an email','User/password'=>'how can you make a new user without a password');
or mix the two
$jsvalid->required(array('User/email'=>'Hey smart guy, put in an email','User/password');
$jsvalid->confirm('User/password',array('User/confirm'=>'The password and confirm do not match'));
Ok this is to check that two or more fields are equal. The first parameter the first field that you will be checking the others with. next the array is for fieldnames => feedback to check.
echo $jsvalid->returnScript();
This returns the javascript code block so echo it out wherever you want on the page.
The next bit of code uses some of the jsvalid helpers tidy functions. They combine the HTML, and Form helpers. Most of them make a label, and a input of some sort.
echo $jsvalid->form('/users/validator')
This returns a starting form tag, and it contains the execution of the javascript function.
echo $jsvalid->server('User/email', 'Email', '/users/validator');
This is kind of a cool extra, but it doesn't really tie into the rest of the functionality of the helper, so I guess it could be used alone. This makes a label, a text input tag, a button, an empty div tag, and some simple ajax. It makes a function that when the button is pressed it sends the data in the input tag to a url and returns the results into the empty div tag. It was originally designed for users to be able to check if a username is already used. The parameters are field name, label text, and the url where it will be sent.
echo $jsvalid->password('User/password','Password');
This returns a label and input pair. In this case it is a password input, but the text input is the same syntax. The first parameter is the field name, the second is the label text.
and that is pretty much it for this view.
If you aren't planning on using the server function you should be go to go, if you are lets keep going.
Lets review the validator action in the users controller.
function validator(){
$this->layout = '';
$this->set('user',$this->User->query("SELECT * FROM `users` WHERE `email` = '{$this->data['User']['email']}'"));
}
Since the server function puts the content of this page in the div we need to make sure that it doesn't use a layout, not even the default.
Next this is just some sql to check to see if the email is already in the database. It sets the results of the query to the variable user
app/views/users/validator.thtml
View Template:
<? if(empty($user)){
echo "New user";
} else {
echo "This email address is already found in the database";
}
?>
This is just a simple if else to tell the user if the email address was already in the database or not.
Latest Comments