mardi 31 mars 2015

Symfony2 - dynamically added option in choice, form validation showing field value to null

I want to build a form with choice option, which values will add using ajax and it's working like i want but after submitting form symfony2 showing choice option value to null ?


I tried and successfully added dynamically from generation like cookbook shows - http://ift.tt/1bEX27C


but in my current problem, my field in not depend on other option like described in cookbook - sport-position is depend on sport but in my case option is one and without choices initially and then user add options using ajax ..


i tried this and others - Symfony2 - Dynamic form choices - validation remove but these will work when choice is based on other form option, in my case it depend on itself.


my from class code is this - here

'seachText' is text box using for ajax request and then i added selected option to 'college' choice after selection by user but then after submitting form 'POST_SUBMIT' / 'PRE_BIND', i get $event->getForm()->getData()->getCollege() = null :(


college - entity is like symfony2 default declaration with getter and setter

collegeText - public -no db use, just for ajax field


view image - http://ift.tt/1xTgU7S



<?php

namespace MyBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class SomethingForm extends AbstractType
{
private $container;

public function __construct(ContainerInterface $container){
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{

$builder->add('searchText','text', array(
'label' => 'College',
'required' => false,
)
);

$formModifier = function (FormInterface $form, $college = null) {
$form->add('college', 'choice', array(
'label' => 'College',
'choices' => array(),
'multiple' => true
)
);
};

$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
// this would be your entity, i.e. getCollege
$data = $event->getData();

$data ? ($data->searchText = $this->container->get('my.service')->getCollegeBasedOnId($data->getCollege())) : '';

$formModifier($event->getForm(), null);
}
);

$builder->addEventListener(
FormEvents::PRE_BIND,
function (FormEvent $event) use ($formModifier) {

//here i am getting null if i added values using ajax
var_dump($event->getForm()->getData());
die();



$college = $event->getForm()->getData();
$formModifier($event->getForm()->getParent(), $college);
}
);
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MyBundle\Entity\Somthing',
));
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'something';
}
}

Aucun commentaire:

Enregistrer un commentaire