8 votes

How to change the form value field from the controller in Symfony2?

I am developing a booking app and I'm saving from a form when the user selects a radio button, the array of the hours is the step in the following way:

    $form = $this->createFormBuilder($reserva)
            ->add('horaReserva', 'choice', [
                'choices'=> $horasDisponibles,
                'expanded' => true,
                'multiple' => false,

                ]
            )

In the template twig what I show in the following way:

{{ form_widget(form.horaReserva, {'attr': {'class': 'form-control'}}) }}

And this is what is shown on the web

<input type="radio" id="form_horaReserva_0" name="form[horaReserva]" required="required" value="0"> <label for="form_horaReserva_0" class="required">9:00</label>

The case is that when you save it in the database I save it with a 0 value, there would be a way to change the field value from the controller and the template so that the value field has as its value the hour?

Here are the available hours:

Formulario

By which the sending of the data on the array is done correctly and displays the data well. But when rendering the template adds a field value=0 and that is what is saved in the database when the form is submitted

BBDD

This is the result when you save it ,you should save the time however saves the position of the radio button.

1voto

elAIR Points 31

I solved it when in value appeared 0,1,2 for the positions of each element of the array I solved it creating an auxiliary array and combining it in this way:

 $horasClaveValor=array_combine($horasDisponibles, $horasDisponibles); 
 

That way I will always have the same values in a dynamic way, both in terms of key and value.

Thanks for the help.

0voto

Rene Arias Points 16

Within the entity

     $hora_reserva;
    public function __construct()
    {    
        $this->hora_reserva=new DateTime(); 
    }
 

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X