2016-10-05 7 views
1

タイトルで述べたように、私は実際にSymfonyで検証プロセスを作成する必要があります。 私はYAMLファイルを使用していますが、大丈夫です。 しかし、場合によっては、データが有効であると言う前にデータベースをチェックする必要があります。 私はCallbackメソッドで検索していましたが、実際には値をチェックすることしかできません。私は依存関係注入を行うために検索したり、定義されたサービスをコールバックとして渡したりしましたが、それはあまり役に立ちません。Symfonyコールバックバリデータを作成する方法Doctrineは認識していますか?

質問は、要するに、それを達成することは可能ですか?どのように?

+1

Doctrineサービスを注入するカスタムのConstraintとValidatorが必要です。あなたはそれを試しましたか? –

+0

それを試したことはありません、私はそれについてのいくつかの文書を見つけましたが、私は必要なもののために少し複雑に思えます。それが解決策なら、私はこれを実装します –

+2

私はそれほど複雑ではありません。ここを見てください。 http://symfony.com/doc/current/validation/custom_constraint.htmlこの実装に問題がある場合は、お手伝いします。 –

答えて

3

@dragosteはコメントの中で、私自身の制約でそれを作り出す方法を探しました。

解決策はCustom Constraintを使用するようになっています。どのファイルを作成し、何をすべきかを知るのはちょっと面倒なので、ここでは私が行ったことがあります。

私のファイルが何であるかを説明するために、目的は、作成された方法ではなく、同じ瞬間に家賃がないことを確認することでした。だから私はその中でDoctrineの制約を使用しなければなりません。

バンドルのルート内にValidatorフォルダを作成します。次に、Validatorフォルダ内にConstraintsフォルダを追加します。

Validaor /制約フォルダ内のファイルRentDatesConstraint.phpを作成します。ここ は、それがどのように見えるかです:

<?php 

namespace ApiBundle\Validator\Constraints; 

use ApiBundle\Validator\RentDatesValidator; 
use Symfony\Component\Validator\Constraint; 

class RentDatesConstraint extends Constraint 
{ 
    public $message = 'The beginning and ending date of the rent are not available for this vehicle.'; // note that you could use parameters inside it, by naming it with % to surround it 

    /** 
    * @inheritdoc 
    */ 
    public function validatedBy() 
    { 
     return RentDatesValidator::class; // this is the name of the class that will be triggered when you need to validate this constraint 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function getTargets() 
    { 
     return self::CLASS_CONSTRAINT; // says that this constraints is a class constraint 
    } 
} 

今、あなたがあなた自身のクラスの制約を作成して、独自のバリデータを作成する必要があります。

RentDatesValidator.phpバリデータフォルダにファイルを作成します。

<?php 

namespace ApiBundle\Validator; 

use Doctrine\Bundle\DoctrineBundle\Registry; 
use Doctrine\Common\Collections\Collection; 
use Symfony\Component\Validator\Constraint; 
use Symfony\Component\Validator\ConstraintValidator; 

class RentDatesValidator extends ConstraintValidator 
{ 
    /** 
    * @var Registry $doctrine 
    */ 
    private $doctrine; 

    /** 
    * RentDatesValidator constructor. 
    * @param Registry $_doctrine 
    */ 
    public function __construct(Registry $_doctrine) 
    { 
     $this 
      ->setDoctrine($_doctrine) 
     ; 
    } 

    /** 
    * @param Registry $_doctrine 
    * @return $this 
    */ 
    public function setDoctrine(Registry $_doctrine) 
    { 
     $this->doctrine = $_doctrine; 
     return $this; 
    } 

    /** 
    * @inheritdoc 
    * @param Rent $_value 
    */ 
    public function validate($_value, Constraint $_constraint) 
    { 
     //do your stuff here 

     if ($testFails) { 
      $this 
       ->context 
       ->buildViolation($_constraint->message) // here you can pass an array to set the parameters of the string, surrounded by % 
       ->addViolation() 
      ; 
     } 
    } 
} 

我々はので、ここで我々はservices.yml内のリソース/ configに

services: 
    # [...] 
    validator.rent_dates: 
     class: ApiBundle\Validator\RentDatesValidator 
     tags: 
      - { name: validator.constraint_validator } 
     arguments: [ "@doctrine" ] 

を編集あなたは、私が合格したことをここに気づくことができ、我々はサービスとしてそれを宣言する必要があり、ほとんど完成しています@doctrineサービスですが、RentDatesValidatorクラスを適切に定義してそのコンストラクタでこれらのサービスを受け入れることができる限り、多くの場合でも、必要なサービスを実際に渡すことができます。

これで、検証でこれを使用するだけです。我々が行われ

ApiBundle\Entity\Rent: 
    constraints: 
     - ApiBundle\Validator\Constraints\RentDatesConstraint: ~ 

: ここではこれだけの行を追加するリソース/設定/検証Rent.ymlを編集!検証は、オブジェクトをバリデータサービスに渡すときに機能します。

これはYAMLで作成されていることに気付くことがありますが、個人的には各部分(エンティティ定義、データベーススキーマ、検証ファイルなど)を分離するためにこの方法をお勧めしますが、注釈、 XML、あるいは純粋なPHPです。それはあなた次第です。シンタックスをもっと見たいのであれば、これを行う方法を知るためにSymfonyドキュメンテーションのリンクに行くことができます。

関連する問題