简体   繁体   中英

php - Symfony2 form dynamic input text field fill depending on a selection

I have the following situation:

In the database there is a table with all the counties in a country and a second table which contains all the cities in the same contry. The city table has the columns:

- id
- name
- longitude
- latitude
- county - county id

on the page I have a form with:

- county select field - symfony entity field type
- city select field - symfony entity field type
- longitude input (text) field
- latitude input (text) field - let's ignore this field for the moment

So, when the user selects a county, the city field is filled with all the cities coresponding the selected county; then, when the user selects a city, the longitude field is filled with the city logitude database value.

Following this tutorial ( http://aulatic.16mb.com/wordpress/2011/08/symfony2-dynamic-forms-an-event-driven-approach/ ) I have managed to deal with county selection and city filed dynamic replacing. But I can't seem to find how to fill a simple text value (in this case longitude) "symfony way".

I have tried to do it like

$refreshLongitude = function( $form, $city, $translator ){
    $form->add(
        'longitude',
        'text',
        array(
            'label' => $translator->trans( 'project.longitude' ),
            'required' => true,
            'attr' => array(
                'class'=> 'form-control',
                'value' => function( \Doctrine\ORM\EntityRepository $repository ) use ( $city ){
                    if( $city instanceof City ){
                        return $city->getLongitude();
                    }

                    $query = $repository->createQueryBuilder( 'city' );

                    if( is_numeric( $city ) ){
                        $query->where( 'city.id = :city' )
                            ->setParameter( 'city', $city );
                    }
                    else{
                        $query->where( 'city.id = 2715' );
                    }

                    $city = $query->getSingleResult();

                    return $city->getLongitude();
                }
            )
        )
    );
};

but I am getting: ContextErrorException: Catchable Fatal Error: Object of class Closure could not be converted to string

Any ideea?

So, starting from phpisuber01 comment (many thanks), I have managed to outline the following code to dinamically fill a simple text field with certain database value in symfony2. I am not sure that this is the right way, but it works. Any more elegant way is welcomed.

$refreshLongitude = function( $form, $city, $translator, $em ){
    $form->add(
        'longitude',
        'text',
        array(
            'label' => $translator->trans( 'project.longitude' ),
            'required' => true,
            'attr' => array(
                'class'=> 'form-control',
                'value' => call_user_func( function() use ( $city, $em ){
                    if( $city instanceof City ){
                        return $city->getLongitude();
                    }
                    else{
                        $repository = $em->getRepository( 'AppBundle:City' );
                        $query = $repository->createQueryBuilder( 'city' );

                        if( is_numeric( $city ) ){
                            $query->where( 'city.id = :city' )
                                ->setParameter( 'city', $city );
                        }
                        else{
                            $query->where( 'city.id = 2715' );
                        }

                        $city = $query->getQuery()->getSingleResult();

                        return $city->getLongitude();
                    }
                })
            )
        )
    );
};

$translator and $em vars are passed from the controller to form builder, then to event listeners, then to $refreshLongitude function

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM