一个Symfony 4应用程序具有一个用户实体,该用户实体使用Joined InheritanceType,类型为Admin
,Staff
和Volunteer
。 Staff
实体与Organization
实体具有OneToOne关系。因此,“组织”表单类型包括子staff
,其子项具有自定义表单类型NewUserType
。 (相关摘录显示在下面。)如果提交了空的OrganizationType
表单,则除了名称和电子邮件错误外,所有预期的表单错误都会被呈现。 password
的{{1}}格式错误字段已呈现。 Symfony的探查器不会显示名称或电子邮件错误。 NewUserType
中的error_bubbling
没有区别。组织实体的NewUserType
属性上的@Assert\Valid
没有区别。
registerOrganization:
staff
new_user.html.twig:
public function registerOrganiztion(Request $request) {
$form = $this->createForm(OrganizationType::class);
$templates = [
'Registration/organization.html.twig',
'Registration/new_user.html.twig',
'Registration/focuses.html.twig',
];
...
return $this->render('Default/formTemplates.html.twig', [
'form' => $form->createView(),
'headerText' => 'Add an organization',
'userHeader' => 'Staff Member',
'orgHeader' => 'Organization',
'focusHeader' => "Organization's Focus",
'templates' => $templates,
]);
}
OrganizationType:
{% if form.staff is defined %}{% set user = form.staff %}{% else %}{% set user = form %}{% endif %}
<div class="text-center text-bold">
{{ userHeader }}
</div>
{{ form_row(user.fname) }}
{{ form_row(user.sname) }}
{{ form_row(user.email) }}
{{ form_row(user.plainPassword.first) }}
{{ form_row(user.plainPassword.second) }}
NewUserType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
...
->add('staff', NewUserType::class)
}
组织实体
class NewUserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('sname', null, [
'attr' => [
'class' => 'mb-2',
'size' => '15',
],
'label' => 'Last name: ',
'label_attr' => ['class' => 'mr-2'],
])
->add('fname', null, [
'attr' => [
'class' => 'mb-2',
'size' => '15',
],
'label' => 'First name: ',
'label_attr' => ['class' => 'mr-2'],
])
->add('email', null, [
'attr' => [
'class' => 'mb-2',
'size' => '15',
],
'label' => 'Email: ',
'label_attr' => ['class' => 'mr-2'],
])
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'mapped' => false,
'constraints' => [new NotBlank(['message' => "Password may not empty"])],
'invalid_message' => 'Passwords do not match',
'first_options' => [
'attr' => [
'class' => 'mb-2',
'size' => '15',
],
'label' => 'Password:',
'label_attr' => ['class' => 'mr-2'],
'required' => true,
],
'second_options' => [
'attr' => [
'class' => 'mb-2',
'size' => '15',
],
'label' => 'Confirm:',
'label_attr' => ['class' => 'mr-2'],
'required' => true,
],
))
;
if (Volunteer::class === $options['data_class']) {
$builder
->add('focuses', FocusFieldType::class)
->add('skills', SkillFieldType::class)
;
}
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'data_class' => Staff::class,
'required' => false,
'error_bubbling' => true,
]);
}
}
员工实体
/**
* Organization
*
* @ORM\Table(name="organization")
* @ORM\Entity
*/
class Organization
{
...
/**
* @ORM\OneToOne(targetEntity="Staff", inversedBy="organization")
* @ORM\JoinColumn(name="staff_id", referencedColumnName="id")
* @Assert\Valid
*/
protected $staff;
...
}
答案 0 :(得分:0)
虽然我不知道为什么不显示姓名和电子邮件表单错误,但是可以通过将约束添加到表单类型来强制执行。然后只需从用户实体中的这些字段中删除@Assert\...
。