[英]Nested Form / Mapping field to entity
I'm looking for best (or just working) way to solve following problem. 我正在寻找最好的(或只是工作)方式来解决以下问题。 I have like standard UserType form
我喜欢标准的UserType表单
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'username',
Type\TextType::class
)
->add(
'email',
Type\EmailType::class
)
->add(
'plainPassword',
Security\UserRepeatedPasswordType::class
)
->add(
'roles',
Type\ChoiceType::class,
[
'multiple' => true,
'expanded' => true,
'choices' => $this->getRoleChoices()
]
);
}
What is nonstandard is that UserRepeatedPasswordType
, it looks like this 什么是非标准的是
UserRepeatedPasswordType
,它看起来像这样
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'password',
Type\RepeatedType::class,
[
'type' => Type\PasswordType::class,
'required' => true,
'first_options' => [
'label' => 'Password'
],
'second_options' => [
'label' => 'Repeat Password'
],
]
);
}
And I created it because those two fields are also used in passwordReset form and userSettings form. 我创建它是因为这两个字段也用于passwordReset表单和userSettings表单。 And now I have two problems:
现在我有两个问题:
1.) When I use it this way, value from UserRepeatedPasswordType
is not correctly mapped for my User
Entity - there is an error that string is expected (duh ;) but it got array. 1.)当我以这种方式使用它时,
UserRepeatedPasswordType
值未正确映射到我的User
实体 - 有一个错误,即字符串是预期的(duh;)但它有数组。 I tried using View and Model transformer but no proper results (but I don't have much experience with those, so that maybe the case). 我尝试使用View和Model变换器但没有正确的结果(但我对这些没有太多经验,所以可能就是这种情况)。 I also tried to experiment with
getParent()
, and pass there UserType
but it goes to some endless loop and I got 500. If I just copy paste field from UserRepeatedPasswordType
to UserType
it works correctly. 我也尝试用
getParent()
进行实验,然后传递给UserType
但它进入了一些无限循环,我得到了500.如果我只是将UserRepeatedPasswordType
粘贴字段复制到UserType
它UserRepeatedPasswordType
正常工作。
2.) If this is solved (or even by copy paste, if can't be done other way), there is another related (I believe) problem: 2.)如果这个问题得到解决(或者甚至通过复制粘贴,如果不能以其他方式完成),还有另一个相关的(我相信)问题:
I have this ChangePasswordType
form, which is used to reset your password. 我有这个
ChangePasswordType
表单,用于重置密码。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'confirmationToken',
Type\HiddenType::class,
[
'required' => true,
'constraints' => [
new NotBlank(),
]
]
)
->add(
'plainPassword',
Type\RepeatedType::class,
[
'type' => Type\PasswordType::class,
'required' => true,
'first_options' => [
'label' => 'Password'
],
'second_options' => [
'label' => 'Repeat Password'
],
]
)
->add(
'changePassword',
Type\SubmitType::class
);
}
And it works fine as it is but I want to do two things with it - first, solving my first problem and use UserRepeatedPasswordType
in it, second - I have some Assert\\Length
done in User
Entity on $plainPassword
and it workes correctly when I submit new user via UserType form. 并且它工作得很好但是我想用它做两件事 - 首先,解决我的第一个问题并在其中使用
UserRepeatedPasswordType
,第二个 - 我在$plainPassword
上的User
实体中完成了一些Assert\\Length
,它在我运行时正常工作通过UserType表单提交新用户。 But I want that validation somewhat mapped to ChangePasswordType
or ideally to UserRepeatedPasswordType
- just to have all rules in one place. 但我希望验证有些映射到
ChangePasswordType
或理想情况下映射到UserRepeatedPasswordType
- 只是将所有规则放在一个地方。 Can this even be done? 甚至可以这样做吗? Thanks for any solutions / hints / advices.
感谢您提供任何解决方案/提示/建议。
Ok, dunno if anyone is interested but that is how I completed this. 好吧,不知道是否有人感兴趣,但这就是我完成这个的方式。 If anyone have better answer, just give me a sign (mostly to the first one) ;)
如果有人有更好的答案,请给我一个标志(主要是给第一个);)
1.) As i thought, solved by ViewTransformer but in parent form (In UserType
not in UserRepeatedPasswordType
1.)正如我所想,由ViewTransformer解决,但是以父表单形式解决(在
UserType
不在UserRepeatedPasswordType
$builder->get('plainPassword')
->addViewTransformer(new CallbackTransformer(
function ($singleAsArray) {
return $singleAsArray;
},
function ($arrayAsSingle) {
return $arrayAsSingle['password'] ?? '';
}
));
2.) That was actually quite easy. 2.)这实际上非常简单。 All you have to do is to map that form to UserEntity that same way as
UserType
and made custom validation groups just to have everything nice and under control :) 您所要做的就是以与
UserType
相同的方式将该表单映射到UserEntity,并制作自定义验证组,以使一切都很好并受到控制:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.