Skip to content

[Form] Access to the Form Object in Form Type Extensions #50426

Closed
@MichaelBrauner

Description

@MichaelBrauner

Description

In Symfony forms, particularly when utilizing form extensions, there is often a need to configure certain options based on the properties of the current form. A common scenario is, for example, adding or removing options depending on whether the current form is the root form or not.

Currently, there is no way to access the form object or its properties (such as isRoot()) within the OptionsResolver. This limits the flexibility in configuring forms and can lead to developers having to implement more complex and less intuitive solutions.

Here is an example to illustrate the problem:

class AjaxFieldsExtension extends AbstractTypeExtension
{

    public static function getExtendedTypes(): iterable
    {
        return [FormType::class];
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        // Check if the form is the root form
        if (/* check condition */) {
            $resolver->define('has_ajax_fields')
                ->required()
                ->default(false)
                ->allowedTypes('bool')
                ->info('If set this form can have ajax fields.');           
        } else {
            
            // Check if the root form has the option 'has_ajax_fields'
            if (/* check condition */) {
                $resolver->define('is_ajax_field')
                    ->required()
                    ->default(false)
                    ->allowedTypes('bool')
                    ->info('If set this form field will be an ajax field.');
            }
        }
    }
}

In the code above, there is no current method to access the form object in order to check if it's the root form before defining the options. Additionally, there's no way to check if the root form has the option 'has_ajax_fields' before defining the 'is_ajax_field' option.

Proposed Solution

It would be highly beneficial if Symfony could provide a way to access the form object, or at least some of its properties, within the OptionsResolver.

Possible Challenges

Care would have to be taken to ensure that the form object is fully initialized and valid at the point when the OptionsResolver is executed. There might also be issues with the order in which the various parts of the form are processed.

Conclusion

Despite these potential difficulties, implementing this feature would be a highly valuable improvement to the Symfony Form component. It would make form configuration more flexible and powerful, and would make developers' lives easier.

Additional Question

Would it be worthwhile for developers to write a PR for this feature? What would be the chances of such a PR being accepted?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions