我有一个包含此内容的ExtraPerfilType
Symfony2表单(已删除不相关的信息):
class ExtraPerfilType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('estado', 'entity', array(
'class' => 'CommonBundle:SysEstado',
'property' => 'nombre',
'required' => true,
'label' => 'Estado',
'empty_value' => 'Seleccione un estado',
'attr' => array(
'class' => 'estados'
)
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array(
'data_class' => 'Sunahip\UserBundle\Entity\SysPerfil',
'intention' => 'extra_user_profile'
)
);
}
public function getName()
{
return 'extra_user_profile';
}
}
这是BasicDataController.php
上构建和显示表单的函数:
/**
* @Route("/basicdata", name="basicdata")
* @Template()
*/
public function basicDataAction()
{
$em = $this->getDoctrine()->getEntityManager();
$id = $this->getUser()->getId();
$entity = $em->getRepository('UserBundle:SysPerfil')->findOneBy(array("user" => $id));
$form = $this->createForm(new ExtraPerfilType(), $entity, array('action' => $this->generateUrl('basicdata-save')));
return array('entity' => $entity, 'form' => $form->createView());
}
在basicData.html.twig
我有这个HTML(不相关的信息被删除):
{{ form_start(form, {'attr': {'id':'form_extra_perfil'}}) }}
{{ form_label(form.state) }} <span class="oblig">(*)</span>
{{ form_widget(form.state) }}
<select class="municipalities" name="municipalities" id="municipalities" disabled="disabled">
<option value="-1">-- Pick one --</option>
<span class="oblig">(*)</span>
</select>
<select class="parishes" name="parishes" id="parishes" disabled="disabled">
<option value="-1">-- Pick one --</option>
</select>
<select class="city" name="city" id="city" disabled="disabled">
</select>
<div><input type="submit" id="form_btttn" value="Guardar"></div>
{{ form_widget(form._token) }}
{{ form_errors(form) }}
{{ form_end(form) }}
最后,我在函数中处理数据并更新DB上的记录:
/**
* @Route("/basicdata/save", name="basicdata-save")
* @Method("POST")
*/
public function guardarAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$id = $this->getUser()->getId();
var_dump($request->get("extra_user_profile"));
die();
...
}
自var_dump
以来我的问题就出现了:
array (size=12)
'estado' => string '7' (length=1)
'calle' => string 'asdasd' (length=6)
'apartamento' => string 'asdasdasd' (length=9)
'apartamento_no' => string 'asdasda' (length=7)
'referencia' => string 'sdasdas' (length=7)
'codigo_postal' => string 'asdasdas' (length=8)
'fax' => string 'dasdasd' (length=7)
'telefono_local' => string 'asdasdasd' (length=9)
'telefono_movil' => string 'asdasdasd' (length=9)
'correo_alternativo' => string 'sdsad@asd.com' (length=13)
'sitio_web' => string 'sadasdads' (length=9)
'_token' => string 'Wsfi7yJCrovMct-yphPuh_XRQcO4VRqBrcK1BZ89-jE' (length=43)
由于市政,教区和城市不是ExtraPerfilType
形式,因为它们是依赖组合,我用jQuery手工填写它们,当然还有DB上的咨询记录,它们不在请求中,那我怎么样处理它们以更新记录中的值?我正在使用最新的Symfony 2.5.1,任何帮助或建议?
请求处理不正确
在实现了如何使用额外字段之后,我为DB更新记录而编写的函数不是句柄$request
,我不知道它在哪里失败。这是功能:
/**
* @Route("/basic-data/update", name="basic-data-update")
* @Method("POST")
*/
public function updateAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$id = $this->getUser()->getId();
$entityProfile = $em->getRepository('UserBundle:SysPerfil')->findOneBy(array("user" => $id));
$formProfile = $this->createForm(new PerfilType(), $entityProfile);
$formProfile->handleRequest($request);
if ($formProfile->isValid()) {
$municipalities= $em->getRepository('CommonBundle:SysMunicipio')->findOneBy(array("id" => $request->get('municipios')));
$parishes = $em->getRepository('CommonBundle:SysParroquia')->findOneBy(array("id" => $request->get('parroquias')));
$city= $em->getRepository('CommonBundle:SysCiudad')->findOneBy(array("id" => $request->get('ciudades')));
$entityProfile->setMunicipio($municipalities);
$entityProfile->setParroquia($parishes );
$entityProfile->setCiudad($city);
$em->flush();
return $this->redirect($this->generateUrl('datos-basicos'));
}
else {
$errors = $this->getFormErrors($formProfile);
}
return new JsonResponse(array('status' => true, 'errors' => $errors));
}
结果如下:
{"status":true,"errors":{"persJuridica":[],"rif":[],"ci":[],"nombre":[],"apellido":[],"user":{"email":[],"password":[],"confirm":[]}}}
如何从请求中删除这些字段,因为它们是以其他形式处理的?
PS:在某些部分,代码是西班牙语,因为它是一个软件要求,对不起,我试图在英语上设置一些部分供你理解我找到了解决方案
没关系最新的编辑,我找到了解决方案:
$builder
->remove('persJuridica')
->remove('rif')
->remove('ci')
->remove('nombre')
->remove('apellido')
->remove('email')
->remove('password')
->remove('confirm')
....
答案 0 :(得分:2)
假设您想获得municipalities
值:
在控制器中你可以买到它:
$municipality = $request->get('municipalities');
请注意,您可以将字段添加到未映射到实体的symfony2表单
像这样:
$builder->add('extra_field_name','extra_field_type', array('mapped' => false)); // the option mapped is set to false
通过这种方式,您可以按照问题中提到的方式获取额外字段(使用$request->get("extra_user_profile"))
)