0

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

user968865
  • 97
  • 2
  • 17

2 Answers2

1

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.

drew010
  • 68,777
  • 11
  • 134
  • 162
1

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
              }
            }
          }
        });
b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57