我有一個symfony表單,用於檢查數據庫中是否已存在一個人名和姓氏。 如果是這種情況,它顯示一個JavaScript窗口,其中包含3個按鈕「添加」,「修改」,「取消」 如果此人不在數據庫中,它會保留數據。 現在我想要做的是在同一個動作中放置一個'if'條件,這樣當單擊按鈕「add」時,數據仍然會持續存在。我想我必須在我的條件包括一些JavaScript,但我不知道如何做到這一點,因爲我不熟悉這種語言。任何人都可以告訴我如何做到這一點,或者有另一種方式......感謝您的時間和答案。symfony2如果JavaScript按鈕上的條件
形式
public function createAction(Request $request) {
$entity = new Invite();
$id = null;
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
$formData = $form->getData();
$prenom = $form['prenom']->getData();
$nom = $form['nom']->getData();
if ($form->isValid()) {
$repository = $this->getDoctrine()
->getManager()
->getRepository('AcmeProtoBundle:Invite');
//here the repository query that checks if the person is in the db
$invites = $repository->findByPrenomAndNom($prenom, $nom, $id);
//if a person is found with same name and surname
if ($invites) {
//in the new.html.twig file the pop up windows appears with the 3 buttons
return $this->render('AcmeProtoBundle:Invite:new.html.twig', array(
'form' => $form->createView(),
'invite' => $invites
));
//here i would like to put the condition if the "add" button is clicked
//the data are persisted
} else {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('invite_show', array(
'id' => $entity->getId())));
}
}
return array(
'entity' => $entity,
'form' => $form->createView(),
'messages' => $messages,
);
}
樹枝文件
<div>
{% if invite is defined %}
{% for person in invite %}
<div id="dialog-confirm" title="Attention Doublon">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:30 7px 10px 10;">
</span> {{person.prenom}} {{person.nom}} ID: {{person.id}} est déjà enregistré</p>
</div>
{% endfor %}
{% endif %}
</div>
javascript函數在樹枝文件
{% block documentReady %}
{% if invite is defined %}
{% for person in invite %}
$(function() {
$("#dialog-confirm").dialog({
resizable: false,
height: 200,
width: 500,
modal: true,
buttons: {
"add": function() {
$(this).dialog("close");
},
"Modify": function() {
window.location.href = "{{ path('invite_edit', { 'id': person.id }) }}";
$(this).dialog("close");
},
Cancel: function() {
window.location.href = "{{ path('invite_search'}}";
$(this).dialog("close");
}
}
});
});
{% endfor %}
{% endif %}
{% endblock documentReady %}
您可以在一看看到https://github.com/showmethecodeteam/showmethecode,他們表現出了幾種策略在一個頁面中處理多個記錄。要特別注意src/SMTC/MainBundle/Resources/views/Example/Form/new_one_to_many.html.twig,他們使用數據原型HTML內置的Symfony技巧處理集合。 –