2016-05-07 3 views
0

問題ビュー:symfonyの3 - フォームのエラーがVichImageTypeに発生したときに、 "持っているエラー" クラスを追加し、カスタム制約はない

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は、第三のフォームフィールドに追加されていない理由

は誰でも、私に教えてもらえますか?

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

最新のを使用して、私のカスタム制約を使用しているtitleフィールドが@Assert\NotBlank()と "イメージファイル" を使用して、エラーを生成するにはsymfony 3およびEasyAdminBundleを使用してこのフォームを生成します。この問題の原因は何ですか?

答えて

関連する問題