Zf2 - Doctrine Hydrator Fieldset

时间:2015-03-21 17:42:03

标签: forms zend-framework2 doctrine

我是ZendFramework和Doctrine 2的新手。 我尝试用fieldset和Doctrine Hydrator实现一个表单。

表单和验证器工作正常,但不是Hydrator。

我的源代码:

AuthController.php

public function signupAction()
{
     $userForm = new UserForm($this->getServiceLocator()->get('Doctrine\ORM\EntityManager'));

        $request = $this->getRequest();
        if ($request->isPost()) {
            var_dump($request->getPost());
            $userForm->setData($request->getPost());
            if ($userForm->isValid()) {
                $user = $userForm->getData();
                var_dump($user);
            }
        }

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

UserForm.php

class UserForm extends Form
{

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

        $this->setAttribute('method', 'post')
             ->setHydrator(new DoctrineObject($em, 'User\Entity\User'));

        $this->add(array(
            'type' => 'User\Form\Fieldset\UserFieldset',
            'options' => array(
                'use_as_base_fieldset' => true
            )
        ));

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type' => 'submit',
                'value' => 'Send'
            )
        ));
    }

}

UserFieldset.php

class UserFieldset extends Fieldset implements InputFilterProviderInterface, ObjectManagerAwareInterface
{
    /**
     * @var ObjectManager
     */
    private $objectManager;


    public function __construct()
    {
        parent::__construct('User');

        // Id
        $this->add(array(
            'name'        => 'id',
            'type'        => 'Zend\Form\Element\Hidden',
        ));

        // FirstName
        $this->add(array(
            'name'        => 'firstName',
            'attributes'  => array(
                'required' => 'required',
            ),
            'options'     => array(
                'label'       => 'Prénom',
            ),
            'type'        => 'Zend\Form\Element\Text',
        ));
    }

    public function getInputFilterSpecification()
    {
        return array(
            'firstName' => array(
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 2,
                            'max' => 100,
                            'messages' => array(
                                \Zend\Validator\StringLength::TOO_LONG  => 'Le prénom est trop long (maximum %max% caractères)',
                                \Zend\Validator\StringLength::TOO_SHORT => 'Le prénom est trop court (minimum %min% caractères)',
                            ),
                        ),
                    ),
                ),
            ),
            'id' => array(
                'required'    => false,
            ),
        );
    }

    public function setOption($key, $value) { }

    /**
     * Set the object manager
     *
     * @param ObjectManager $objectManager
     */
    public function setObjectManager(ObjectManager $objectManager)
    {
        $this->objectManager = $objectManager;
    }

    /**
     * Get the object manager
     *
     * @return ObjectManager
     */
    public function getObjectManager()
    {
        return $this->getObjectManager();
    }
}

我没有User对象,只有数组......

一个想法?

谢谢! 的Aurelien

0 个答案:

没有答案