php - Zend form and annotations validation

705

I'm trying using annotatnions to build and validate zend forms. But currently I recive an error when I open showformAction:

"Fatal error: Uncaught exception 'Zend\Form\Exception\InvalidElementException' with message 'No element by the name of [username] found in form' ..."

So below is my code. What I doing wrong ?

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;

}

Controller\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 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 title=";)">;)</span>
        $this->savetodb($form->getData());
        $response->setContent(\Zend\Json\Json::encode(array('success' => 1)));
      }
    }

    return $response;
  }

}
981

Answer

Solution:

Your annotation should be

/**
 * @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:"})
 */

People are also looking for solutions to the problem: php - Magento - Check if item has specific custom option or not in shopping cart

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.