缺少一些必需参数(“用户”)来生成路由的URL

时间:2016-12-02 10:26:08

标签: symfony doctrine-orm symfony-forms

我的动机是编辑此编辑表单中显示的值。但是,当我按下编辑按钮时,它会抛出此错误。我无法弄清楚。任何人都可以帮助我的代码中缺少什么吗?

  

在渲染模板期间抛出了异常(“Some   缺少必需参数(“user”)以生成路由的URL   “sokosimu_editor_edit_editoruser”。“)in   SokosimuEditorBundle:用户:第7行的editUser.html.twig。500内部   服务器错误 - Twig_Error_Runtime

路由器

sokosimu_editor_edit_editoruser:
    path: /edit/editoruser/{user}
    defaults: {_controller:SokosimuEditorBundle:Editor:editEditorUser}
    requirements:
            _method:  GET|POST

控制器

    public function editEditorUserAction(User $user,Request $request){

    $form = $this->createForm(new EditUserType(),$user);

    //2. handle the submit (will happen on POST)
    $form->handleRequest($request);
    if($form ->isValid() && $form->isSubmitted()){
    }
    $em = $this->get('doctrine')->getManager();

    $editUser = $user ->getEditoruser();
    return $this->render('SokosimuEditorBundle:User:editUser.html.twig', array(
        'form' => $form->createView()
    ));

}

查看

 {% block title %}Edit User{% endblock%}

 {% block body %}

<form action="{{ path('sokosimu_editor_edit_editoruser') }}" method="post" {{ form_enctype(form) }} class="formedit">
    {{ form_errors(form) }}

    {{ form_row(form.alias)}}
    {{ form_row(form.email) }}
    {#{{ form_row(form.password) }}#}
    {{ form_row(form.mobile) }}
    {{ form_row(form.submit) }}
    {{ form_rest(form) }}
  </form>
 {% endblock %}

表格

<?php

 namespace Sokosimu\EditorBundle\Form\Type;

 use Symfony\Component\Form\Extension\Core\Type\SubmitType;
 use Symfony\Component\Form\AbstractType; 
 use Symfony\Component\Form\FormBuilderInterface;
 use Symfony\Component\OptionsResolver\OptionsResolverInterface;

 class EditUserType extends AbstractType
 {

   public function buildForm(FormBuilderInterface $builder, array $options)
  {
     $builder->add('alias','text',array('required'=>false));
     $builder->add('email', 'email',array('required'=>true));
    // $builder->add('password','password',array('required'=>true));
     $builder->add('mobile','text',array('required'=>false));
     $builder->add('submit', 'submit');
  }

   public function setDefaultOptions(OptionsResolverInterface   $resolver)
  {
    $resolver->setDefaults(array(
        'data_class' => 'Sokosimu\UserBundle\Entity\User'
     //   'data_class' => NULL
    ));
}

public function getName()
 {
   return 'editUser';
 }
}

3 个答案:

答案 0 :(得分:1)

您需要在路径中传递用户:

<form action="{{ path('sokosimu_editor_edit_editoruser', {'user': user}) }}" method="post" {{ form_enctype(form) }} class="formedit">

在控制器中用用户渲染树枝:

return $this->render('SokosimuEditorBundle:User:editUser.html.twig', array(
    'form' => $form->createView(),
    'user' => $user
));

答案 1 :(得分:0)

修复路线

sokosimu_editor_edit_editoruser:
    path: /edit/editoruser/{userId}
    defaults: {_controller:SokosimuEditorBundle:Editor:editEditorUser}
    requirements:
        _method:  GET|POST

将控制器修复为

public function editEditorUserAction(Request $request, $userId)
{
    $user = $this->getDoctrine()->getRepository('SokosimuEditorBundle:User')->find($userId);
    $form = $this->createForm(new EditUserType(), $user);

    //2. handle the submit (will happen on POST)
    $form->handleRequest($request);

    if ($form->isValid() && $form->isSubmitted()) {
        $em = $this->get('doctrine')->getManager();

        $editUser = $user->getEditoruser();
    }

    return $this->render('SokosimuEditorBundle:User:editUser.html.twig', array(
        'form' => $form->createView()
    ));
}

答案 2 :(得分:0)

在@ panche14的帮助下,我对代码进行了一些修改。来自@ panche14的答案返回了对象,但@param形式应该是字符串或整数。

return $this->render('SokosimuEditorBundle:User:editUser.html.twig', array('form' => $form->createView(), 'user' => $user ->getId(); ));

还要在twig文件中添加:

  

{&#39;用户&#39;:用户}

由@ panche14

指定

现在,编辑按钮可以正常工作。