What I'm trying to do is to set a "seoUpdatedAt" field when the "pageTitle" or "metaDescription" fields are modified. In the same time I'd like to render the content of this field in my backoffice through the Symfony2 form component, without giving the possibility to edit the field value (because the value must be set up by the timestampable extension).
I tried to set up the field as *read_only* but the value doesn't update, and neither does the doctrine extension (as though it is being ignored and it makes sense).
I also tried to set the field as a *datetime single_text* but two problems arose:
- the admin can manually change the value
- right after the submit action takes place the value that is rendered appears to be the old datetime although it's been updated on the DB side, as though the value in the POST request is overwriting the DB value in the form rendering flow. refreshing the page without resubmitting the data shows the updated value (updated by the doctrine extension)
I finally tried with the disabled option but it's not working as expected.
A pinch of code:
// in my entity
/**
* @var \DateTime
* @Gedmo\Timestampable(on="change", field={"pageTitle", "metaDescription"})
* @ORM\Column(name="seo_updated_at", type="datetime", nullable=true)
*/
private $seoUpdatedAt;
And here the form:
// in my form type
$builder
->add('pageTitle', 'text', array('required' => false))
->add('metaDescription', 'textarea', array('required' => false))
->add(
'seoUpdatedAt',
'datetime',
array(
'widget' => 'single_text',
'format' => 'dd-MM-yyyy HH:mm:ss',
'required' => false
)
);
So, how do you suggest meeting the following requirements?
- the value shouldn't be manually editable
- the value must be always rendered with the updated value even after a form submission
Listener on Doctrine events? Listener on a form event? A new form type extension?