Reputation: 698
I am using Sonata Admin bundle to create admin for one of my sites. I have a form and it has many fields. I have a date field and I want this to get auto saved to database when saving the form. Means I want the date field to be hidden from the user but the date should get saved. How can I do this..?? I have tried with the hidden form type, but it was of no use. Please help me. Thanks in advance
Upvotes: 1
Views: 3248
Reputation: 2631
In the AdminClass, you have preUpdate()
and prePersist()
methods where you can set your date value.
You can also use StofDoctrineExtensionsBundle https://github.com/stof/StofDoctrineExtensionsBundle/blob/master/Resources/doc/index.rst and the Timestampable Extension
Timestampable - updates date fields on create, update and even property change.
Upvotes: 1
Reputation: 576
First remove datefield in configureFormFields() function
you can use prePersist() and preUpdate() method in Admin Class
public function prePersist($data) {
$data->setDateField(date);
}
public function preUpdate($data) {
$data->setDateField(date);
}
In your Entitie datefield called startDate.
So, you can change setDateField() to setStartDate() in prePersist() or preUpdate()
Upvotes: 2