Reputation: 10412
How do I directly input validation as required into input box on ctp file without going to controller or model in Cakephp?
Upvotes: 0
Views: 3089
Reputation: 547
I would use JQuery, with the Jquery Validaton plugin.
It is rather straight foreward to include and write the scripts for.
In the view (using a Users form view for the example)
<?php
echo $form->create('User', array('id'=>'UserForm'));
echo $form->input('User.name', array('class'=>'required', 'minlength'=>2));
echo $form->input('User.email', array('class'=>'required email'));
echo $form->end('Send');
echo $javascript->codeBlock('$("#UserForm").validate();', array('inline'=>true));
In the layout under the header part
echo $javascript->link('jquery-1.6.2.min.js');
echo $javascript->link('jquery.validate.min.js');
In the user controller
var $helpers = array('Html', 'Form', 'Javascript');
I did testrun the code to confirm it works.
Upvotes: 1