My application manage families. One family consist of 1 or N members.
I want the possibility to add one parent or two and 0 or N children. The children part works fine, but I have a hard time dealing with 1 or 2 parents.
Here is my family form type:
$builder
... many attributes
->add('parent1', MemberType::class)
->add('parent2', MemberType::class)
Parent and parent2 are OneToOne association (Family to member). The member form type :
$builder
->add('firstName', TextType::class, [
'label' => 'Prénom',
'constraints' => array(
new NotBlank(),
new Length(array('max' => 150))
)
])
... many other attributes with choices or not
I thought of a checkbox that grey out the fields of the parent 2 if unchecked, but the member values are all required. Because of that SF2 does not validate my form.
If I set required => false to these fields (in the builder) then the user will have the possibility to validate without filling everything (which I don't want).
I'd like to create the following process :
- Either we fill all the fields of the member2 in order to validate the form
- Either we check a checkbox (single parent) and no field is required, and my final member2 will be null (or another solution)