10

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 フォームは「プロファイル」フォームと同じではありませんか? (登録フォームの設定も更新してみましたが)それともバリデーションルールを別の場所に設定しますか?

うまくいけば、私は何か小さなものを逃しただけです!

前もって感謝します、

4

1 に答える 1

10

UserAdmin フォームは、プロファイル フォームと同じではありません。


フォームが使用している検証グループを調べるには、symfony のプロファイラーを開きます

  1. フォームを開発環境 (app_dev.php) に投稿します。
  2. http://127.0.0.1:8000/app_dev.php/_profiler/を開く
  3. 最新の 10 件の提出物を表示します。
  4. フォームが送信された POST メソッドをクリックします (この場合は sonata user admin create form)。
  5. クリックフォーム
  6. Passed Options または Resolved Options に、どの validation_groups が設定されているかが表示されます。

カスタマイズされた検証ルールを SonataUserBundle の UserAdmin create または edit ユーザーに追加するには、次の手順を実行する必要があります。

https://github.com/sonata-project/SonataUserBundle/blob/master/Admin/Model/UserAdmin.phpを見る と、ユーザー管理者の validation_groups が登録 (新規) またはプロファイル (編集)。

2 つの選択肢があります。

A) 検証ルールをプロジェクトの validation.yml に追加し、既存の登録検証グループを使用するだけです。

Application\Sonata\UserBundle\Entity\User:
    properties:
        foo:
            - NotNull: { groups: [Registration, Profile] }

B) UserAdmin::getFormBulder() をオーバーライドして、カスタム検証グループを追加します

// In Application\Sonata\UserBundle\Admin\UserAdmin.php
public function getFormBuilder()
{
    $this->formOptions['data_class'] = $this->getClass();

    $options = $this->formOptions;
    $options['validation_groups'] = (!$this->getSubject() || is_null($this->getSubject()->getId())) ? ['CustomGroup','Registration'] : 'Profile';

    $formBuilder = $this->getFormContractor()->getFormBuilder( $this->getUniqid(), $options);

    $this->defineFormBuilder($formBuilder);

    return $formBuilder;
}
于 2015-10-25T18:59:47.267 に答える