4

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....enter image description here

But when i tried to click on create.its showing an error enter image description here

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

Aman Varshney
  • 820
  • 1
  • 9
  • 26

3 Answers3

0

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

Charles-Antoine Fournel
  • 1,713
  • 1
  • 25
  • 37
  • 1
    i made an hack which works but it is not clean code at all . If you fills the choice values ( in configureFormField ) with index from 0 to X values by using a for, it ll works . no one gets a valid solution ? – Charles-Antoine Fournel Feb 21 '14 at 16:08
0

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.

Baba Yaga
  • 1,819
  • 15
  • 20
0

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.