Reputation: 4349
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?
Upvotes: 3
Views: 2631
Reputation: 4623
To overcome this by leaning on Symfony's Form builder, I've added constraint "NotBlank" to the "CKEditorField".
It looks like this at the controller:
...
use App\Admin\Field\CKEditorField;
use Symfony\Component\Validator\Constraints\NotBlank;
...
public function configureFields(string $pageName): iterable
{
return [
IdField::new('id')->hideOnForm(),
TextField::new('title')->setFormTypeOption('required',true),
CKEditorField::new('description')->setFormTypeOption('required',true)
->setFormTypeOption('constraints',[
new NotBlank(),
])
];
}
...
And the EasyAdmin field class file which is used in controller (add this to follow EasyAdmin's approach):
<?php
namespace App\Admin\Field;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
final class CKEditorField implements FieldInterface
{
use FieldTrait;
public static function new(string $propertyName, ?string $label = null):self
{
return (new self())
->setProperty($propertyName)
->setLabel($label)
->setFormType(CKEditorType::class)
->onlyOnForms()
;
}
}
Upvotes: 2
Reputation: 2280
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
Upvotes: 5