Zend表单和注释验证

时间:2014-05-20 08:02:59

标签: php annotations zend-framework2 zend-form

我正在尝试使用annotatnions来构建和验证zend表单。 但是,当我打开showformAction时,我目前发现了一个错误:

“致命错误:未捕获的异常'Zend \ Form \ Exception \ InvalidElementException',并显示消息'在表单中找不到[username]名称的元素...”

以下是我的代码。我做错了什么?

Entity \ User.php

namespace Application\Model;
use Zend\Form\Annotation;

/**
 * @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
 * @Annotation\Name("user")
 */
class User
{
    /**
     * @Annotation\Attributes({"type":"text" })
     * @Annotation\Validator({"type":"Regex","options":{"regex":"/^[a-zA-Z][a-zA-Z0-9_-]{1,19}/"}})
     * @Annotation\Options({"label":"Username:"})
     */
    public $username;

}

控制器\ ProductsController.php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Json\Json;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use Zend\Debug\Debug;
use Application\Entity\Products;
use Application\Entity\Category;
use Application\Form\ProductsForm;
use Doctrine\ORM\EntityManager;
use Application\Model\User;
use Zend\Form\Annotation\AnnotationBuilder;

class ProductsController extends AbstractActionController {

    protected $albumTable;
    protected $em;
    protected $form;

  public function savetodb($data) {
        //code save to db ....
    }

    protected function getForm() {
        $entity = new User();
        $builder = new AnnotationBuilder();
        $this->form = $builder->createForm($entity);

        return $this->form;
    }

    public function showformAction() {
        $viewmodel = new ViewModel();
        $form = $this->getForm();
        $request = $this->getRequest();

        //disable layout if request by Ajax
        $viewmodel->setTerminal($request->isXmlHttpRequest());

        $is_xmlhttprequest = 1;
        if (!$request->isXmlHttpRequest()) {
            //if NOT using Ajax
            $is_xmlhttprequest = 0;
            if ($request->isPost()) {
                $form->setData($request->getPost());
                if ($form->isValid()) {
                    //save to db <span class="wp-smiley emoji emoji-wink" title=";)">;)</span>
                    $this->savetodb($form->getData());
                }
            }
        }

        $viewmodel->setVariables(array(
            'form' => $form,
            // is_xmlhttprequest is needed for check this form is in modal dialog or not
            // in view
            'is_xmlhttprequest' => $is_xmlhttprequest
        ));

        return $viewmodel;
    }

    public function validatepostajaxAction() {
        $form = $this->getForm();
        $request = $this->getRequest();
        $response = $this->getResponse();

        $messages = array();
        if ($request->isPost()) {
            $form->setData($request->getPost());
            if (!$form->isValid()) {
                $errors = $form->getMessages();
                foreach ($errors as $key => $row) {
                    if (!empty($row) && $key != 'submit') {
                        foreach ($row as $keyer => $rower) {
                            //save error(s) per-element that
                            //needed by Javascript
                            $messages[$key][] = $rower;
                        }
                    }
                }
            }

            if (!empty($messages)) {
                $response->setContent(\Zend\Json\Json::encode($messages));
            } else {
                //save to db <span class="wp-smiley emoji emoji-wink" title=";)">;)</span>
                $this->savetodb($form->getData());
                $response->setContent(\Zend\Json\Json::encode(array('success' => 1)));
            }
        }

        return $response;
    }

}

1 个答案:

答案 0 :(得分:1)

您的注释应该是

/**
 * @Annotation\Type("Zend\Form\Element\Text")
 * @Annotation\Validator({"type":"Regex","options":{"regex":"/^[a-zA-Z][a-zA-Z0-9_-]{1,19}/"}})
 * @Annotation\Options({"label":"Username:"})
 */