Michi
Michi

Reputation: 2520

How should I implement a custom form field type only for display accepting several formats

In my forms I need a field to only display data. No editing will be done on this data so I feel it's not enough to set read_only on a text field. The idea is to create custom form field type that would accept different formats as the value (ex. DateTime, String).

The output would be somethink like

<div>
    <label>My Label</label>
    My value
</div>

I've been looking into the docs and also the code of the predefined field types but couldn't make out how to go on :-(

I'm not looking for the complete code, only need directions...

Thanks

Upvotes: 0

Views: 133

Answers (3)

Michi
Michi

Reputation: 2520

Finally, thanks to another thread on Stackoverflow, I went with DataTransformer (see the Docs).

This way I add the field as a text type and add a custom DataTransformer to the FormBuilder which will convert the data to the type I need.

If anyone is interested in it I will post the code.

Upvotes: 0

smoreno
smoreno

Reputation: 3530

Maybe you should use form events. Read this example in official docs.

Upvotes: 0

Peter Bailey
Peter Bailey

Reputation: 105878

In my opinion: If this is for display only, then it's not form data and therefore has no business being implemented as a Symfony\Component\Form\AbstractType

Perhaps you can give some more detail about exactly what it is you're trying to accomplish?

Upvotes: 1

Related Questions