Norbert Orzechowicz
Norbert Orzechowicz

Reputation: 1311

symfony2 form creator based on annotations

I'm working on mechanism that will allow me create forms automatically from class annotations. For example there is a class called "News" with some custom annotations.

/**
 * @Admin\Form(name="news")
 */
class News
{
    /**
     *
     * @Admin\Field(name="title", type="text")
     */
    private $title;

} 

My goal is to write mechanism that will check if exists class with "Form" annotation and create form based on this class fields.

Where should I put this mechanism? First I was thinking about owerwritting FormFactory but I believe there is a better place for such thing, maybe Extension?

Upvotes: 3

Views: 2117

Answers (2)

jmlnik
jmlnik

Reputation: 2887

In Symfony2, you can add functionality to existing form fields through the use of "Form Type Extensions".

To apply your extension to all field types, set the return value of the getExtendedType() method to "form", i.e:

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

I haven't figured out how to fetch the annotations from the form extension yet.

Upvotes: 0

Lusitanian
Lusitanian

Reputation: 11122

There already is a bundle that does what you're asking for: http://knpbundles.com/FlintLabs/FormMetadataBundle

However, if you'd like to create it yourself, you should create a bundle and within it create a custom annotation driver based on the doctrine2 specs (as Symfony uses Doctrine for reading annotations)

Upvotes: 3

Related Questions