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
Out of curiosity: why did my validation work when I used the default
textType
field and not aCKEditorType
field?I think It depends on the form component, version and FosCkedito version, this kind of hack is fixed with last versions... I don't know where exactly, but it's all about data transforms. There's an issue in github talking about the trick I will try to find it. Any way Ckeditor does not ` implement
DataTransformerInterface
but TextType does. Could you give me some informations about your versions!!I am using "friendsofsymfony/ckeditor-bundle" version 1.2.0
This did not work for me. I now get an SQL Integrity constraint violation. For me, Easy Admin is not validating the form at all, and gets as far as trying to write the data to the database.
Could give more details about the constraint violatio