2

I have Symfony form (FormType) and want to use AngularJS and ng-submit on client-side, but Symfony adds an action attribute to the form and ng-submit doesn't prevent form submission and page reload.

2 possible solutions:

  1. How I can remove action attribute in Symfony form?
  2. How I can call preventDefault() (or something similar) inside my Angular controller?
2
  • 1
    How about you render the form template manually?
    – ferdynator
    Commented Feb 13, 2015 at 12:29
  • 1
    @ferdynator I'm using {{ form(myFormVar) }} to render form, but if I set "action" attribute in Twig or PHP to NULL, FALSE, TRUE, "" and other gives zero results -- Symfony will render form with empty action attribute.
    – kovalevsky
    Commented Feb 13, 2015 at 12:37

1 Answer 1

2

Okay, I found a solution. I just extend the form_start block in my base form theme:

{%- block form_start -%}
    {% set method = method|upper %}
    {%- if method in ["GET", "POST"] -%}
    {% set form_method = method %}
{%- else -%}
    {% set form_method = "POST" %}
{%- endif -%}
    <form name="{{ name }}" method="{{ form_method|lower }}" {% if action|length %}action="{{ action }}"{% endif %}{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}{% if multipart %} enctype="multipart/form-data"{% endif %}>
    {%- if form_method != method -%}
    <input type="hidden" name="_method" value="{{ method }}" />
{%- endif -%}
{%- endblock form_start -%}

What's new is the added if:

{% if action|length %}action="{{ action }}"{% endif %}
1
  • I doubt it is the only solution. I'm searching right now and post the answer later
    – julio
    Commented Apr 14, 2015 at 12:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.