【问题标题】:How to change form id attribute in symfony2 using php templates?如何使用 php 模板更改 symfony2 中的表单 id 属性?
【发布时间】:2013-12-13 00:29:27
【问题描述】:

我的 php 模板中有多个表单,我想给它们分配不同的 id,以便它们可以使用 js 提交。我怎么能这样做,因为我找不到任何 php 模板?

在我的控制器中:

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()));
}

在我的模板中:

这是带有 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;
}

这是始终显示的添加表单:

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

我希望能够像这样用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');?>");
}

【问题讨论】:

  • 能贴出相关代码会很有帮助
  • 我编辑了问题...

标签: php html forms symfony


【解决方案1】:

通过创建表单,您可以编辑参数 attr 并给 ihm 一个 Id 值,例如我的字段是 title 。在您的表单生成器中:

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

您不需要像在模板中创建它们那样使用那些 Id 或 Classes 对代码进行操作。

如果我的英语不好,请原谅。如果它不起作用,请告诉我。

【讨论】:

  • 感谢您的努力。但我正在尝试更改表单的 id,而不是它的字段。
猜你喜欢
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多