I'm using the latest version of Symfony and Easyadmin.
In my form I have a collection with an inline form to add another domain object "GameAward". I'm defining a Symfony form to display this form.

The problem is that while the main form displays red asterisks to highlight required fields, this is NOT the case for the inline form fields and there is no validation happening when submitting the form!
My config:
form:
fields:
[...]
- { property: 'gameAwards', type: 'collection', type_options: { entry_type: 'MyVendor\MyBundle\Form\GameAwardType', by_reference: false } }
My Form Type:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class, array(
'required' => true
))
->add('description')
->add('type', EnumGameAwardTypeType::class)
->add('condition')
->add('ScoreBonus');
}
I'm aware that required: true is the default of Symfony form fields, and in fact all of these fields are required and defined as nullable=false in the Doctrine Entity. However, to rule out that the issue is causes by the default behaviour as you can see I also explicitly specified it for the name field.
Is this behaviour a known limitation, a defect or am I doing something wrong?
I've the same problem, I think is a known limitation, if someone has an idea to fix it?
By design Symfony only validates the top level form. To validate an embedded form you must manually tell Symfony to do so by adding the Valid constraint to the mapped property:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\Valid()
*/
private $gameAwards;
And then add constraints inside your GameAward entity:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\NotBlank()
*/
private $name;
@phpPhil See #1124 to solve it.
@amustill it's not related to validation, even with assert, there is no "red star" in front for required fiels in nested collection
Is this issue still relevant in the latest versions of this bundle? I have simple collections and they display the asterisk for required fields ... but I can't test it with complex collections. Thanks!
Hi @javiereguiluz
Nop, still the same.
The complete case is:
Entity Page with a oneToMany (AssertValid) to Entity Block:/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="PageBlock", mappedBy="page", cascade={"merge", "persist"}, fetch="EAGER")
* @ORM\OrderBy({"createdAt" = "ASC"})
* @Assert\Valid()
*/
private $blocks;
Entity fields (and non-nullable field):/**
* @var string
* @ORM\Column(name="content", type="text")
* @Assert\NotBlank(message="Vous devez renseigner le contenu.")
*/
private $content;
FormType:- { property: 'blocks', label: 'Blocks de contenu', type: 'collection', type_options: { entry_type: 'AppBundle\Form\Type\PageBlockType', by_reference: false } }
FormType (event with trying to force 'required': true):/**
* Class PageBlockType.
*
* used by easyadminbundle custom bo
*/
class PageBlockType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'content',
CKEditorType::class,
array(
'label' => false,
'required' => true,
)
)
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\PageBlock',
));
}
}

As @HeahDude said in #1124, the only way to hack it is to add the correct classes for the label directly in the FormType:
$builder
->add(
'content',
CKEditorType::class,
array(
'label' => false,
'required' => true,
'label_attr' => ['class' => 'required label-required']
)
)
;
Hope it can helps.
This solve this issue, but i still don't know the source of this problem.
form:
fields:
- { property: '...', type: 'collection', type_options: { required: true } }
I'm closing this issue because we're starting a new phase in the history of this bundle (see #2059). We've moved it into a new GitHub organization and we need to start from scratch: no past issues, no pending pull requests, etc.
I understand if you are angry or disappointed by this, but we really need to "reset" everything in order to reignite the development of this bundle.
As you can see in Symfony code source (Form.php):
/**
* {@inheritdoc}
*/
public function isRequired()
{
if (null === $this->parent || $this->parent->isRequired()) {
return $this->config->getRequired();
}
return false;
}
This means you must set required to true like this:
- { property: rules, type: 'collection', type_options: {required: true, entry_type: 'App\Form\OfferRuleFormType', by_reference: false} }