Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyAdminBundle: Validation not working on CKEditorType

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?

like image 207
Helenesh Avatar asked Sep 03 '26 23:09

Helenesh


2 Answers

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

like image 103
famas23 Avatar answered Sep 05 '26 15:09

famas23


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()
        ;
    }
}
like image 23
Vladimir Djuricic Avatar answered Sep 05 '26 16:09

Vladimir Djuricic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!