-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Required fields in collection #1671
Comments
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 use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\Valid()
*/
private $gameAwards; And then add constraints inside your use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\NotBlank()
*/
private $name; |
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! |
Nop, still the same. The complete case is:
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="PageBlock", mappedBy="page", cascade={"merge", "persist"}, fetch="EAGER")
* @ORM\OrderBy({"createdAt" = "ASC"})
* @Assert\Valid()
*/
private $blocks;
/**
* @var string
* @ORM\Column(name="content", type="text")
* @Assert\NotBlank(message="Vous devez renseigner le contenu.")
*/
private $content;
- { property: 'blocks', label: 'Blocks de contenu', type: 'collection', type_options: { entry_type: 'AppBundle\Form\Type\PageBlockType', by_reference: false } }
/**
* 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):
This means you must set required to true like this:
|
Example
PropertyType is a custom class to generate subform |
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:
My Form Type:
I'm aware that
required: true
is the default of Symfony form fields, and in fact all of these fields are required and defined asnullable=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 thename
field.Is this behaviour a known limitation, a defect or am I doing something wrong?
The text was updated successfully, but these errors were encountered: