Symfony 3 - 表示不添加"有错误" VichImageType和自定义约束中发生错误时的类

时间:2016-05-07 11:25:33

标签: php symfony vichuploaderbundle symfony2-easyadmin

问题视图:

Error

问题代码:

enter image description here

形式:

    $builder->add('title', Type\TextType::class, [
        'label_attr' => ['class' => 'required label-required'],
    ]);
    $builder->add('isPublished');
    $builder->add('imageFile', VichImageType::class, [
        'label_attr'     => ['class' => 'required label-required'],
        'allow_delete'   => false,
    ]);
    $builder->add('alt', Type\TextType::class, [
        'label_attr' => ['class' => 'required label-required'],
    ]);

问题:

有人可以告诉我,为什么has-error没有添加到第三个表单字段?要生成错误,title字段正在使用@Assert\NotBlank()和"图像文件"正在使用我的自定义约束:

FileNotEmpty 类:

<?php

namespace Notimeo\CoreBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 * @Target("CLASS")
 */
class FileNotEmpty extends Constraint
{
    /**
     * @var string
     */
    public $message = 'This field cannot be empty.';

    /**
     * @var array
     */
    public $fields = [];

    /**
     * @return string
     */
    public function validatedBy()
    {
        return get_class($this).'Validator';
    }

    /**
     * @return string
     */
    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }
}

FileNotEmptyValidator 类:

<?php

namespace Notimeo\CoreBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
 * Class FileNotEmptyValidator
 *
 * @package Notimeo\CoreBundle
 */
class FileNotEmptyValidator extends ConstraintValidator
{
    /**
     * @param mixed      $protocol
     * @param Constraint $constraint
     */
    public function validate($protocol, Constraint $constraint)
    {
        /* @var $constraint FileNotEmpty */
        foreach($constraint->fields as $field) {
            $method1 = 'get'.ucfirst($field);
            $method2 = $method1.'File';

            $value1 = call_user_func([$protocol, $method1]);
            $value2 = call_user_func([$protocol, $method2]);

            if(empty($value1) && empty($value2)) {
                $this->context->buildViolation($constraint->message)
                    ->atPath($field.'File')
                    ->addViolation();
            }
        }
    }
}

使用最新的 Symfony 3 EasyAdminBundle 生成此表单。是什么导致了这个问题?

1 个答案:

答案 0 :(得分:0)

我们最近做了一些与此相关的更改。请参阅:https://github.com/javiereguiluz/EasyAdminBundle/commit/3405a7a1029762365a08d38d59765197836c9fcb

请问您是否正在使用包含这些更改的捆绑版本?谢谢!