1

我正在尝试在我已经实现的 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 文件

`

类 IndexController 扩展 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-标准形式元素选项][1]

问候 Prashant Sudeep

4

0 回答 0