Foros del Web » Programando para Internet » PHP » Symfony »

select dependiente

Estas en el tema de select dependiente en el foro de Symfony en Foros del Web. Buenas noches. Estoy haciendo un select dependiente, si selecciono un valor en el primer select entonces en el segundo debe cargarse con los datos relacionados ...
  #1 (permalink)  
Antiguo 07/08/2018, 23:04
 
Fecha de Ingreso: enero-2010
Ubicación: Colombia
Mensajes: 238
Antigüedad: 14 años, 2 meses
Puntos: 2
select dependiente

Buenas noches. Estoy haciendo un select dependiente, si selecciono un valor en el primer select entonces en el segundo debe cargarse con los datos relacionados pero cuando doy guardar al formulario, en el segundo select me sale
Código:
Este valor no es válido.
como si no tuviera valores o los valores cargados no fueran validos pero no se por que

Mi controlador:

Código PHP:
Ver original
  1. public function addAction()
  2.     {
  3.         $ataque = new Ataque();
  4.         $form = $this->createCreateForm($ataque);
  5.         return $this->render('PPPCanBundle:Ataque:add.html.twig', array('form' => $form->createView()));
  6.     }
  7.  
  8.     private function createCreateForm(Ataque $entity)
  9.     {
  10.         $form = $this->createForm(new AtaqueType(), $entity, array(
  11.             'action' => $this->generateUrl('ppp_ataque_create'),
  12.             'method' => 'POST'
  13.             ));
  14.         return $form;
  15.     }
  16.  
  17.     public function createAction(Request $request)
  18.     {
  19.         $ataque = new ataque();
  20.         $form = $this->createCreateForm($ataque);
  21.         $form->handleRequest($request);
  22.  
  23.         if($form->isSubmitted() && $form->isValid())
  24.          {
  25.             $em = $this->getDoctrine()->getManager();
  26.             $em->persist($ataque);
  27.             $em->flush();
  28.  
  29.             $this->addFlash('mensaje', 'Mascota creada correctamente');
  30.             return $this->redirectToRoute('ppp_ataque_index');
  31.          }
  32.  
  33.         return $this->render('PPPCanBundle:Ataque:add.html.twig', array('form' => $form->createView()));
  34.     }

Entidad:
Código PHP:
Ver original
  1. /**
  2.      * @var string
  3.      *
  4.      * @ORM\Column(name="departamento", type="string", length=255)
  5.      * @Assert\NotBlank()
  6.      */
  7.     private $departamento;
  8.  
  9.     /**
  10.      * @var string
  11.      *
  12.      * @ORM\Column(name="municipio", type="string", length=255)
  13.      * @Assert\NotBlank()
  14.      */
  15.     private $municipio;


Formulario:

Código PHP:
Ver original
  1. public function buildForm(FormBuilderInterface $builder, array $options)
  2.     {
  3.         $builder
  4.             ->add('departamento','choice', array('choices'=> array(
  5.                 'color' => 'color',
  6.                 'country' => 'country'
  7.                 ), 'placeholder' => 'Selectccione una opcion...'))            
  8.                    
  9. ->add('municipio', 'choice')


Vista add.html.twig

Código HTML:
Ver original
  1. <td>{{ form_label(form.departamento, ('Departamento')) }}
  2.  
  3. {{ form_widget(form.departamento, {'attr': {'class': 'form-control', 'id' : "primary"}}) }}
  4. <span class="text-danger">{{ form_errors(form.departamento) }}</span>
  5. </td>
  6.  
  7. <td>
  8.     {{ form_label(form.municipio, ('Municipio')) }}
  9.  
  10. {{ form_widget(form.municipio, {'attr': {'class': 'form-control'}}) }}
  11. <span class="text-danger">{{ form_errors(form.municipio) }}</span>



Archivo javascript que hace que cambie el segundo select dependiendo lo que se seleccione en el primero:

Código Javascript:
Ver original
  1. var options = {
  2.         color : ["red","green","blue"],
  3.         country : ["Spain","Germany","France"]
  4. }
  5.  
  6. $(function(){
  7.     var fillSecondary = function(){
  8.         var selected = $('#departamento').val();
  9.         $('#municipio').empty();
  10.         options[selected].forEach(function(element,index){
  11.             $('#municipio').append('<option value="'+element+'">'+element+'</option>');
  12.         });
  13.     }
  14.     $('#departamento').change(fillSecondary);
  15.     fillSecondary();
  16. });

Etiquetas: dependiente, select
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:13.