问题描述:

I have created a ZF2 project with the following dependencies:

 "php": ">=5.3.3",

"zendframework/zendframework": "2.*",

"zendframework/zend-developer-tools": "dev-master",

"doctrine/doctrine-orm-module": "dev-master",

"bjyoungblood/bjy-profiler": "dev-master"

I created a User Entity and a RegisterUser Form. In my index action I bind the User Entity to my register form and pass it to my view - like so:

public function indexAction() {

$entityManager = $this->getServiceLocator ()->get ( 'Doctrine\ORM\EntityManager' );

$form = new RegisterUserForm ( $this->serviceLocator );

$user = new User ();

$form->bind ( $user );

if ($this->getRequest()->isPost()) {

$form->setData ( $this->getRequest()->getPost () );

if ($form->isValid ()) {

$entityManager->persist($user);

$entityManager->flush();

}

}

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

}

Now I would like to add another form (login) to the same view. I am having some trouble figuring out the best way to do so. Binding multiple forms to the same User entity feels dangerous, and I'm not sure how to handle the posts in a different actions.

Here is a look at my form:

class RegisterUserForm extends Form

{

public function __construct(ServiceManager $serviceManager)

{

parent::__construct('register-user-form');

$entityManager = $serviceManager->get('Doctrine\ORM\EntityManager');

$this->setHydrator(new DoctrineHydrator ($entityManager, 'Application\Entity\User'));

$userPostFieldset = new UserPostFieldset($serviceManager);

$userPostFieldset->setUseAsBaseFieldset(true);

$this->add($userPostFieldset);

}

}

And the corresponding Fieldset:

class UserPostFieldset extends Fieldset implements InputFilterProviderInterface {

public function __construct(ServiceManager $serviceManager) {

parent::__construct ( 'user' );

$entityManager = $serviceManager->get ( 'Doctrine\ORM\EntityManager' );

$this->setHydrator ( new DoctrineHydrator ( $entityManager, 'Application\Entity\User' ) )->setObject ( new User () );

$this->add ( array (

'type' => 'Zend\Form\Element\Text',

'name' => 'firstName',

'options' => array (

'label' => 'First Name'

)

) );

$this->add ( array (

'type' => 'Zend\Form\Element\Email',

'name' => 'email',

'options' => array (

'label' => 'Email Address'

)

) );

$this->add ( array (

'type' => 'Zend\Form\Element\Email',

'name' => 'emailCheck',

'options' => array (

'label' => 'Re-Enter Email Address'

)

) );

$this->add ( array (

'name' => 'submit',

'attributes' => array (

'type' => 'submit',

'value' => 'Submit'

)

) );

}

public function getInputFilterSpecification() {

return array (

'email' => array (

'required' => true,

'filters' => array (

array (

'name' => 'StripTags'

),

array (

'name' => 'StringTrim'

)

),

'validators' => array (

array (

'name' => 'Identical',

'options' => array (

'token' => 'emailCheck',

'message' => 'Emails do not match'

)

)

)

)

);

}

}

Any insight would be appreciated.

Questions:

1 - What is the best practice for adding multiple forms to a view that deal with the same Entity?

2 - How can I break apart my index action so it passes multiple forms to my view, and then map the posts to different actions to perform validation checks?

Thanks,

D

网友答案:

Are those Forms really identical? A register Form usually has at least either a PW-Confirmation Field, OR a PW-Checkbox to display the PW as a text.

Ultimately though, you'd simply pass 2 Form-Objects to your View. Both Form-Objects should point to a different action like:

// Class Module#getServiceConfig()
return array( 'factories' => array(
'my-login-form' => function($sm) {
    $url  = $sm->get('viewhelpermanager')->get('url');
    $form = new RegisterLoginForm($sm);
    $form->setAttribute('action', $url('my/route/login'));
    return $form;
}
'my-register-form' => function($sm) {
    $url  = $sm->get('viewhelpermanager')->get('url');
    $form = new RegisterLoginForm($sm);
    $form->setAttribute('action', $url('my/route/register'));
    return $form;
}

This way you have two Form Objects and everything should work out just well. Your Controller then would simply look like this:

$sl           = $this->getServiceLocator();
$formLogin    = $sl->get('my-login-form');
$formRegister = $sl->get('my-register-form');

return array(
    'loginform'    => $formLogin,
    'registerform' => $formRegister
);

Realize, that the final "bind" to your Entities will only be made upon posting the form. Therefore only one form / entity is concerned.

相关阅读:
Top