Sonataadminbundle: Adding button to admin form throwing error

Created on 23 Jun 2015  路  5Comments  路  Source: sonata-project/SonataAdminBundle

Hey!

In

protected function configureFormFields(FormMapper $formMapper)

I have added the following line in the $formMapper

->add('show_games', 'button',
    array('label' => 'Show Games', 'required' => false, 'mapped' => false)
)

This gives me this error:

The options "label_render", "mapped", "required", "sonata_field_description" do not exist. Defined options are: "attr", "auto_initialize", "block_name", "disabled", "label", "label_format", "translation_domain"

This is coming from a symfony file

in vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php at line 761

but what is causing this problem is that line with the button. Am I not able to add buttons in the forms?

Most helpful comment

The problem is related to the fact that sonata set default option (label_render, mapped) for all "form" type. But reading the documentation of symfony the button are not part of the "form" type.

A work around that I am building is to create do a custom extension for button that just configuration the option resolver.

class AdminButtonTypeExtension extends AbstractTypeExtension
{
    public function __construct()
    {

    }

    public function getExtendedType()
    {
        return 'button';
    }

    /**
     * Add the image_path option
     *
     * @param OptionsResolver $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            [
                'sonata_field_description' => null,
                'label_render' => null,
            ]
        );
    }

    /**
     * Add the image_path option
     *
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $this->setDefaultOptions($resolver);
    }
}

Configure your extension alias to be "button"
Just follow those instruction: http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html

I am not done completly so I am not sure it will fix everything but just add up the needed options.

Hope it help but I am expecting a cleaner solution from sonata project.

All 5 comments

Look at the error message:
The options "label_render", "mapped", "required", "sonata_field_description" do not exist.

remove this:
required' => false, 'mapped' => false

and try again

I have the same problem. It seems like it's not possible to add a button at all:

->add('myButton', 'button')

gives the error:

The options "label_render", "mapped", "required", "sonata_field_description" do not exist. Defined options are: "attr", "auto_initialize", "block_name", "disabled", "label", "label_format", "translation_domain"

The problem is related to the fact that sonata set default option (label_render, mapped) for all "form" type. But reading the documentation of symfony the button are not part of the "form" type.

A work around that I am building is to create do a custom extension for button that just configuration the option resolver.

class AdminButtonTypeExtension extends AbstractTypeExtension
{
    public function __construct()
    {

    }

    public function getExtendedType()
    {
        return 'button';
    }

    /**
     * Add the image_path option
     *
     * @param OptionsResolver $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            [
                'sonata_field_description' => null,
                'label_render' => null,
            ]
        );
    }

    /**
     * Add the image_path option
     *
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $this->setDefaultOptions($resolver);
    }
}

Configure your extension alias to be "button"
Just follow those instruction: http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html

I am not done completly so I am not sure it will fix everything but just add up the needed options.

Hope it help but I am expecting a cleaner solution from sonata project.

The line:

public function setDefaultOptions(OptionsResolverInterface $resolver)

Should it not be the implementation instead of the interface? I am receiving errors passing the interface as argument.

public function setDefaultOptions(OptionsResolver $resolver)

This issue is duplicated here too: https://github.com/sonata-project/SonataAdminBundle/issues/3217

I will close this one, since the other has a recent comment. Feel free to ping me to reopen if this is a completely different issue to reopen it.

Was this page helpful?
0 / 5 - 0 ratings