如何验证Zend_Form?

时间:2012-09-03 07:31:42

标签: jquery zend-framework validation zend-form

首先,我尝试在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,您可能会在我的代码中找到我已经有一些用于验证的参数,这不是我想要的,我想要的前端使用一些jquery插件进行实时输入验证。

1 个答案:

答案 0 :(得分:1)

要添加jQuery验证,您可以添加一些装饰器来形成,例如:

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

然后在View.phtml示例中执行jQuery验证:

$('#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"
                }
            }
        });

我希望这可以帮到你。