0

問題ビュー:

エラー

問題コード:

ここに画像の説明を入力

形:

    $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-error3番目のフォームフィールドに追加されないのはなぜですか? エラーを生成するために、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 3EasyAdminBundleを使用してこのフォームを生成します。この問題の原因は何ですか?

4

1 に答える 1

0

最近、これに関連するいくつかの変更を行いました。参照: https://github.com/javiereguiluz/EasyAdminBundle/commit/3405a7a1029762365a08d38d59765197836c9fcb

これらの変更を含むバージョンのバンドルを使用しているかどうかを確認していただけますか? ありがとう!

于 2016-05-08T16:37:29.157 に答える