validate in "real-time" using ajax. Let's say that form has 4 fields:
login_name, pass, confirm_pass, email.
Validation rules in model User are: login - from 3 to 20 letters, pass
- the same, email - VALID EMAIL.
What I want is to code that kind of form:
When user enters login_name, the ajax->observeField calls action with
validating login_name field. When I enter login_name and go to another
field (or better - without clicking outside the input), ajax validates
that field and puts either "tick" or "cross" image with error message,
if field didn't pass validation. I want to validate each field without
submiting the form.
I tried this way:
form view:
<?php echo $form->create('User',array('url'=>'/users/register'))?>
<?php echo $form->text('login_name',array('id'=>'login_name'));?
>
<?php echo $ajax->observeField('login_name',array('url' => '/
users/verify_field/login_name','update' => 'login-error')); ?><br/>
<span id="login-error"><?php echo $form->error
('login_name','Wrong login');?></span>
<?php if(isset($login_error)):?><span class="error-message"><?
php echo $login_error;?></span><?php endif;?></div>
<div class="clean"></div>
....(the same with other fields, instead "login_name" there is:
"pass","confirm_pass","email").
function verify_field in controller:
function verify_field($field) {
$this->layout = 'blank-nohtml'; //blank html
if(!empty($this->data['User'][$field])) {
$this->User->set($this->data);
if($this->User->validates() && !($field=='login_name' && $this-
>User->find("login_name='".$this->data['User']['login_name']."'"))) {
$img="tick";
} else {
if($field=='login_name' && $this->User->find("login_name='".
$this->data['User']['login_name']."'")) {
$this->set('login_error',ERROR_LOGIN_USED);
}
$this->validateErrors($this->User);
$img="cross";
}
$this->set('img',$img);
}
}
verify_field view:
<?php echo $html->image('icons/'.$img.'.png')?>
<?php echo $form->error('login_name',ERROR_LOGIN);?>
<?php if(isset($login_error)):?><span class="error-message"><?php echo
$login_error;?></span><?php endif;?>
But this is validation for only one field. How to make it better?
And the most important question: when I verify filed like this, I do
get correct image (tick if validation passed, cross if not), but the
$form->error doesn't show anything if the field didn't pass
validation.
For any help I would be very greatful. If you know any ajax form field
validation tutorials - let me know.
Kisses :-)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment