1

プロジェクトに FOSUserBundle を正常にインストールし、すべてが期待どおりに機能します。しかし、実際のプロジェクトでそれを実装する方法に苦労しています。

次のセットアップを作成したいと思います。

  • 1 つのフォーム (ニュースレターの購読など) でいくつかのユーザー設定を表示するページ、2 つ目のフォームでパスワードを変更する可能性、および場合によっては 3 つ目のフォームでユーザー名を変更する可能性もあります。

  • 設定フォームとその他の情報は、コントローラーの既存のアクションから取得されており、うまく機能しています。

私はいくつかのことを試しましたが、まだうまくいきません:

  • FOSUserBundle\Controller\ChangePasswordController\changePasswordAction()いくつかの機能を自分のアクションにコピーしました。このようにして、パスワード変更フォームを取得し、ビューを作成してテンプレートに渡すことができました。

  • フォームをテンプレートに追加しました{{ form_widget(form) }}。フォームが表示されており、機能しています。パスワードを変更できます。ただし、ラベルは失われておりCurrent、 、First、および と表示されていSecondます。また、2 つの新しいパスワードが一致しない場合や空のままになっている場合にも、エラー メッセージは表示されません。

全体として、私はおそらくこれを間違った方法で行っていると感じています。このタスクをどのように処理すればよいか、どこで愚かなことをしている可能性があるかを指摘していただけませんか?

これが私のアクションのコードであり、ここで重要なものに縮小されています。

# src/Acme/MyBundle/Controller/BackendController.php
public function accountAction(){

    //pretty much a copy of FOSUserBundle\Controller\ChangePasswordController\changePasswordAction()
    $user = $this->get('security.context')->getToken()->getUser();
    $form = $this->container->get('fos_user.change_password.form');
    $formHandler = $this->container->get('fos_user.change_password.form.handler');

    $process = $formHandler->process($user);
    if ($process) {
       //password has been changed, response will be generated
    }

    //more stuff going on here
    $moreStuff = ...

    //render view
    return $this->render('AcmeMyBundle:Backend:account.html.twig', array(
        'form' => $form->createView(),
        'moreStuff' => $moreStuff
    ));
}
4

1 に答える 1

0

IMO rendering more than one form in one action is not a good idea. Always try to separate things and let an action handle only one feature.

In your twig template I suggest to use the render method :

{% render 'AcmeBundle:SomeAction' with{'param:param} %}

It will generate a GET request on the action provided with some params if needed.

Create one action that will render the twig template with subrequests :

// AcmeUserBundle:editAction

{% render 'AcmeUserBundle:changePasswordAction' %}
{% render 'AcmeUserBundle:settingsAction' %}
{% render 'AcmeUserBundle:profileAction' %}

And then you'll need to create one action per form.

For password and username modification you can also override FOSUserBundle views if your needs are only visual. If you need to add/remove a field on the form you will need to create a new service.

I sugget reading FOSUserBundle documentation about overriding :

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#next-steps

于 2012-09-10T07:54:33.827 に答える