zend form dispatch a validator onblur an element

Is that possible?

I already wrote a simple validator that is triggered when the form is submitted. Can I use the same validator, but it must be triggered right after the input field has been left.

is it possible? pablo

Answers


It would be possible, here is one way to go about it.

  • Add a Javascript onblur event to the form element
  • The onblur call sends an ajax request containing that one field and its value to a ZF action
  • The action calls the validator on that element, or uses Zend_Form::isValidPartial to check the populated element
  • Return a JSON response indicating valid/invalid and optional error message
  • On ajax complete, read the JSON response and update HTML to reflect the result of validation

Hope that helps.


This is part of working example:

Backend:

class UserController extends Zend_Controller_Action
{
    /* ... */
    public function validateAction()
    {
        if ($this->_request->isXmlHttpRequest()) {
            $values = $this->_request->getParam('values');
            $form = new Form_User();
            $isValid = true;

            if (!$form->isValidPartial($values)) {
                $isValid = $form->getMessages();
            }

            $this->_helper->json(array('errors' => $isValid));
        }
    }

    /* ... */
}

Frontend, (just ajax call part) should be attached on event:

$.ajax({
          type: "POST",
          url: "/user/validate/",
          data: {
            'values': $('#my-form-id').serialize()
          },
          dataType: "json",
          beforeSend:function(){

          },
          success: function(response){
            var result = response.errors;

            if (result == true) {
              // given fields are valid
              // do some extra stuff here
            } else {
              // invalid
              // do some extra stuff here
              }
            }
          }
        });

Need Your Help

Issues in creating Queue Managers from Web Sphere 5.3 on Windows7

windows-7 windows-7-x64 ibm-mq

I installed IBM MQ Version 5.3 on 64bits Windows7 computer, then i tried to create a Queue Manager using Web Sphere MQ Explorer. It is always popping up the the below error message when i create qu...

Unit testing compile-time weaving

java spring junit aspectj aspectj-maven-plugin

I am using the aspectj maven plugin to weave Aspects at compile time. When I run the application, the class with the @Advice annotation is being instantiated just before the first time the advice is