2012-03-29 51 views
0

我使用FOS用戶捆綁,我想重寫驗證文件FOS/UserBundle /資源/配置/ validaiton.xml:Symfony2的覆蓋模型驗證

<constraint name="FOS\UserBundle\Validator\Unique"> 
    <option name="property">usernameCanonical</option> 
    <option name="message">fos_user.username.already_used</option> 
    <option name="groups"> 
     <!-- <value>Registration</value> --> 
     <value>Profile</value> 
    </option> 
</constraint> 

用戶名不是我的報名表(我只是將它設置爲隱藏),這就是驗證不應產生任何錯誤......

也許有更好的方法來去除表格的用戶名...

回答

2

要刪除以適當的方式形式的用戶名字段,你應該重寫RegistrationFormType,創建自己的,並從原來的FOSUserBundle現在

<?php 
namespace Acme\UserBundle\Form\Type; 
use Symfony\Component\Form\FormBuilder; 
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType; 

class RegistrationFormType extends BaseType 
{ 
    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder->add('email', 'email') 
       ->add('plainPassword', 'repeated', array('type' => 'password')) 

       // your other custom fields, if any. 

    } 
} 

擴展它,你應該將被覆蓋的表單聲明爲服務,然後告訴FOSUserBundle配置文件您現在使用的是一個被覆蓋的表單。 Here is the complete documentation

+0

sry,這是行不通的。它仍然要求輸入用戶名。所以我認爲驗證仍然活躍。 – tester 2013-07-19 15:46:07

1

呦你可以把實體驗證信息放在任何validation.yml文件中。所以,你可以做

#validation.yml 
FQCN\Of\User\Entity: 
    constraints: 
    - FOS\UserBundle\Validator\Unique: 
     property: usernameCanonical 
     groups: [Profile] 
     message: fos_user.username.already_used 


    properties: 
    # property validations here