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

时间:2013-12-13 00:29:28

标签: php html forms symfony

我的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显示的编辑表单:

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');?>");
}

1 个答案:

答案 0 :(得分:0)

通过创建表单,您可以编辑参数attr并为ihm提供Id值,例如我的字段是标题。在您的表单生成器中:

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

您不需要对您使用那些ID或类的代码进行操作,就像您在模板中创建它们一样。

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