In advance, sorry for my english...
I have a problem with my symfony form which seem simple but i don't understand how to do.
I have a user class which has an argument "salaries". This is an array of object "salary" with an id, a label and an amount.
When i add a user, i want to fill my objects "salary". I want for each instance of salary a widget with the label (which is in my object salary) and the input to put the amount.
I tried with collectionType but it doesn't seem to work. I tried to create a field in my object user per salary but i can't create the setter and getter dynamically.
Just for information, i don't use Doctrine.
If it's not clear, i can explain in French, it will be better...
Here's my userType.php :
$builder
->add('name', TextType::class, array('label'=>'Name : '))
->add('lastname', TextType::class, array('label'=>'Lastname : '))
->add('salaries', CollectionType::class, array('entry_type' => salaryType::class));
My salaryType.php :
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('amount', TextType::class, array('label'=> $builder->getData()->getLabel(), 'required' => false));
$builder->getForm();
}
And my twig :
<div id="infos">
<div class="title">Salary</div>
<div>
{% for salary in formUser.salaries %}
<div><span>{{ form_label(salary.label) }}</span><span>{{ form_widget(salary.amount) }}</span></div>
{% endfor %}
</div>
</div>
in your userType replace this
->add('salaries', CollectionType::class, array('entry_type' =>
salaryType::class));
By
$builder->add('salaries', EntityType::class, [
'class' => 'NameBundle:salary',
'empty_value' => 'Choise a Salary',
'label' => 'Salary',
'required' => false,
'query_builder' => function (SalaryRepository $er){
return $er->findAll();
}
]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.