prezequias prezequias - 27 days ago 5
Twig Question

Using multiple dropdown lists to fill one column on db using PostType Form in Symfony 3

I have the following situation:
I have two dropdown lists which both values need to be stored on the same db colum. My question is how can I work this out on my buildForm method as well as on html.twig?
I'd like to thank you all for your help.
Here are my codes:

Entity

* @var string
*
* @ORM\Column(name="Diningroom", type="text")
*/
private $diningroom;
/**
* @var string
*
* @ORM\Column(name="Kitchen", type="text")
*/
private $kitchen;
/**
* @var string
*
* @ORM\Column(name="Office", type="text")
*/
private $office;
/**
* @var string
*
* @ORM\Column(name="BedRoom", type="text")
*/
private $bedroom;


buildForm

->add('firstName')
->add('lastName')
->add('email')
->add('telephone')
->add('county')
->add('city')
->add('street')
->add('postcode')
->add('floor', 'entity')
->add('elevator', 'entity')
->add('diningroom')
->add('kitchen')
->add('office')
->add('bedroom')
->add('bathrooms')
->add('boxes')
->add('message')
->add('packing')
->add('personrequired')
->add('submit', SubmitType::class, array(
'label' => 'Get Quotation'
// 'class' => 'btn btn-danger pull right'
))


Controller

public function createAction(Request $request)
{
//create a new post
$removalPost = new Post();
$form = $this->createForm(PostType::class, $removalPost, [
'action' => $request->getUri()
]);

$form->handleRequest($request);

if($form->isValid()){
$em = $this->getDoctrine()->getManager();

$removalPost->setAuthor($this->getUser());
$removalPost->setDate(new \DateTime());
$em->persist($removalPost);
$em->flush();
return $this->redirect($this->generateUrl('RemovalsUK_view',
['id' => $removalPost->getId()]));
}
return $this->render('RemovalsUKBundle:Post:create.html.twig', array
('form' => $form->createView()
));
}


My HTML

<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="form-group">
<label>Table</label>
<select class="form-control required" name="kitchen_table" id="kitchen_table">
<option value="" selected>Quantity</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="form-group">
<label>Seat</label>
<select class="form-control required" name="kitchen_seat" id="kitchen_seat">
<option value="" selected>Quantity</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</div>
</div>


What I want to do is for instance, is insert into kitchen column the values (Quantity) from Table and Seat
I would appreciate any any to sort this matter out. Thanks

Answer Source

i would say create getter for kitchen_table and kitchen_seat to fill the form. For save is the question in which format do you want to save?

$data = $form->getData(); 
$removalPost->setKitchen($data['kitchen_table'].'_'.$data['kitchen_seat']);

and this should set. But you also have an issue by setting up your form. the Form should fit with your fields otherwise you cannot get the data via the form, you than have to get the data via the request...