I have installed SonataUserBundle according to the docs and it all works fine. Except that I cannot add custom validation rules.
My understanding is that the new rules should be added to a new Validation Group and then config.yml is updated to tell SonataUserBundle (or FosUserBundle) to add the new rules to the validation sequence.
I have tried this, in various ways, but the new rules just don't seem to be picked up at all...
Here's the configuration I'm using...
(For the sake of this example, I'm just trying to adding a NotNull constraint to a new foo field. In reality I would like to see this much work and then add more validation rules.)
foo フィールドを追加しましたがApplication\Sonata\UserBundle\Resources\config\doctrine\User.orm.xml
、すべて正常に動作し、foo フィールドをUser.php
クラスに追加しました。
# in Application\Sonata\UserBundle\Resources\config\doctrine\User.orm.xml
...
<field name="foo" type="string" length="100" nullable="true" />
...
User.php には、getter と setter を含むプロパティがあります。
// In Application\Sonata\UserBundle\Entity\User.php
// ...
/**
* @var string
*/
private $foo;
/**
* Set foo
*
* @param string $foo
* @return User
*/
public function setFoo($foo)
{
$this->foo = $foo;
return $this;
}
/**
* Get foo
*
* @return string
*/
public function getFoo()
{
return $this->foo;
}
// ...
次に、プロジェクトの既存の validation.yml ファイルに新しい検証ルールを追加しました。
Application\Sonata\UserBundle\Entity\User:
properties:
foo:
- NotNull: { groups: [CustomGroup] }
( Application\Sonata\UserBundle\Resources\config に validation.yml および validation.xml ファイルを作成しようとしたことにも注意してください。ただし、違いはないようです。)
config.yml で、新しい CustomGroup を検証に使用するよう SonataUserBundle に指示します。
sonata_user:
# ...
profile:
form:
validation_groups: [CustomGroup, Profile, Default]
(また、fos_user レベル ( fos_user.profile.form.validation_groups: [CustomGroup, Profile, Default]
) で検証グループを追加し、 を追加しようとしましsonata_user.profile.register.form.validation_groups: [CustomGroup, Registration, Default]
たが、役に立たないことに注意してください。)
そして、完全を期すために、UserAdmin.php に追加されたフィールドを次に示します。
// In Application\Sonata\UserBundle\Admin\UserAdmin.php
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
// ...
->add('foo', null, array('required' => false))
->end()
;
}
それで... 私は何を逃したのですか?UserAdmin フォームは「プロファイル」フォームと同じではありませんか? (登録フォームの設定も更新してみましたが)それともバリデーションルールを別の場所に設定しますか?
うまくいけば、私は何か小さなものを逃しただけです!
前もって感謝します、
ハ