Starten mit Symfony ist ziemlich eine Lernkurve. Auch nach stundenlanger Lektüre kann ich dieses vermeintlich einfache Problem nicht nachvollziehen. Ich möchte ein Auswahlformular mit Werten aus einer Entität laden.Symfony: Zugriff auf Controller-Variablen aus Formular
Controller:
namespace AppBundle\Controller
class ItemController extends Controller
{
public function itemAction (Request $request)
{
$myItems = new Itemlist();
//some statements to fill $myItems
$form = $this->createForm (AllitemsType::class, $myItems);
// some more stuff
return $this->render (...);
}
}
Entity:
namespace AppBundle\Entity;
class Itemlist
{
protected $choices;
protected $defaultvalue;
public function __construct()
{
$choices = array();
}
// all the get and set-methods to fill/read the $choices array and $defaultvalue
}
Form:
namespace AppBundle\Form
class AllitemsType extends AbstractType
{
public function buildForm (FormBuilderInterface $builder, array $options)
{
// and here is my problem: how can I fill next two lines with values from the Itemlist-Entity?
// The Itemlist instance has been build in the controller and is unknown here
$items = ??? // should be 'AppBundle\Entity\Itemlist->$choices
$defaultitem = ??? // should be 'AppBundle\Entity\Itemlist->$defaultvalue
$choices_of_items = array (
'choices' => $items,
'expanded' => true,
'multiple' => false,
'data' => $defaultitem,
);
$builder->add ('radio1', ChoiceType::class, $choices_of_items);
}
}
Jede Hilfe dankbar, Wolfram
Ihre "best practice" -Antwort löst zwar die Auswahl, füllt aber nicht das Attribut 'data', korrekt? Und Entschuldigung, ich nehme nicht die anderen beiden optionalen Antworten. Trotzdem vielen Dank für Ihren Beitrag. – Wolfram
@wolfram Sorry, ich habe die Datenoption vergessen, aber es ist die gleiche Methode, die ich für die Auswahl verwende – DonCallisto