Upgrading Symfony 2.0 using JS?

I have been working for several hours, but surprisingly I did not find a topic on this subject.

I have the following form

class propertyType extends AbstractType
{

    public function buildForm(FormBuilder $builder, array $options)
    {

        $builder
            ->add('city')
            ->add('district', 'entity', array('class'=>'FlatShanghaidefaultBundle:district', 
                                   'property'=>'name',
                                   'query_builder' => function ($repository) {
                                   $qb = $repository->createQueryBuilder('district');
                                   $qb->add('where', 'city = :city');
                                   $qb->setParameter('city', 1);
                                   return $qb;

    }


    public function getName()
    {
        return 'property';
    }
}

When the user selects a city in the form, I want the district parameters to be dynamically updated and limited to this city. With Ajax or JS? What would be the best practice? Do you know a tutorial on this topic? If someone could put me on the right track, that would help a lot.

Thank!

+3
source share
3 answers

The query designer will not solve your problem, you can completely remove it.

This request is launched when the form is created, as soon as you use it in your browser, you need to use javascript to fill in the parameters.

, javascript, ajax ( ajax).

, jquery , :

, , Bundle, : https://github.com/genemu/GenemuFormBundle, ajax, jquery. , ajax , (, ). , , .

+2

. (), , , . , .

:

{% macro javascript_filter_unit(event, selector) %}
<script>
    $(function(){
        $('#usersection')
                .on('{{ event }}', '{{ selector }}', function(e){
                    e.preventDefault();
                    if (!$(this).val()) return;
                    $.ajax({
                        $parent: $(this).closest('.child_collection'),
                        url: $(this).attr('data-url'),
                        type: "get",
                        dataType: "json",
                        data: {'id' : $(this).val(), 'repo': $(this).attr('data-repo'), parameter: $(this).attr('data-parameter')},
                        success: function (result) {
                            if (result['success'])
                            {
                                var units = result['units'];
                                this.$parent.find('.unit').eq(0).html(units);   
                            }
                        }
                    });
                })
    });

</script>

{% endmacro %}

ajax : array ('success' = > $value, 'units' = > $html). $html , . , javascript- ajax .

, :

{% import ':Model/Macros:_macros.html.twig' as macros %}
{{ macros.javascript_filter_unit('change', '.unitTrigger') }}

, : , . , , ajax.

, .

0
source

All Articles