2

在使用EasyAdminBundle创建的管理面板中,我的表单验证仅适用于没有CKEditorType. 有些字段需要编辑,所以我使用FOSCKEditorBundle实现了所见即所得。

相关领域的片段:

- { property: 'content', type: 'FOS\CKEditorBundle\Form\Type\CKEditorType'} 

当我提交带有空“内容”字段的表单时,我收到InvalidArgumentException错误消息:Expected argument of type "string", "NULL" given.而不是像请填写此字段这样的验证错误。

没有CKEditor的相关领域的片段:

- { property: 'content' } 

=> 验证工作完美。

我的实体字段:

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank
     * @Assert\NotNull
     */
    private $content;

Symfony 分析器显示该字段确实有一个required属性。

如何启用CKEditor字段类型的验证?

4

2 回答 2

5

这与ckeditor无关。您所需要的只是修复您的内容设置器以通过参数接受NULL 。然后应该正确触发验证过程:

public function setContent(?string $content) {
    $this->content = $content;

    retrun $this;
}

在请求值设置为表单数据(在您的案例实体中)字段后执行验证。您可以在此处找到表单提交流程:https ://symfony.com/doc/current/form/events.html#submitting-a-form-formevents-pre-submit-formevents-submit-and-formevents-post-submit

于 2019-01-06T13:27:52.673 回答
1

为了通过使用 Symfony 的表单构建器来克服这个问题,我在“CKEditorField”中添加了约束“NotBlank”。

在控制器上看起来像这样:

...
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(),
        ])
    ];
}
...

以及控制器中使用的 EasyAdmin 字段类文件(添加此文件以遵循 EasyAdmin 的方法):

<?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()
        ;
    }
}
于 2020-12-01T11:52:04.000 回答