Dropdown Ajax onchange SonataAdminBundle Symfony2 Issue

守給你的承諾、 提交于 2019-12-12 11:34:04

问题


I am trying to implement onchange dropdown in SonataAdminBundle. 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

     <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

    $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

I am not able to figure out the problem. your help will be appreciated


回答1:


A first element of answer :

after doing several test, it seems that the options values "granted" for submitting is entityAdminClass related .

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.

i'm looking for a way to bypass this control ..




回答2:


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.




回答3:


Noscope, it is exactly what he wants to do. 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.



来源:https://stackoverflow.com/questions/20798777/dropdown-ajax-onchange-sonataadminbundle-symfony2-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!