11.16 php saved
ovihc
Tags add more
ajax form validation  
Note
validateField function for validating single fields from a form (using observeField) and passing along the message to the view.
  1. // view - first, view sends the input to the controller.
  2. <?php echo $ajax->observeField('postcode', array('update'=>'postcode_error',
  3.                                'url'=>"/wolistings/validateField/postcode",
  4.                      'frequency'=>1)); ?>
  5.  
  6.  
  7.  
  8.  
  9.  
  10. // controller - second, it gets the field contents, sends it to validate, and receives a message.
  11. function validateField($field = null) {
  12.       
  13.         if ($this->RequestHandler->isAjax()) {
  14.            
  15.            $this->Wolisting->set($this->data['Wolisting'][$field]);
  16.             if ($this->Wolisting->create($this->data) && $this->Wolisting->validates()) {   
  17.                 $this->set('message', null);
  18.                 $this->render('validateField', 'ajax');
  19.             }
  20.             else {
  21.                 $errorMessages = $this->validateErrors($this->Wolisting);
  22.                 foreach ($errorMessages as $val) {
  23.                     $errorMessage = $val;
  24.                 }
  25.                 $this->set('message', $errorMessage);
  26.                 $this->render('validateField', 'ajax');
  27.             }
  28.            
  29.         }
  30.     }
  31.  
  32.  
  33.  
  34.  
  35.  
  36. // validate_field.ctp view - third, if a message is null, then it pasts validation, otherwise it failed and now i have the message.
  37.  
  38.  
  39. <?php
  40. if(!is_null($message)){
  41.        echo '<img src="/img/cross.gif">';
  42.      print_r($message);
  43. }
  44. else echo '<img src="/img/accept.gif">';
  45.         
  46. ?>
Parsed in 0.063 seconds, using GeSHi 1.0.7.14

Modify this Paste