我需要将用户名转换为用户实体,并且我以多种形式使用此转换,每个表单都需要对返回的用户实体进行一些特定的测试。
在注册表中,用户可以输入推荐代码(用户名)或将其留空,推荐代码必须具有角色ROLE_PARTNER。
在仪表板中,合作伙伴可以将某个用户附加到他的帐户:在这种情况下,用户名称不能为空,用户必须具有角色ROLE_CLIENT,并且合作伙伴无法输入自己的用户名
这是我的数据转换器类
class UserToNameTransformer implements DataTransformerInterface
{
private $om;
private $role;
private $acceptEmpty;
private $logged;
public function __construct(ObjectManager $om,$role,$acceptEmpty,$logged)
{
$this->om = $om;
$this->role=$role;
$this->acceptEmpty=$acceptEmpty;
$this->logged=$logged;
}
public function transform($user)
{
if (null === $user) {
return "";
}
return $user->getUsername();
}
public function reverseTransform($username)
{
if (!$username) {
if ($this->acceptEmpty)
return null;
else
throw new TransformationFailedException(sprintf(
'user name can\'t be empty!'
));
}
$user = $this->om
->getRepository('FMFmBundle:User')
->findOneBy(array('username' => $username))
;
if (null === $user) {
throw new TransformationFailedException(sprintf(
'user with user name "%s" not found!',
$username
));
}
else if (!$user->hasRole($this->role)){
throw new TransformationFailedException(sprintf(
'user name "%s" is not valid',
$username
));
}
else if($this->logged==true){
$activeUser=$this->get('security.context')->getToken()->getUser();
if($user===$activeUser)
throw new TransformationFailedException(sprintf(
'you can\'t enter you user name'
));
}
else
return $user;
}
}
表格类型
此表单工作正常,因为我没有使用自定义字段
class ActivatePartnerType extends AbstractType
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user','text', array(
'invalid_message' => 'That is not a valid username',
'label' => 'Partner Username :',
))
->add('next','submit')
;
$builder->get('user')
->addModelTransformer(new UserToNameTransformer($this->entityManager,'ROLE_PARTNER',false,true));
}
public function getName()
{
return 'fm_fmbundle_activate_partner';
}
}
自定义字段类型
class UserSelectorType extends AbstractType
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new UserToNameTransformer($this->entityManager,**{others parametres}**);
$builder->addModelTransformer($transformer);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'invalid_message' => 'The selected user does not exist'
));
}
public function getParent()
{
return 'text';
}
public function getName()
{
return 'user_selector';
}
}
自定义字段服务
FmarketUserSelector:
class: FM\FmBundle\Form\UserSelectorType
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: form.type, alias: user_selector }
表单类型
class ActivatePartnerType extends AbstractType
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user','user_selector')
->add('next','submit')
;
}
public function getName()
{
return 'fm_fmbundle_activate_partner';
}
}
答案 0 :(得分:0)
这里是我如何使用$ options数组
class UserSelectorType extends AbstractType
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new UserToNameTransformer($this->entityManager,$options['role'],$options['accept_empty'],$options['logged']);
$builder->addModelTransformer($transformer);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'invalid_message' => 'The selected user does not exist',
'role'=>'ROLE_PARTNER',
'accept_empty'=>true,
'logged'=>false
));
}
public function getParent()
{
return 'text';
}
public function getName()
{
return 'user_selector';
}
}
在表单类型
中$builder->add('referral','user_selector',array(
'role'=>'ROLE_PARTNER',
'accept_empty'=>false,
'logged'=>false
));