0

Symfony2のエンティティの1つにCRUDがあります。新しいエントリを作成するために、2つのコントローラー機能があります。

public function newAction($id) {

    $entity = new Clientes();

    // Get the reference to the Login entity using its ID
    $em = $this->getDoctrine()->getManager();
    $ref_login = $em->getReference('LoginBundle:Login', $id);       

    // Put the retrieved reference to the entity
    $entity->setLogin($ref_login);

    $form = $this->createForm(new ClientesType(), $entity);

    return $this
            ->render('MovinivelBundle:Persona/Clientes:new.html.twig',
                    array('entity' => $entity,
                            'form' => $form->createView(),));
}

public function createAction(Request $request) {

    $entity = new Clientes();

    $form = $this->createForm(new ClientesType(), $entity);
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();               

        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('clientes'));
    }

    return $this
            ->render('MovinivelBundle:Persona/Clientes:new.html.twig',
                    array('entity' => $entity,
                            'form' => $form->createView(),));
}

前のコードでは、$ id入力パラメーターをnewAction()関数に追加しました。これは、このクライアントのそれぞれがログインの追加情報であり、リンクする必要があるため、外部から確立する必要があるためです。

ClientesTypeフォームには、次のものがあります。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('login')
        ->add('direccion')
        ->add('localidad')
        ->add('provincia')
        ->add('telefono')
    ;
}

これまでのところ、それは機能します。私のフォームでは、ログインパラメータは$id値に応じて選択されます。ただし、フォームの作成後にログインパラメータを修正したいので、ユーザーはフォームからログインパラメータを変更できず、適切な値を使用してnewAction($ id)関数を呼び出すだけです。

FormTypeの->add('login')行を削除すると、機能しなくなります。私の頭に浮かぶのは2つの選択肢です。

  • どういうわけかフォームの「ログイン」を非表示にしますが、方法はわかりませんが、機能し続けます。
  • $idパラメーターと$requestパラメーターを入力パラメーターとしてcreateActionに渡しますが、その方法もわかりません。

これについて何か考えはありますか?

4

2 に答える 2

0

hidden私はあなたがフィールドタイプを探していると思います:

public function buildForm(...)
{
    $builder
        ->add('login', 'hidden')
        // ...
    ;
}
于 2013-01-06T14:36:08.790 に答える
0

わかりました、私は解決策を思いつきました。私が探していたのは、実際には次のとおりです。

<div style="display:none">
{{ form_rest(form) }}
</div>

他のフォームフィールドを明示的に表示した後でテンプレートの最後にこれを入力すると、$ POSTメソッドを使用して情報を送信しながら、ユーザーが不要なフィールドを変更することを回避できます。

于 2013-01-18T12:36:07.673 に答える