In my admin panel created with EasyAdminBundle, my form validations only work with fields that do not have the CKEditorType
. Some fields need to be edited so I implemented a WYSIWYG with FOSCKEditorBundle.
Snippet from field concerned:
- { property: 'content', type: 'FOS\CKEditorBundle\Form\Type\CKEditorType'}
When I submit the form with an empty 'content' field, I get an InvalidArgumentException
with the error: Expected argument of type "string", "NULL" given.
instead of a validation error like Please fill in this field.
Snippet from field concerned without CKEditor:
- { property: 'content' }
=> validation works perfectly.
My entity field:
/**
* @ORM\Column(type="text")
* @Assert\NotBlank
* @Assert\NotNull
*/
private $content;
The Symfony profiler shows that this field indeed has a required
attribute.
How can enable the validations with the CKEditor
field type?
It's not about ckeditor. All you need is to fix your content setter to accept NULL through the argument. Then the validation process should be fired correctly:
public function setContent(?string $content) {
$this->content = $content;
retrun $this;
}
Validation is performed after request values are set to form data (in your case entity) fields. You can find form submit flow here: https://symfony.com/doc/current/form/events.html#submitting-a-form-formevents-pre-submit-formevents-submit-and-formevents-post-submit