Symfony 2 Object Entity Custom Choice Selected Value Sorunu

Reading time ~1 minute

Object türü Entity ile birlikte Choice formu kullanırken selected olan veriyi Form otomatik olarak işlemiyor. Bu sorunun çözümü işin aslında bir hayli kolay ancak konu ile ilgili bir kaynak bulmak imkansız.

$choice1 = new Choice();
$choice1->setName('Yok');
$choice1->setValue(0);

$choice2 = new Choice();
$choice2->setName('Var');
$choice2->setValue(1);

$resolver->setDefaults(array(
            'choices' =>  array(
                $choice1,
                $choice2
            ),
            'choices_as_values' => true,
            'choice_label' => function($choice, $key, $index) {
                /** @var Choice $choice */
                return $choice->getName();
            },
            'choice_value' => '__value__',
        ));

Sorunu çözümü choice_value kısımında. Bu ayara ‘value’ değerini verdiğinizde Data Class’ınızın “getValue” methodundan dönen değeri alıyor.

Symfony ile Doctrine'de Random Entity

Symfony 2 Object Field Entity Custom Choice'un seçilen verisini alma sorunu. Continue reading

Daha Hızlı Web - Asenkron Javascript

Published on September 25, 2015