I've been trying for w hile to make a dependent dropdown in symfony, I saw this post in the symfony documentation and I followed it, the only think that I don't understand in here:
$formModifier = function (FormInterface $form, Sport $sport = null) {
$positions = null === $sport ? array() : $sport->getAvailablePositions();
$form->add('position', EntityType::class, array(
'class' => 'AppBundle:Position',
'placeholder' => '',
'choices' => $positions,
));
};
Where does the getAvailablePositions should be defined? and how it would look like? I'm really lost and have been since a while. I would really appreciate your help. Thanks in advance.
Btw. by dependent dropdown I mean is like in the symfony document you have two dropdowns, one with sports and the other with positions, then you select soccer, and after that the second one populates with positions like goalkeeper, defense, attack, ...
EDIT:
This is my javascript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#sport").change(function(){
var data = {
option_id: $(this).val()
};
$.ajax({
type: 'POST',
url: '{{ path("positions") }}',
data: data,
success: function(data) {
var $position_select = $('#positions');
$position_select.html('<option>positions</option>');
for (var i=0, total = data.length; i < total; i++) {
$position_select.append('<option value="' + data[i].id + '">' + data[i].name + '</option>');
}
$position_select.html('');
$.each(data, function(k, v) {
$position_select.append('<option value="' + v + '">' + k + '</option>');
});
}
});
});
});
</script>
It can be done using event subscribers. Pretty good tutorial here http://showmethecode.es/php/symfony/symfony2-4-dependent-forms/