簡體   English   中英

下載Ajax onchange SonataAdminBundle Symfony2問題

[英]Dropdown Ajax onchange SonataAdminBundle Symfony2 Issue

我正在嘗試在SonataAdminBundle中實現onchange下拉列表。 我的實體就像

 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;

我的管理員班

     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 ))

    ;
}

我的構建器: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請求控制器

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

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

直到現在一切正常.... 在此輸入圖像描述

但是,當我試圖點擊顯示錯誤的create.its時 在此輸入圖像描述

我無法弄清楚問題。 你的幫助將不勝感激

回答的第一個要素:

在進行多次測試之后,似乎提交的“授權”選項值與entityAdminClass相關。

只有當其值與configureFormFields中字段聲明中的choices數組中定義的值匹配時,才能添加所需的所有選項。

我正在尋找繞過這種控制的方法..

這是因為您沒有在管理類中指定任何選項。

提交表單時,管理類會檢查您提交的值是否與管理類的某個值匹配。 這樣做是為了安全,因此您無法提交未指定的值。

Noscope,這正是他想要做的。 他想動態填充第二個選擇,因此他無法在管理類中填充它。

我有同樣的問題,我找不到如何解決它。 我現在找到的唯一方法是在我的操作中檢查$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