Добавяне на бойни единици
Здравейте,
Опитвам се да направя правенето на бойни единици, но нещо не мога да се оправя с формата, която трябва да се събмитне.
Как да направя, така че на всеки бутон "Arm units" да има по 1 форма, която да сочи към @Route("/armUnits", name="arm_units")
В момента ми се получава това:
В units/index.html.twig имам:
<h3>Barracks - arm more units</h3>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Name</th>
<th>Costs per 1 unit</th>
<th>Count</th>
<th>Add units</th>
</tr>
</thead>
<tbody>
{% for unit in units %}
{{ form_start(form) }}
<tr>
<td>{{ unit.name }}</td>
<td>
{% for unit_cost in unit.costs %}
<b>{{ unit_cost.resource.name }}</b>: {{ unit_cost.amount }}
{% endfor %}
</td>
<td>
{{ form_widget(form) }}
</td>
<td>
<button type="submit" class="btn btn-primary btn-xs">Arm units</button>
</td>
</tr>
{{ form_row(form._token) }}
{{ form_end(form) }}
{% endfor %}
</tbody>
</table>
UnitType:
class UnitType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('unit_count', IntegerType::class);
$builder->add('unit_id', HiddenType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Unit::class,
));
}
public function getName()
{
return 'kingdom_game_bundle_unit';
}
}
UnitsController:
class UnitsController extends KingdomCurrentController
{
/**
* @Route("/units", name="units")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction()
{
/** @var Kingdom $kingdom */
$kingdom = $this->getDoctrine()->getRepository(Kingdom::class)->find($this->getKingdomId());
$units = $this->getDoctrine()->getRepository(Unit::class)->findAll();
$unit = new Unit();
$form = $this->createForm(UnitType::class);
return $this->render('units/index.html.twig',
array(
'units' => $units,
'kingdom' => $kingdom,
'form' => $form->createView()
));
}
/**
* @Route("armUnits")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function armUnits(Request $request){
$kingdom = $this->getDoctrine()->getRepository(Kingdom::class)->find($this->getKingdomId());
$units = $this->getDoctrine()->getRepository(Unit::class)->findAll();
$unit = new Unit();
$form = $this->createForm(UnitType::class, $unit);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {...}
return $this->render('units/index.html.twig',
array(
'units' => $units,
'kingdom' => $kingdom,
'form' => $form->createView()
));
}
}
Да, има само 1 поле за брой единици, а трябва да са 2 - по 1 поле за всеки вид бойни единици.
пробвай вместо form.widget да бъде нормално текстово поле <input type="text" name="units[unit_count]" ....