Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Option does not exist error when passing through option from controller to embedded Symfony form

I get the following error when trying to pass through an option to my buildForm method in one of my forms.

The option "numOfHoles" does not exist. Defined options are: "action", "allow_extra_fields", "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", "label_format", "mapped", "max_length", "method", "pattern", "post_max_size_message", "property_path", "read_only", "required", "translation_domain", "trim", "validation_groups", "virtual".

In my controller:

// hardcoded here for brevity in this example
$form = $this->createForm('crmpicco_course_row', $courseRow, array('numOfHoles' => 18));

In crmpicco_course_row Form class:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text')
        ->add('course', 'crmpicco_course', array('numOfHoles' => $options['numOfHoles']))
    ;
}

In crmpicco_course Form class:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    for ($i = 0; $i < $options['numOfHoles']; $i++) {
        $builder->add($i, 'text', array('required' => 'false'));
    }
}

/**
 * @return string name
 */
public function getName()
{
    return 'crmpicco_course';
}

Can anyone see why the option numOfHoles is not pulling through?

like image 436
crmpicco Avatar asked Oct 26 '15 10:10

crmpicco


1 Answers

As you have discovered, each form type has a predefined list of options. Adding a new option requires a slight adjustment. The actual method has changed over the course of Symfony development so you may come across some older depreciated solutions.

The most up to date solution is discussed here: http://symfony.com/blog/new-in-symfony-2-7-form-and-validator-updates#deprecated-setdefaultoptions-in-favor-of-configureoptions

So basically add

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Whatever',
        'numOfHoles' => 0,
    ));

To your form types and you should be good to go.

like image 51
Cerad Avatar answered Oct 12 '22 03:10

Cerad