Reputation: 18660
I have a ExtraPerfilType
Symfony2 form with this content (irrelevant info was removed):
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';
}
}
And this is the function on BasicDataController.php
that build and display the form:
/**
* @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());
}
In basicData.html.twig
I have this HTML (irrelevant info was removed):
{{ 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) }}
Finally I have this in the function for handle the data and update the records on 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();
...
}
And here is where my problem come since var_dump
have this:
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 '[email protected]' (length=13)
'sitio_web' => string 'sadasdads' (length=9)
'_token' => string 'Wsfi7yJCrovMct-yphPuh_XRQcO4VRqBrcK1BZ89-jE' (length=43)
Since municipalities, parishes and city are not in the ExtraPerfilType
form because they are dependant combos and I fill them by hand using jQuery and of course consulting records on DB and they aren't in the request then how I handle them in order to update theirs values on the record? I'm using latest Symfony 2.5.1, any help or advice?
Request isn't handle right
After realize how to work with extra fields now the function I wrote for update the record at DB isn't handle $request
and I don't know where it's failing. This is the function:
/**
* @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));
}
This is the result:
{"status":true,"errors":{"persJuridica":[],"rif":[],"ci":[],"nombre":[],"apellido":[],"user":{"email":[],"password":[],"confirm":[]}}}
How do I remove those fields from the request since they are handle in other form?
PS: In some parts the code is in spanish since it's a software requirement sorry for that one I tried to set some parts on english for yours to understand
I've found the solution
Nevermind about latest edit, I've found the solution:
$builder
->remove('persJuridica')
->remove('rif')
->remove('ci')
->remove('nombre')
->remove('apellido')
->remove('email')
->remove('password')
->remove('confirm')
....
Upvotes: 1
Views: 1178
Reputation: 7800
suppose you want to get municipalities
value :
In controller you get it buy this :
$municipality = $request->get('municipalities');
Note that you could add fields to a symfony2 form that are not mapped to an entity
Like this :
$builder->add('extra_field_name','extra_field_type', array('mapped' => false)); // the option mapped is set to false
This way you could get your extra fields the way you mentioned in your question ( with $request->get("extra_user_profile"))
)
Upvotes: 2