简体   繁体   English

下载Ajax onchange SonataAdminBundle Symfony2问题

[英]Dropdown Ajax onchange SonataAdminBundle Symfony2 Issue

I am trying to implement onchange dropdown in SonataAdminBundle. 我正在尝试在SonataAdminBundle中实现onchange下拉列表。 My Entity is like 我的实体就像

 class BuilderHomePage
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

 /**
 * @var \Hello\FrontendBundle\Entity\MtContentType
 *
 * @ORM\ManyToOne(targetEntity="Hello\FrontendBundle\Entity\MtContentType")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="content_type_id", referencedColumnName="id")
 * })
 */
private $section;


/**
 * @var string
 *
 * @ORM\Column(name="title", type="string",length=100, nullable=false)
 */
private $title;

My Admin Class 我的管理员班

     public function getTemplate($name)
     {
       switch ($name) {
        case 'edit':
            if ($this->getSubject()->getId()) {
                return 'HelloAdminBundle:Builder:base_edit.html.twig';
            } else {
                return 'HelloAdminBundle:Builder:base_edit.html.twig';
            }
            break;
        default:
            return parent::getTemplate($name);
            break;
    }

}

protected function configureRoutes(RouteCollection $collection) {
$collection
    ->add('getArticleFromSection', 'getArticleFromSection')
    ;
}
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper

        ->add('section')
        ->add('title','choice',array('required' => false ))

    ;
}

My Builder:base_edit.html.twig 我的构建器:base_edit.html.twig

     <script type="text/javascript">
     $(document).ready(function()
       {
         $("#{{ admin.uniqId }}_section").change(function()
        {
        var id=$(this).val();
        var dataString = 'id='+ id;
        $.ajax
        ({
            type: "POST",
            url: "{{ admin.generateObjectUrl('getArticleFromSection', object) }}",
            data: dataString,
            cache: false,
            success: function(html)
            {
                $("#{{ admin.uniqId }}_title").html(html);
            } 
        });
       });

     });

</script>

Ajax Request Controller Ajax请求控制器

    $article        = $this->get('hello_frontend.article');
    $totalArticle        = $article->getArticleByContentType($id);

     $html = "";
    foreach($totalArticle as $res){
    $html .="<option value=".$res->getId().">".$res->getTitle()."</option>";
    }

Till now everything works fine.... 直到现在一切正常.... 在此输入图像描述

But when i tried to click on create.its showing an error 但是,当我试图点击显示错误的create.its时 在此输入图像描述

I am not able to figure out the problem. 我无法弄清楚问题。 your help will be appreciated 你的帮助将不胜感激

A first element of answer : 回答的第一个要素:

after doing several test, it seems that the options values "granted" for submitting is entityAdminClass related . 在进行多次测试之后,似乎提交的“授权”选项值与entityAdminClass相关。

you can add all the options you want only if its values match these defined in your choices array inside the declaration of fields in configureFormFields. 只有当其值与configureFormFields中字段声明中的choices数组中定义的值匹配时,才能添加所需的所有选项。

i'm looking for a way to bypass this control .. 我正在寻找绕过这种控制的方法..

It is because you didn't specify any choices in your admin class. 这是因为您没有在管理类中指定任何选项。

When you submit the form the admin class checks if the value you submitted matches one of the values of your admin class. 提交表单时,管理类会检查您提交的值是否与管理类的某个值匹配。 This is done for security so you can't submit a value that you didn't specify. 这样做是为了安全,因此您无法提交未指定的值。

Noscope, it is exactly what he wants to do. Noscope,这正是他想要做的。 He wants to populate the second select dynamically, so there is no way he could populate it in the admin class. 他想动态填充第二个选择,因此他无法在管理类中填充它。

I have the same problem and I can't find how to solve it. 我有同样的问题,我找不到如何解决它。 The only way I've found for now is to check $this->container->get('request')->request->get('YourFormName[your_field]', null, true) in my action. 我现在找到的唯一方法是在我的操作中检查$this->container->get('request')->request->get('YourFormName[your_field]', null, true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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