Skip to content
barbir edited this page Sep 14, 2010 · 4 revisions

Installation

First you need to install the plugin like so:

  • Download the CakePHP JQValidator plugin
  • Unpack the contents of the archive to the plugins folder of the CakePHP project and rename the first level folder in the archive (eg: “barbir-cakephp-jq-validator-xxxxxxx”) to “j_q_validator”, and you will have a structure like this “myproject/plugins/j_q_validator/”.
  • It’s installed… Contents of the “myproject/plugins/j_q_validator/” folder should look like this:
    • controllers
    • views
    • j_q_validator_app_controller.php
    • j_q_validator_app_model.php
    • README

Usage

When you install the plugin, you are ready to use it. In order to use the CakePHP JQValidator plugin, you need to:

  • include the JQValidator helper and component
  • initialize the validation in your controller
  • render the validation script in your view where you want to use the client side validation to appear

Include the JQValidator helper and component

Include the JQValidator component and helper in your controller, by adding ‘JQValidator.JQValidator’ to both your $components and $helpers arrays.

var $components = array(... 'JQValidator.JQValidator');
var $helpers = array(... 'JQValidator.JQValidator');

Initialize the validation

In the controller action (in which you wish to use the validation), type the code like in the following example:

$this->JQValidator->addValidation
(
	'MyModel',
	$this->MyModel->validate,
	__('Save failed, fix the following errors:', true),
	'MyModelAddForm'
);

Parameter description for addValidation function:

  • $modelName – name of the model that contains the fields to validate
  • $validationOptions – validation array of the model to validate
  • $errorMessageTitle – (not mandatory) – a title of the message that will be displayed if the validation fails.
  • $formId – (not mandatory) id of the form to validate, if it is not sent, all fields that are defined in the $validationOptions for the model with the $modelName will be validated

Render the validation script

In your view you should add the following line to render validation scripts for validating all defined models:

<?php echo $jQValidator->validator(); ?>

Clone this wiki locally