symfony2如果条件在javascript按钮上

时间:2014-01-09 18:05:50

标签: javascript jquery symfony

我有一个symfony表单,通过他的姓名和姓氏来检查数据库中是否已存在某个人。 如果是这种情况,它会显示一个javascript窗口,其中包含3个按钮“添加”,“修改”,“取消” 如果该人不在数据库中,则它会持久保存数据。 我现在要做的是在同一个动作中放入一个“if”条件,这样当单击“add”按钮时,无论如何数据都会被保留。我想我必须在我的条件中包含一些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>

twig文件中的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 %}

2 个答案:

答案 0 :(得分:0)

您需要将“添加”按钮与传递给createAction的路径和变量相关联。伪代码中有类似的东西:

控制器

function CreateAction(Request $request, $isAdding = false) {
    if($form->isValid() || $isAdding) {
        if($isAdding) {
            // Do what you need here
        }
    }

枝条

<div id="dialog-confirm" title="Attention Doublon">
    <p> {{person.prenom}} {{person.nom}} ID: {{person.id}} est déjà enregistré</p>
    <a href="your_add_route/is_adding"> Add anyway </a>
</div>

路由

your_add_route:
    pattern: /your_add_route/{is_adding}
    default: createAction

答案 1 :(得分:0)

我认为添加Twig链接的更好方法是:

枝条

<div id="dialog-confirm" title="Attention Doublon">
    <p> {{person.prenom}} {{person.nom}} ID: {{person.id}} est déjà enregistré</p>
    <a href="{{ path('your_add_route', {'is_adding': 'is_adding'}) }}"> Add anyway </a>

如果在某一天有人会更改此操作的路线

,它将更改网址