使用XML将验证添加到Zend表单

时间:2012-10-18 05:25:27

标签: xml zend-form

我正在尝试使用我已经实现的Zend Framework中的XML构建表单。 我还添加了验证,但如果验证失败,我无法显示错误。  这是xml文件。

`

<?xml version="1.0" encoding="UTF-8"?>
<form>
<user>
    <login>
        <name>signup</name>
        <class>test</class>
        <method>post</method>

        <elements>
            <username>
                <type>text</type>
                <options>
                    <label>Username:</label>
                    <validators>
                        <strlen>
                            enter code here<validator>StringLength</validator>
                            <options>
                                <min>4</min>
                                <max>20</max>
                            </options>
                        </strlen>
                    </validators>
                    <required>true</required>
                </options>
            </username>

            <password>
                <type>password</type>
                <options>
                    <label>Password:</label>
                    <validators>
                        <regex>
                            <validator>regex</validator>
                            <options>
                                <pattern>/^[a-z]/i</pattern>
                            </options>
                        </regex>
                        <strlen>
                            <validator>StringLength</validator>
                            <options>
                                <min>3</min>
                            </options>
                        </strlen>
                    </validators>
                    <required>true</required>
                </options>
            </password>

            <submit>
                <type>submit</type>
                <options>
                    <value>Submit</value>
                </options>
            </submit>
        </elements>
    </login>
</user>

`

IndexController.php文件

`

class IndexController extends Zend_Controller_Action {

function indexAction() { $this->view->baseUrl = $this->_request->getBaseUrl(); $this->view->pageTitle = "Zend_Form Login"; Zend_Loader::loadClass("Zend_Config_Xml"); $form = new Zend_Config_Xml('../application/forms/login.xml'); $form = new Zend_Form($form->user->login); $form->setDecorators(array( 'FormElements', 'Form' )); $form->setElementDecorators(array( 'ViewHelper', 'Label' )); foreach($form->getElements() as $element) { $element->addDecorators(array( array('Errors', ''), array('HtmlTag', ''), array('Label', ''), )); } if ($this->getRequest()->isPost()) { if ($form->isValid($_POST)) { $formData = $form->getValues(); $username = $form->getValue('username'); $password = $form->getValue('password'); if ($username == "admin" && $password == "root") { $this->_redirect("index/complete"); } else { //Booh! } } else { $form->populate($formData); } } $this->view->form = $form; } function completeAction(){}

}`

如果验证失败,请帮助显示错误 [http://webcache.googleusercontent.com/search?q=cache:http://www.maltblue.com/php/zend-form-mastery-with-zend-config-part-3-standard-form-element -options] [1]

此致 Prashant Sudeep

0 个答案:

没有答案