【问题标题】:EasyAdminBundle: Validation not working on CKEditorTypeEasyAdminBundle:验证不适用于 CKEditorType
【发布时间】:2019-06-01 08:55:12
【问题描述】:

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

相关领域的片段:

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

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

不使用 CKEditor 的相关字段的片段:

- { property: 'content' } 

=> 验证完美运行。

我的实体字段:

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

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

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

【问题讨论】:

    标签: symfony ckeditor symfony4 easyadmin


    【解决方案1】:

    这与 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

    【讨论】:

    • 出于好奇:为什么当我使用默认的 textType 字段而不是 CKEditorType 字段时我的验证工作?
    • 我认为这取决于表单组件、版本和 FosCkedito 版本,这种 hack 已在最新版本中修复...我不知道具体在哪里,但都是关于数据转换的。 github中有一个问题,谈论我将尝试找到它的技巧。无论如何,Ckeditor 都不会 ` 实现 DataTransformerInterface 但 TextType 会。你能给我一些关于你的版本的信息吗!
    • 我正在使用“friendsofsymfony/ckeditor-bundle”版本 1.2.0
    • 这对我不起作用。我现在得到一个 SQL 完整性约束违规。对我来说,Easy Admin 根本不验证表单,而是尝试将数据写入数据库。
    • 可以提供有关约束违规的更多详细信息
    【解决方案2】:

    为了通过依赖 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()
            ;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-18
      • 2011-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 2018-01-24
      相关资源
      最近更新 更多