简体   繁体   中英

Validating a Zend Framework form on client-side

I am a reletively newbie to the Zend Framework, I am busy with a simple application which does not require the entire MVC structure of the framework, so I have set my include path to see the Zend Framework Library.

So far I have managed to setup a simple login form. But what I am struggling with now is the form validation does not occur in my form.MY code looks like this:

    $form = new Zend_Form();
    $form->setView(new Zend_View())
->setAction('includes/login.php')
->setMethod('post')
->setAttrib('id', 'login');

$username = new Zend_Form_Element_Text('username');
$username->addValidator(new Zend_Validate_Alnum())
->setLabel('username')
->setRequired(true);

$password = new Zend_Form_Element_Password('password');
$password->addValidator('StringLength', false, array(5))
->setLabel('password')
->setRequired(true);

$login = new Zend_Form_Element_Submit('login');
$login->setLabel('login');

$form->addElement($username)
    ->addElement($password)
->addElement($login);

echo $form->render();

I'm not sure how exactly one would use the validation classes/methods in this instance, does anyone perhaps have a guide and tutorial I could use or maybe point me in the right direction cause I am not sure if what I did is correct ?

ldevstarx

Could you make your question a little more clear, are you saying that your validation is not working on the client side or the server side ?

If you're trying to get it to work on the Server side You will need to use $form->isValid(my_post_Data) in order to validate the form on your controller. But if you're already doing this and you're not being able to get it work then are you getting any error ?

If you already know how to do it on the server side but expecting it to work on client side then it is not enough to get it to validate on the client side. But you could do it easily using simple ajax request without having to write client side validation again. You could consult this . This is very simple and saves you a lot of time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM