我正在使用 zfcUser 进行注册和身份验证,我有一些问题。用户可以通过 2 种类型的配置文件进行注册,并且形式不同,所以问题是,在 zfcUser 中使用少量注册表单的最佳实践是什么,在一个页面上,使用选项卡。
1 回答
1
我能想到几个解决方案。这是我认为最适合您的目的。这是一段代码。
首先,您必须考虑数据库,根据用户类型,您可能会拥有一张具有不同字段的表,或者一张普通表和具有额外字段的不同表。解决方法是一样的。
首先,如您所知,有一个检索注册表的服务。您可以使用注册事件回调来修改它们,但是,由于您需要不同的表单,而且它们都不是标准的,我认为最好是为这两个新表单创建 2 个新服务。
为此,在getServiceConfig()函数的模块中,您创建了这两个服务,它们回复 zfcuser_register_form 服务,但添加和删除字段以及输入过滤器字段
<?
public function getServiceConfig()
{
return array(
'factories' => array(
'zfcuser_register_form_usertype1' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$form = new Form\Register(null, $options);
///for type:
$this->add(array(
'name' => 'usertype',
'attributes' => array(
'type' => 'hidden',
'value' => '1',
),
));
/*
* Add o remove fields to the form
*/
$form->setInputFilter(new Form\RegisterFilter(
new Validator\NoRecordExists(array(
'mapper' => $sm->get('zfcuser_user_mapper'),
'key' => 'email'
)),
new Validator\NoRecordExists(array(
'mapper' => $sm->get('zfcuser_user_mapper'),
'key' => 'username'
)),
$options
));
/*
* Add o remove fields to the input filter
*/
return $form;
},
////////////////////////////////////////////////////////////
'zfcuser_register_form_usertype2' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$form = new Form\Register(null, $options);
///for type:
$this->add(array(
'name' => 'usertype',
'attributes' => array(
'type' => 'hidden',
'value' => '2',
),
));
/*
* Add o remove fields to the form
*/
$form->setInputFilter(new Form\RegisterFilter(
new Validator\NoRecordExists(array(
'mapper' => $sm->get('zfcuser_user_mapper'),
'key' => 'email'
)),
new Validator\NoRecordExists(array(
'mapper' => $sm->get('zfcuser_user_mapper'),
'key' => 'username'
)),
$options
));
/*
* Add o remove fields to the input filter
*/
return $form;
},
),
);
}
有关如何添加或删除字段的指南,请访问表单的官方手册页
然后,在将显示注册表单的控制器/操作中(它应该是您自己的路由,而不是标准的 zfuser 之一)
public function registerAction()
{
//get the first form
$form1 = $this->getServiceLocator ()->get ( 'zfcuser_register_form_usertype1' );
//set the action to the zfuser registation route
$form1->setAttribute('action', $this->url('zfcuser/register'));
$form1->setAttribute('method', 'post');
//the same for the second form
$form2 = $this->getServiceLocator ()->get ( 'zfcuser_register_form_usertype2' );
$form2->setAttribute('action', $this->url('zfcuser/register'));
$form2->setAttribute('method', 'post');
//send both form to the view
return array('form1' => $form1,'form2' => $form2);
}
然后是视图,只需检索表单并呈现它们,就像使用任何表单一样(如果您有任何疑问,请转到上面链接的手册,其中还包含有关如何呈现表单的信息)
<?php
/*
* html for tabs
*/
$form = $this->form1;
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('usertype'));
echo $this->formRow($form->get('field1'));
echo $this->formRow($form->get('field2'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
/*
* other html for tabs
*/
$form2 = $this->form2;
$form2->prepare();
echo $this->form()->openTag($form2);
echo $this->formHidden($form->get('usertype'));
echo $this->formRow($form2->get('field1'));
echo $this->formRow($form2->get('field2'));
echo $this->formSubmit($form2->get('submit'));
echo $this->form()->closeTag();
/*
* other html for tabs
*/
现在,你有了你的表格。剩下的唯一事情就是处理注册。您可能知道,zfcuser 有一个事件管理器,它允许我们为某些事件安排操作,例如,当创建一个新帐户时
为此,在您的模块中,在您的onBootstrap功能中
<?
/**
* executes on boostrap
*
* @param \Zend\Mvc\MvcEvent $e
* @return null
*/
public function onBootstrap(MvcEvent $e) {
//retrieve the zfcuser event manager
$sm = $e->getApplication()->getServiceManager();
$zfcServiceEvents = $sm->get('zfcuser_user_service')->getEventManager();
// add a callback to Store the field when the form is submited
$zfcServiceEvents->attach('register', function ($e) use($sm)
{
$form = $e->getParam('form');//here you have the submited form
$user = $e->getParam('user');//here you have the already created standard user entity
$type=$form->get("usertype")->getValue();//retrieve the user type
//with this, you can decide what other fields to retrieve,
//and how to asign it to the entity or to other related entities:
if ($type==1) {
//...
}
else {
//...
}
});
}
于 2014-01-22T01:57:18.300 回答