I'm encountering some problem with Symfony 2.5 i've the need to dinamicaly modify a form based on some parameters that i've to pass from the controller but i cant initializate the $options array to the task.
My Files:
// ordersContrroller.php
$orders=new OrdersM();
$form = $this->createForm(new ListOrdersType(), $orders, array('id'=>1));
// ListOrdersType.php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\FormEvents;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ListOrdersType extends AbstractType
{
public function getDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\MyBundle\Form\Model\OrdersM',
'id'=>1,
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
var_dump(array_keys($options));
$builder->add('orders', 'entity', array(
'class' => 'AcmeMyBundle:Orders',
'property' => 'name',
'query_builder' => function($repository) use ($options) {
return $repository->createQueryBuilder('o')
->where('o.id='.$options['id'])
->orderBy('o.id', 'ASC' )
;
}
));
}
public function getName()
{
return 'orders';
}
}
I've TWO distinct problems. When i put the options array in che controller array('id'=>1)
i recive the following error:
The option "id" does not exist. Known options are: "action", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "validation_groups", "virtual"
Like the option is not declared during the declaration of the defaults options of the resolver.
And ofcorse when i try to use the $options array on the key $option['id'] on the query_builder function i get the following error:
Notice: Undefined index: id in ****/Form/Type/ListOrdersType.php line 56
that ofc is this one:
->where('o.id='.$options['id'])
The id $option is ony one , but i realy need to make my query in the EntityType.php files dinaic following differents parameters, i realy need to pass them over options in some way.
What i'm missing? Tx for your time.
setParameter(). - sjagr