2

首先,我尝试在 stackoverflow 上搜索类似的主题。我知道一些类似的帖子被关闭了,因为已经有回复的帖子,我真的觉得它没有帮助(这里:zend form validation,也许我不够聪明),所以请管理员,允许这个帖子呆一会儿>**

就我而言,首先我在控制器中声明表单:

        $this->_helper->layout->disableLayout();
    $form = new Zend_Form();
    $form->setAction('/user/login')
         ->setMethod('post');

    // Create and configure username element:
    $username = $form->createElement('text', 'username');
    $username->addValidator('alnum')
             ->addValidator('regex', false, array('/^[a-z]+/'))
             ->addValidator('stringLength', false, array(6, 20))
             ->setRequired(true)
             ->addFilter('StringToLower');

    // Create and configure password element:
    $password = $form->createElement('password', 'password');
    $password->addValidator('StringLength', false, array(6))
             ->setRequired(true);

    // Add elements to form:
    $form->addElement($username)
         ->addElement($password)
         // use addElement() as a factory to create 'Login' button:
         ->addElement('submit', 'login', array('label' => 'Login'));

然后我在我的视图文件中输出:

<?php echo  $this->testForm;?> 

我们发现了一些非常酷的用于表单验证器的 Jquery 插件,但我看不出它是如何与 Zend Form 中的代码集成的。我尝试引用 Zendx_Jquery,似乎它甚至不包含表单验证选项。

任何人都可以撕碎灯吗?谢谢。

PS, you might find in my code I already I have some params for validations, that's not what I want, all I want for front-end using some jquery plugin for real-time input validation.

4

1 回答 1

2

To add jQuery validation you can add some decorators to form, example :

    $form->setName('postForm');
    $form->setMethod('post');
    $form->addDecorator('HtmlTag', array('tag' => 'div', 'class' => 'my-lovely-form'));
    $form->setAttrib('enctype', 'multipart/form-data');

Then you do jQuery validation in View.phtml example:

$('#postForm').validate({
            submitHandler: function(form) {
                $("#submit").attr('disabled', 'disabled');
                form.submit();
            },
            onchange: true,         
            errorLabelContainer: $('.error-container'),
            wrapper: "p",
            rules: {
                name: {
                    required: true,
                    minlength: <?php echo $this->validationConfig->form->name->minlength; ?>,
                    maxlength: <?php echo $this->validationConfig->form->name->maxlength; ?>
                },
                title: {
                    required: true,
                    minlength: <?php echo $this->validationConfig->form->title->minlength; ?>,
                    maxlength: <?php echo $this->validationConfig->form->title->maxlength; ?>
                }
            },
            messages: {
                name: {
                    required: "Full name is required",
                    minlength: "Full name has to be at least <?php echo $this->validationConfig->form->name->minlength; ?> characters long",
                    maxlength: "Full name has to be less than <?php echo $this->validationConfig->form->name->maxlength; ?> characters long"
                },
                title: {
                    required: "Title is required",
                    minlength: "Title has to be at least <?php echo $this->validationConfig->form->title->minlength; ?> characters long",
                    maxlength: "Title has to be less than <?php echo $this->validationConfig->form->title->maxlength; ?> characters long"
                }
            }
        });

I hope this can help you.

于 2012-09-03T07:54:18.663 回答