我的动机是以此编辑形式显示的编辑值。但是,当我按编辑按钮时,它会引发此错误。我无法弄清楚。谁能帮助我的代码中缺少的内容?
在渲染模板的渲染过程中抛出了一个例外("有些 丢失了强制性参数("用户")以生成路由的URL " sokosimu_editor_edit_editoruser"。) sokoSimueditorBundle:用户:edituser.html.twig在第7行。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 %}
form
<?php
namespace SokosimuEditorBundleFormType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
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' => 'SokosimuUserBundleEntityUser'
// 'data_class' => NULL
));
}
public function getName()
{
return 'editUser';
}
}
您需要在路径中传递用户:
<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
));
修复路由
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()
));
}
在 @panche14的帮助下,我从 @panche14返回的对象中修改了bit.answer,但@param的表单@param预计为字符串或整数。
return $this->render('SokosimuEditorBundle:User:editUser.html.twig',
array('form' => $form->createView(),
'user' => $user ->getId();
));
也将其添加到树枝文件中:
{'用户':用户}
@panche14
指定现在,根据需要编辑按钮正常工作。