簡體   English   中英

帶有注釋字段集的ZF2表單未通過驗證

[英]ZF2 Form with annotation fieldset not validating

好的,可能是我所缺少的東西,但是我的字段集無法通過驗證。 這就是我目前所擁有的。

我的實體:

用戶:

<?php
namespace User\Model\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

use Zend\Form\Annotation as ZFA;

/**
 * @ORM\Entity
 * @ORM\Table(name="Users")
 *
 * @ZFA\Name("user")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @ORM\OrderBy({"name" = "ASC"})
     * @ZFA\Exclude()
     * @var int
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     * @ZFA\Filter({"name":"StringTrim"})
     * @ZFA\Required(true)
     * @ZFA\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
     * @ZFA\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9._-]{0,24}$/"}})
     * @ZFA\Attributes({"type":"text"})
     * @ZFA\Options({"label":"Username:"})
     * @var string
     */
    private $username;

    /**
     * @ORM\Column(type="string")
     * @ZFA\Filter({"name":"StringTrim"})
     * @ZFA\Required(true)
     * @ZFA\Validator({"name":"StringLength", "options":{"min":8}})
     * @ZFA\Attributes({"type":"text"})
     * @ZFA\Options({"label":"Password:"})
     * @var string
     */
    private $password;

    /**
     * @ORM\ManyToMany(targetEntity="Group", inversedBy="users")
     * @ORM\JoinTable(name="User_users_groups")
     **/
    private $groups;

    /**
     * @ORM\OneToMany(targetEntity="Profile",cascade={"persist", "remove"},  mappedBy="user")
     */
    private $profile;

    public function __construct() {
        $this->groups   = new ArrayCollection();
        $this->profile  = new ArrayCollection();
    }

    public function addProfile(Collection $profiles)
    {
        foreach ($profiles as $profile) {
            $profile->setUser($this);
            $this->profile->add($profile);
        }
    }

    public function removeProfile(Collection $profiles)
    {
        foreach ($profiles as $profile) {
            $profile->setUser(null);
            $this->profile->removeElement($profile);
        }
    }

    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setUsername($username)
    {
        $this->username = $username;
        return $this;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function setPassword($password)
    {
        $this->password = $password;
        return $this;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setProfile(Profile $profile) {
        $this->profile = $profile;
        $profile->setUser($this);
    }

    public function getProfile()
    {
        return $this->profile;
    }

}

輪廓:

<?php
namespace User\Model\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\Form\Annotation as ZFA;

/**
 * @ORM\Entity
 * @ORM\Table(name="Profiles")
 * @ZFA\Name("profile")
 * @ZFA\Type("fieldset")
 */
class Profile
{
    /**
     *
     * @var int $id
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @ZFA\Exclude()
     */
    private $id;

    /**
     *
     * @ORM\ManyToOne(targetEntity="User", inversedBy="profile")
     * @ZFA\Exclude()
     */
    private $user;

    /**
     *
     * @ORM\Column(type="string")
     * @ZFA\Required(true)
     * @ZFA\Filter({"name":"StringTrim"})
     * @ZFA\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
     * @ZFA\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9._-]{0,24}$/"}})
     * @ZFA\Attributes({"type":"text"})
     * @ZFA\Options({"label":"Firstname:"})
     * @var string
     */
    private $givenName;

    /**
     *
     * @ORM\Column(type="string")
     * @ZFA\Required(true)
     * @ZFA\Filter({"name":"StringTrim"})
     * @ZFA\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
     * @ZFA\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9._-]{0,24}$/"}})
     * @ZFA\Attributes({"type":"text"})
     * @ZFA\Options({"label":"Surname:"})
     * @var string
     */
    private $surName;

    /**
     * @ORM\Column(type="string")
     * @ZFA\Required(true)
     * @ZFA\Filter({"name":"StringTrim"})
     * @ZFA\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
     * @ZFA\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9._-]{0,24}$/"}})
     * @ZFA\Attributes({"type":"text"})
     * @ZFA\Options({"label":"Initials:"})
     */
    private $initial;

    /**
     * @ORM\Column(type="string")
     * @ZFA\Required(true)
     * @ZFA\Filter({"name":"StringTrim"})
     * @ZFA\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
     * @ZFA\Validator({"name":"Regex", "options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9._-]{0,24}$/"}})
     * @ZFA\Attributes({"type":"text"})
     * @ZFA\Options({"label":"Display name:"})
     */
    private $displayName;

    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setUser($user)
    {
        $this->user = $user;
        return $this;
    }

    public function getUser()
    {
        return $this->user;
    }

    public function setGivenName($givenName)
    {
        $this->givenName = $givenName;
        return $this;
    }

    public function getGivenName()
    {
        return $this->givenName;
    }

    public function setSurName($surName)
    {
        $this->surName = $surName;
        return $this;
    }

    public function getSurName()
    {
        return $this->surName;
    }

    public function setInitial($initial)
    {
        $this->initial = $initial;
        return $this;
    }

    public function getInitial()
    {
        return $this->initial;
    }

    public function setDisplayName($displayName)
    {
        $this->displayName = $displayName;
        return $this;
    }

    public function getDisplayName()
    {
        return $this->displayName;
    }

}

表單創建:

在service.config.php中(用於測試的臨時位置)

'factories' => array(
    'User\Form\AddUserForm' => function($sm) {

        $objectManager      = $sm->get('doctrine.entitymanager.orm_default');
        $builder            = new \Zend\Form\Annotation\AnnotationBuilder();

        $profileFieldset    = $builder->createForm('User\Model\Entity\Profile');
        $profileFieldset
        ->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($objectManager, 'User\Model\Entity\Profile'))
        ->setObject(new \User\Model\Entity\Profile())
        ->setUseAsBaseFieldset(true);


        $form               = $builder->createForm('User\Model\Entity\User');
        $form->add($profileFieldset);
        $form->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($objectManager, 'User\Model\Entity\User'));
        $form->setObject(new \User\Model\Entity\User());

        $form->add(array(
            'type' => 'Zend\Form\Element\Csrf',
            'name' => 'sitecheck'
        ));

        $form->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type' => 'submit',
                'value' => 'Save'
            )
        ));

        return $form;
    }
)

