Houx
Houx

Reputation: 265

Forms in zend framework

I'm new in Zend Framework, and I'm having a problem. usually when verifying data in forms we use javascript like bellow:

<html>
<head>
    <script>
        function verify(){
            if( document.form.name.value=="") { alert("Error");  return false}
        }
    </script>
</head>
<body>

    <form onSubmit="verify()" name="form">
        <input name="name" />
        <input type="submit" />
    </form>
</body>
</html>

I'm using zend forms and i don't have a clue how to do it.

<?php

class Application_Form_Login extends Zend_Form {

    public function init() {
        $login = new Zend_Form_Element_Text('login');

        $password = new Zend_Form_Element_Password('password');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel("Login");

        $remember = new Zend_Form_Element_Checkbox('remember');


        $this->addElements(array($login, $password,$remember ,$submit));
    }

}
?>

Any help please.

thanks for any help.

Upvotes: 2

Views: 1499

Answers (1)

drew010
drew010

Reputation: 69957

You do it the same way as before, but here is how to tell Zend_Form to add the onsubmit event to your so that the HTML code generated contains the attribute in the <form> tag.

class Application_Form_Login extends Zend_Form {

    public function init() {
        $this->setAttrib('onsubmit', 'return verify()');

        // rest of your form code to add elements
    }
}

This adds an attribute onsubmit to the Zend_Form object, such that when the form is rendered your <form> tag has onsubmit="return verify()" in it.

Now you can just put the actual Javascript code to verify the form in the view script itself, or an external javascript file that you can reference in a <script> tag.

Upvotes: 3

Related Questions