Php 在SonataAdminBundle表单中使用Ajax

Php 在SonataAdminBundle表单中使用Ajax,php,ajax,symfony,sonata-admin,Php,Ajax,Symfony,Sonata Admin,我在SonataAdminBundle中使用了Ajax来填充select字段city from zipcode字段。 用户将首先在关联字段中输入zipcode的值,然后ajax将在选择字段中刷新与此zipcode相关的所有城市 这个过程对我很有效,但是当我提交时,城市字段总是得到验证错误。这个值无效 这是我的实体: /** * @ORM\ManyToOne(targetEntity="Insee") * @ORM\JoinColumn(name="adresse_ville_id",

我在SonataAdminBundle中使用了Ajax来填充select字段city from zipcode字段。 用户将首先在关联字段中输入zipcode的值,然后ajax将在选择字段中刷新与此zipcode相关的所有城市

这个过程对我很有效,但是当我提交时,城市字段总是得到验证错误。这个值无效

这是我的实体:

    /**
 * @ORM\ManyToOne(targetEntity="Insee")
 * @ORM\JoinColumn(name="adresse_ville_id", referencedColumnName="id")
 **/
private $adresseVille;

/**
 * @var string
 *
 * @ORM\Column(name="adresse_code_postal", type="string", length=255)
 * @Gedmo\Translatable
 */
private $adresseCodePostal;
这是我的管理类:

 /**
 * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
 *
 * @return void
 */
protected function configureFormFields(FormMapper $formMapper)
{
    //.....
    $formMapper
        ->add('adresseCodePostal')
        ->add(
            'adresseVille',
            'entity',
            array(
                'class' => 'SimuleoAdminBundle:Insee',
                'choices' => array(),
                'property' => 'commune',
                'empty_value' => 'Ville'
            )
        )
    //.....
这是javascript代码:

$("#{{ admin.uniqId }}_adresseCodePostal").blur(function () {
                var zipCode = $("#{{ admin.uniqId }}_adresseCodePostal").val();
                $.ajax({
                    url: "http://simuleo-dev.local/app_dev.php/api/villes/" + zipCode,
                    async: false,
                    dataType: "json",
                    success: function (result) {
                        console.log(result);

                        var i= 0;
                        var html = "";

                        if (result == "Empty") {
                            $("#{{ admin.uniqId }}_adresseCodePostal").css("border-color", "#ff0000");
                            $("#{{ admin.uniqId }}_adresseCodePostal").val("No city !!");
                        }
                        else {
                            $("#{{ admin.uniqId }}_adresseCodePostal").css("border-color", "#aaaaaa");
                            for (i=0;i<result.length;i++){
                                html += '<option value="' + result[i].id + '">' + result[i].commune + '</option>';
                            }
                        }
                        $("#{{ admin.uniqId }}_adresseVille").empty();
                        $("#{{ admin.uniqId }}_adresseVille").append(html);
                    }
                });
            });
你知道怎么修吗


提前感谢。

我们可以查看正在更新的实体的类定义和任何验证配置吗?看不到代码就帮不上忙。我已经编辑了我的问题。