Hello.
I'm trying to setup default data if they non exist in text input field. This is not working for me:
- { property: 'filename', type: 'text', type_options: { empty_data: 'default-template.html', read_only: true } }
So I rather tried this, but it is not good solution:
- { property: 'filename', type: 'text', type_options: { data: 'default-template.html', read_only: true } }
I don't think you can define the empty data like that in the form. See https://symfony.com/doc/current/reference/forms/types/text.html#empty-data
Maybe you can read this article https://github.com/javiereguiluz/EasyAdminBundle/blob/master/Resources/doc/book/7-complex-dynamic-backends.md and override createNewEntity() or createEditForm(), etc. depending on your needs.
If this default value is static, you can set it into property directly:
private $filename = 'default-template.html';
or in you __contructor() method.
I thought that default-template.html meant that you want to include the content of that page as the empty data. But it looks like you want to use this string that resembles the name of a template --> default-template.html The solution proposed by @yceruto should be the easiest one, but the one you shows should work too.
If I can define default data why it is not possible to add some check => if some data exist (in edit mode), don't use default data, but data from database?
That's exactly how Symfony Forms work. The empty data is only used when that field doesn't have any data. I use this option myself in some backends so it should definitely work (I mean, it should work because it's provided by Symfony and not by this bundle).
Do you see any error or do you just never see that empty data?
Can't see them at all. How can I define them in yml config file?
- { property: 'filename', type: 'text', type_options: { empty_data: 'default-template.html', read_only: true, required: false } }
I can confirm this bug ... but I don't know why it happens or how to solve it.
In the EasyAdmin Demo app, I add the same default value as you:
easy_admin:
entities:
Product:
class: AppBundle\Entity\Product
# ...
form:
fields:
- { property: 'name', type_options: { empty_data: 'default-template.html' } }
Then, I do a dump() in this line of the EasyAdminformType class, just before adding the form field to the form. The empty data value is still there:

However, if I do a dump() in this line of our form theme, just inside the form_widget_simple block, I no longer see the empty data:

So the information is lost and I don't know why 馃槶
The empty_data is not about initial/default value in view layer, but in model layer when the form field value is empty upon submission (http://symfony.com/doc/current/reference/forms/types/form.html#empty-data):
This will still render an empty text box, but upon submission the
John Doevalue will be set. Use thedataorplaceholderoptions to show this initial value in the rendered form.
But still by using data option is no good, because (http://symfony.com/doc/current/reference/forms/types/form.html#data):
The
dataoption always overrides the value taken from the domain data (object) when rendering. This means the object value is also overriden when the form edits an already persisted object, causing it to lose it's persisted value when the form is submitted.
So the recomendation is to set explicitly the data in the underlined object on initialization, either in __constructor() or before bind the object to the form.
http://symfony.com/doc/current/components/form.html#setting-default-values
EDIT: Also, I would keep the empty_data if I need to ensure a value for this field, since the initial value can be removed by the user.
Maybe createNew<EntityName>Entity() method is a good place to do that.
For example:
protected function createNewUserEntity()
{
$user = new User();
// Settings initial info
$user->setCompany($this->findDefaultCompanyFromDB());
$user->setRoles(array('ROLE_ADMIN'));
// ...
return $user;
}
Closing this because it's not really a bug but the way Symfony Forms work.
Most helpful comment
The
empty_datais not about initial/default value in view layer, but in model layer when the form field value is empty upon submission (http://symfony.com/doc/current/reference/forms/types/form.html#empty-data):But still by using
dataoption is no good, because (http://symfony.com/doc/current/reference/forms/types/form.html#data):So the recomendation is to set explicitly the data in the underlined object on initialization, either in
__constructor()or before bind the object to the form.http://symfony.com/doc/current/components/form.html#setting-default-values
EDIT: Also, I would keep the
empty_dataif I need to ensure a value for this field, since the initial value can be removed by the user.