行動:

public function createAction()
{

    $user   = new \User\Model\Entity\User();
    $form   = $this->getServiceLocator()->get('User\Form\AddUserForm');
    $form->bind($user);
    var_dump($this->getRequest()->getPost());

    if ($this->request->isPost()) {
        $form->setData($this->getRequest()->getPost());

        if ($form->isValid()) {
            //save stuff isn't relevant here
        }
    }

    return new ViewModel(array(
        'form' => $form,
    ));
}

當我提交表單時,它將驗證“用戶”部分,但忽略字段集(required = true除外)。

我嘗試了我目前的所有知識。 因此,如果有人可以指出正確的方向,那將非常好。

您應該嘗試在User實體的Profile條目中使用ComposedObject批注

哇,解決方案是如此簡單。

感謝Gabriel Baker

在用戶實體中,我必須添加

/**
 * ...
 * @ZFA\ComposedObject("User\Model\Entity\Profile")
 * ...
 */
private $profile;

並將我的表單創建修改為

$objectManager      = $sm->get('doctrine.entitymanager.orm_default');
$builder            = new \Zend\Form\Annotation\AnnotationBuilder();

$form               = $builder->createForm('User\Model\Entity\User');
$form->setHydrator(new \DoctrineModule\Stdlib\Hydrator\DoctrineObject($objectManager, 'User\Model\Entity\User'));
$form->setObject(new \User\Model\Entity\User());

$form->add(array(
    'type' => 'Zend\Form\Element\Csrf',
    'name' => 'sitecheck'
));

$form->add(array(
    'name' => 'submit',
    'attributes' => array(
        'type' => 'submit',
        'value' => 'Save'
    )
));

return $form;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM