我在我的模块(Module.php)中修改了 ZfcUser 注册表单,其中包含两个新字段“firstname”和“lastname”,如下所示:
public function onBootstrap($e)
{
$events = $e->getApplication()->getEventManager()->getSharedManager();
$zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();
$events->attach('ZfcUser\Form\Register', 'init', function($e) {
$form = $e->getTarget();
$form->add(array(
'name' => 'firstname',
'type' => 'Text',
'options' => array(
'label' => 'First name: ',
),
));
$form->add(array(
'name' => 'lastname',
'type' => 'Text',
'options' => array(
'label' => 'Last name: ',
),
));
});
$events->attach('ZfcUser\Form\RegisterFilter', 'init', function($e) {
$filter = $e->getTarget();
$filter->add(array(
'name' => 'firstname',
'required' => true,
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'min' => 3,
'max' => 255,
),
),
),
));
$filter->add(array(
'name' => 'lastname',
'required' => true,
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'min' => 3,
'max' => 255,
),
),
),
));
});
$zfcServiceEvents->attach('register', function($e) {
$user = $e->getParam('user'); // User account object
$form = $e->getParam('form'); // Form object
//var_dump($form->get('firstname')->getValue()); die;
//var_dump($user); die;
});
$zfcServiceEvents->attach('register.post', function($e) {
$user = $e->getParam('user');
});
}
注册表看起来不错。我可以看到其他字段并且验证工作正常。问题是我无法在用户对象中设置这两个新字段:
$user->setFirstname($form->get('firstname')->getValue());
据说这个属性不存在。你能解释一下我做错了什么吗?