title: Kohana validation client-side ajax
body: <p>i've been working on a validation script for client-side, that uses built-in Kohana validation, trying to do it in a way that works both client and server sides. So far i made the server side work, but i need some help improving my javascript (My javascript knowlodgement ain't that good) and finish implementing it. (Currently it works for inputs and textareas).</p>
  
  <p>A random controller:</p>
  
  <pre><code>    // ...
      $errors = array();
  
      if ($this-&gt;request-&gt;method() == 'POST')
      {
          // Post to validate/look and get the decoded array
          $validate = Request::factory('validate/look')-&gt;post($this-&gt;request-&gt;post())-&gt;execute()-&gt;body();
          $errors = json_decode($validate, TRUE);
  
          // Empty array, Validation OK
          if ($errors === array())
          {
              // anything u want here
          }
      }
  </code></pre>
  
  <p>Now, the Validation controller (which will be called from any controller, or via ajax):</p>
  
  <pre><code>class Controller_Validate extends Controller {
  
  public function action_look()
  {
      //$user = $this-&gt;user-&gt;loader() ? $this-&gt;user : NULL;
  
      //Validation
      $valid = Validation::factory($this-&gt;request-&gt;post())
          -&gt;rules('name', array(
              array('not_empty'),
              array('min_length', array(':value', 10)),
              array('max_length', array(':value', 80)),
              array('regex', array(':value', '/^[\pL\pN ]++$/uD')),
              array(array($this, 'check_name')),
          ))
          -&gt;rules('description', array(
              array('not_empty'),
          ))
          -&gt;rule('look_tags', array($this, 'check_tags'))
      ;
  
      $valid-&gt;check();
  
      // Only get messages for the posted fields
      $resp = array_intersect_key($valid-&gt;errors('uploadlook'), $this-&gt;request-&gt;post());
  
      $this-&gt;response-&gt;body(json_encode($resp));
  }
  }
  </code></pre>
  
  <p>And this is the javascript:</p>
  
  <pre><code>$(function(){
  $('.validate').find('input,textarea').blur(function(){
      var element = $(this);
      var name = $(this).attr('name');
      var value = $(this).val();
      $.ajax({
          type: 'POST',
          url: '/comunidad/validate/look',
          data: name + '=' + value,
          success: function(e){
              e = JSON.parse(e);
              if(e.length !== 0) {
                  var msg = e[name];
                  var error = '&lt;p&gt;' + msg + '&lt;/p&gt;';
                  if (element.next().length === 0)    element.after(error);
                  else                                element.next().replaceWith(error);
              } else {
                  if (element.next().length) element.next().remove();
              }
          }
      });
  });
  </code></pre>
  
  <p>});</p>
  
  <p>I need some feedback and little help completing the javascript :)</p>