ZF2 Doctrine 2 NoObjectExists具有多个字段的验证器

时间:2014-08-01 21:40:31

标签: php doctrine-orm zend-framework2

我对Doctrine 2 NoObjectExists验证器有疑问。对于一个字段,它工作正常,但有多个字段我得到错误:

Provided values count is 1, while expected number of fields to be matched is 2

实体:

/**
 * Message
 *
 * @ORM\Table(name="messages")
 * @ORM\Entity
 */
class Message
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=32, nullable=false)
     */
    private $name;

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="User", inversedBy="assignedMessages")
     * @ORM\JoinColumn(name="user_id")
     */
    private $user;
}     

Fieldset类

    class MessageFieldset extends Fieldset implements InputFilterProviderInterface
    {
        protected $objectManager;

        public function __construct(ObjectManager $em)
        {
            parent::__construct('message');

            $this->setObjectManager($em);

            $this->setHydrator(new DoctrineHydrator($em))
                ->setObject(new Message());



            /* User Field */
            $this->add(array(
                'name' => 'user',
                'attributes' => array(
                    'type'  => 'hidden',
                ),
            ));



            /* Name Field */
            $this->add(array(
                'name' => 'name',
                'attributes' => array(
                    'type'  => 'text',
                    'id' => 'name-label',
                    'class' => 'form-control',
                ),
                'options'    => array(
                    'label' => 'Title',
                    'label_attributes' => array(
                        'for' => 'name-label'
                    ),
                )
            ));
        }

        public function getInputFilterSpecification()
        {
            return array(
                'name' => array(
                    'required' => true,
                    'filters'  => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'NotEmpty',
                            'options' => array(
                                'messages' => array(
                                    NotEmpty::IS_EMPTY => 'custom text'
                                )
                            )
                        ),
                        array(
                            'name' => 'Regex',
                            'options' => array(
                                'pattern' => '/[a-zA-Z][a-zA-Z0-9_-]*/'
                            )
                        ),
                        array(
                            'name' => 'DoctrineModule\Validator\NoObjectExists',
                            'options' => array(
                                'object_repository' => $this->getObjectManager()->getRepository('Application\Entity\Message'),
                                'fields' => array('name', 'user'),
                                //'fields' => 'name', <--- with one field it works fine
                                'messages' => array(
                                    'objectFound' => 'Custom text'
                                ),
                            )
                        )
                    )
                ),
            )
        }
}

表单类

class MessageForm extends Form
{


    public function __construct(ObjectManager $em)
    {

        parent::__construct('message-form');

        $this->setAttributes(array(
            'method' => 'post',
            'role' => 'form'
        ));


        $this->setHydrator(new DoctrineHydrator($em));

        $fieldset = new MessageFieldset($em);
        $fieldset->setUseAsBaseFieldset(true);
        $this->add($fieldset);


        $this->add(array(
            'type' => 'Zend\Form\Element\Csrf',
            'name' => 'csrf',
            'options' => array(
                'csrf_options' => array(
                    'timeout' => 600
                )
            )
        ));



        $submit = new Element\Button('submit');
        $submit->setLabel('Add');
        $submit->setAttributes(
            array(
                'type' => 'submit',
                'class' =>'btn btn-primary'
            ));

        $this->add($submit);
    }



}

我的行动:

public function messageAction()
{
    $form = new MessageForm($this->em);

    $message = new Message();
    $form->bind($message);

    if ($this->request->isPost()) {
        $data = $this->request->getPost();
        //var_dump($data);   <--- it shows all values(name, user)
        $form->setData($data);


        if ($form->isValid()) {
            //...
        }else{
            //...
        }
    }

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

因此,如果我只设置一个字段(名称),一切正常,但如果我传递带字段的数组(用户,名称),我会收到上述错误。
知道我的代码可能有什么问题吗?在我的实体用户字段与另一个表的关系,所以我尝试手动设置如下:

if ($this->request->isPost()) {
    $data = $this->request->getPost();
    $data->message['user'] = $this->identity() <--- obj of Entity\User
    $form->setData($data);
    ...
}

但它也没有用。

2 个答案:

答案 0 :(得分:0)

尝试使用DoctrineModule\Validator\UniqueObject验证程序,如下所示:

array(
    'name' => 'DoctrineModule\Validator\UniqueObject',
    'options' => array(
        'object_repository' => $this->getObjectManager()->getRepository('Application\Entity\Message'),
        'object_manager'  => $this->getObjectManager(),
        'fields' => array('name', 'user'),
        'messages' => array(
            'objectNotUnique' => 'Custom text'
        ),
    )
)

这将验证相同的nameuser是否存在对象。

答案 1 :(得分:0)

要在NoObjectExists验证程序中使用多个字段,还需要将一组值绑定到验证程序。

在Ocramius in this post on GitHub

的答案中阅读有关此问题的更多详细信息