In my Symfony project when I select an Employee from an 'Employee' dropdown list, I need to get a default value from another dropdown list, 'Department' that Employee is in.
Both dropdown lists, Employee and Department are already populated from the database.
I have a form in Symfony where I can manually select an Employee and a Department.
How is it possible to get an employee's default department on selecting an employee's name from a dropdown list?
In the FormType.php file I have:
class CommMemberType extends AbstractType
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('employee','entity', array(
'label' => 'Employee Name',
'empty_value' => ' ',
'class' => 'CompanyBundle:Employee',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('e')
->orderBy('e.fname', 'ASC');
},
)
)
->add('dept', 'entity', array(
'label' => 'Department',
'empty_value' => '---Select Department---',
'required' => true,
'class' => 'CompanyBundle:Lookup\Dept',
'property' => 'meaning',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('dept')
->orderBy('dept.meaning', 'ASC');
},
));
I also have a jQuery code and I tested just the console.log part:
$(document).ready(function() {
$('body .employee').on('change', function(){
var id = $(this).val();
console.log(id);
$.post('{{ emp_jquery }}', { 'employee': id },function(data){
$('.dept').html(data);
},"text");
})
});
Could you please give me a hand? Thank you so much!