简体   繁体   English

如何使用PHP模板更改symfony2中的表单ID属性?

[英]How to change form id attribute in symfony2 using php templates?

I have multiple forms in my php template, and I want to give different ids to them so they can be submited using js. 我的php模板中有多种形式,我想给它们提供不同的ID,以便可以使用JS提交它们。 how could I do that as I can't find nothing for php templates? 我该怎么办,因为我找不到php模板的内容?

in my controller: 在我的控制器中:

public function showAction()
{
   //get all from db
   $em = $this->getDoctrine()->getManager();
   $qytete = $em->getRepository('EraRestoranteBundle:Qytet')->findAllOrderedByName();

   $qytet = new Qytet();
   //create add form
   $add_form = $this->createFormBuilder($qytet)
        ->setAction($this->generateUrl('admin_initconfig_qytet_create'))
        ->setMethod('POST')
        ->add('emri', 'text', array('attr' => array('class' => '', 'size' => '12')))
        ->add('foto', 'file', array('required' => false, 'attr' => array('class' => '', 'style' => 'max-width: 150px;')))
        ->add('shto', 'submit', array('attr' => array('class' => '', 'style' => 'padding: 7px 20px 7px 20px; border: none; border-radius: 4px; color: white; background-color: rgb(197, 213, 43);')))
        ->getForm();

    //create edit form      
    $edit_form = $this->createFormBuilder($qytet)
        ->setAction($this->generateUrl('admin_initconfig_qytet_update'))
        ->setMethod('POST')
        ->add('emri', 'text', array('attr' => array('class' => 'cl_emri_edit', 'size' => '12')))
        ->add('foto', 'file', array('required' => false, 'attr' => array('class' => 'cl_foto_edit', 'style' => 'max-width: 150px;')))
        ->getForm();
    //render template
    return $this->render('EraAdminBundle:InitConfig:qytet.html.php', array('qytete'=>$qytete, 'add_form'=>$add_form->createView(), 'edit_form'=>$edit_form->createView()));
}

in my template: 在我的模板中:

this is the edit form shown onclick with js: 这是用js onclick显示的编辑表单:

function qytetShowEditForm(idNumber, cityName)
{
    //check if there's another edit form active
    if(document.getElementById("qytete_table").edit)
    return;
    <?php $string = $view['form']->start($edit_form).
                    "<td style=\"text-align: center;\"></td><td style=\"text-align: center;\">".
                    $view['form']->widget($edit_form['emri']).
                    "</td>
                    <td style=\"max-width: 150px; text-align: center;\">".
                    $view['form']->widget($edit_form['foto']).
                    "</td>
                    <td style=\"text-align: center;\">
                        <a onclick=\"qytetEditSubmit();\" id=\"ndrysho\" style=\"cursor: pointer; padding: 7px 12px 7px 12px; border-radius: 4px; color: white; background-color: orange;\">Ndrysho</a>
                    </td>".
                    $view['form']->end($edit_form);
          $string = str_replace(array("\r\n", "\r", "\n"), "", $string);
    ?>
    document.getElementById("row_"+idNumber).innerHTML = '<?php print($string);?>';
    var edit_emri = document.getElementsByClassName('cl_emri_edit');
    edit_emri[0].placeholder = cityName;

    document.getElementById("qytete_table").edit = true;
}

and this is the add form always shown: 这是总是显示的添加形式:

    <?php echo $view['form']->start($add_form) ?>
<td></td>
<td style="text-align: center;">
<?php echo $view['form']->widget($add_form['emri']) ?>
</td>
<td style="max-width: 150px; text-align: center;">
    <?php echo $view['form']->widget($add_form['foto']) ?>
</td>
    <td style="text-align: center;">
<?php echo $view['form']->widget($add_form['shto']) ?>
    </td>
<?php echo $view['form']->end($add_form); ?>

I want to be able to submit the edit form with js like this: 我希望能够使用js提交编辑表单,如下所示:

  function qytetEditSubmit()
{
    var submit = confirm("Doni t'i ruani ndryshimet?");
    if(submit)
    {
        //document.getElementById().submit();
    }
    else
    window.location.assign("<?php echo $view['router']->generate('admin_initconfig');?>");
}

By creating you form you can edit the parameter attr and give ihm an Id value for example my field is title . 通过创建表格,您可以编辑参数attr并给ihm一个ID值,例如,我的字段是title。 In Your Form Builder: 在您的表单生成器中:

->add('title','textarea','array(
    'attr'=>array(
        'id'=>'my_id',
        'class'=>'my_class'
        'placeholder'=>'My title' ,
        ...................
 )
));

You don't need manipulations on your code you use those Id or Classes like you have created them in the template. 您不需要对代码进行任何操作,就可以像在模板中创建代码那样使用那些ID或类。

I excuse me if my english is poor. 如果我的英语不好,请原谅。 And if it's don't work tell me. 如果不起作用,请告诉我。

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

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