[英]How to change form id attribute in symfony2 using php templates?
我的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');?>");
}
通過創建表格,您可以編輯參數attr並給ihm一個ID值,例如,我的字段是title。 在您的表單生成器中:
->add('title','textarea','array(
'attr'=>array(
'id'=>'my_id',
'class'=>'my_class'
'placeholder'=>'My title' ,
...................
)
));
您不需要對代碼進行任何操作,就可以像在模板中創建代碼那樣使用那些ID或類。
如果我的英語不好,請原諒。 如果不起作用,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.