2013-05-22 11 views
11

FOUserBundleでは、ユーザ名やパスワードなどのフィールドで最小長、最大長、空白でない検証設定を変更したいと考えています。FOSUserBundle - ユーザ名、パスワード、電子メールフィールドの検証

私はカスタムフィールドに@Assertでいくつかの検証を行うことができましたが、今ではFOSUserBundleのユーザー名検証をどのように変更できるのでしょうか?

これらのフィールドは自動的に生成されるため、Userエンティティに追加することはできません。デフォルトでは、{^|などの文字を使用できます。

答えて

12

バンドルに新しい検証ファイルを作成して、デフォルト設定を上書きすることができます。これはバンドルの継承です。ちょうどコピー(カットしない)。

FOSUserBundle/Resources/config/validation/orm.xml~YourBundle/Resources/config/validation/orm.xml。 (それぞれcouchdb.xmlmongodb.xmlpropel.xml

し、あなたのニーズに合わせて調整します。あなたの制約を追加し、クラス名を変更します。

<class name="Vendor\YourBundle\Model\User"> 

    <property name="username"> 
     <!-- minimum length for username --> 
     <constraint name="MinLength"> 
      <option name="limit">3</option> 
      <option name="message">Your name must have at least {{ limit }} characters.</option> 
     </constraint> 
     <!-- custom constraint --> 
     <constraint name="Acme\DemoBundle\Validator\Constraints\ContainsAlphanumeric" /> 
    </property> 

    <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity"> 
     <option name="fields">usernameCanonical</option> 
     <option name="errorPath">username</option> 
     <option name="message">fos_user.username.already_used</option> 
     <option name="groups"> 
      <value>Registration</value> 
      <value>Profile</value> 
     </option> 
    </constraint> 

    <constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity"> 
     <option name="fields">emailCanonical</option> 
     <option name="errorPath">email</option> 
     <option name="message">fos_user.email.already_used</option> 
     <option name="groups"> 
      <value>Registration</value> 
      <value>Profile</value> 
     </option> 
    </constraint> 
</class> 

は制約がValidation Constraints Referenceで利用可能(とどのようにXML設定でそれらを使用する)である詳細についてはこちらをご覧ください。 YMLで

+0

申し訳ありません、ありがとう!私はorm.xmlファイルを追加する必要もなく、Validation Constrains Referenceを正しく読んだ後、私はUserBundleのconfigにvalidation.ymlを追加してそこに追加することができたことを知りました:D –

+0

それは可能です一般的に、FOSUserBundleのxmlファイルをコピー&ペーストで使用する方が速くなります。これは、すでに骨格を提供しています:) – nifr

+1

@M。 Winterinhoもあなたのソリューションを追加していただけますか? –

3

あなたはこのようにそれを行うだろう:私のための最も簡単な方法は、私のカスタムエンティティ内のプロパティを上書きし、アサーションの設定を変更した

# src/Acme/ProjectBundle/Resources/config/validation.yml 
Acme\ProjectBundle\Entity\User: 
    properties: 
     email: 
      - Length: 
       min: 5 
       minMessage: "Your email must have at least {{ limit }} characters." 
       max: 255 
       maxMessage: "Your email is too long." 
      - NotBlank: 
       message: "Please enter an email" 
     username: 
      - Length: 
       min: 6 
       minMessage: "Your username must have at least {{ limit }} characters." 
       max: 255 
       maxMessage: "Your username is too long." 
      - NotBlank: 
       message: "Please enter an username" 
     plainPassword: 
      - Length: 
       min: 8 
       minMessage: "Your password must have at least {{ limit }} characters." 
       max: 255 
       maxMessage: "Your password is too long." 
      - NotBlank: 
       message: "Please enter a password" 

Acme\ProjectBundle\Form\Model\ChangePassword: 
    properties: 
     new: 
      - Length: 
       min: 8 
       minMessage: "Your password must have at least {{ limit }} characters." 
       max: 255 
       maxMessage: "Your password is too long." 
      - NotBlank: 
       message: "Please enter a password" 

Acme\ProjectBundle\Form\Model\ResetPassword: 
    properties: 
     new: 
      - Length: 
       min: 8 
       minMessage: "Your password must have at least {{ limit }} characters." 
       max: 255 
       maxMessage: "Your password is too long." 
      - NotBlank: 
       message: "Please enter a password" 
+0

あなたは "ユニークな"制約 –

+0

と電子メールに '電子メール'制約を再追加するのを忘れていることに注意してください –

4

を:

<?php 
namespace YourBundle\Entity; 

use Symfony\Component\Validator\Constraints as Assert; 

class User extends \FOS\UserBundle\Model\User 
{ 
    // ... 

    /** 
    * @Assert\Length(
    *  min=8, 
    *  max=4096, 
    *  minMessage="user.password.short", 
    *  groups={"Profile", "ResetPassword", "Registration", "ChangePassword"} 
    *) 
    */ 
    protected $plainPassword; 

    // ... 
} 

ドン検証グループを忘れないでください。

ヘッドアップ:user.password.short翻訳はvalidatorsにあります。YourBundle\Resources\translations\のバンドルのドメイン。

例:これは、特定のバージョンがある場合

# validators.en.yml 
user: 
    password: 
     short: Password must be at least 8 characters long. 

は知ってはいけません。私はsymfony v2.8.3とfosuser [email protected](a39d000)を使用しています。

関連する問題