3

I have a simple edit form built with Symfony2 form component. Which means that this form will be populated with model data in my php controller.

But each form inputs has associated an ng-model directive. Now here is the problem I'm facing:

When entering on the edit form the input are empty of values(because they weren't initialized in angular controller), however if i check with firebug they will have the value attribute set with the appropriate data(because the data was set in the php controller).

Is there a way somehow to also initialize the angular models?

4
  • Are you using Symfony to generate the form html i.e. twig? Commented Feb 6, 2014 at 14:32
  • Yes, but this shouldn't be a problem Commented Feb 6, 2014 at 14:34
  • I just did the similar thing two days ago. Are you generating the form thought AJAX? If so, have you tried running $scope.$apply afterwards? (if you use Angular's $http you don't need this) Commented Feb 6, 2014 at 14:48
  • Yes, I use form through Ajavx, but using $apply I got an error: Error: [$rootScope:inprog] $apply already in progress Commented Feb 6, 2014 at 15:01

1 Answer 1

7

This is not a direct answer to the question but rather some comments on using AngularJS and Symfony 2 (S2).

AngularJS really wants to take control of your browser and act as a single page application. It wants the server to passively serve up templates, images etc. It want to transfer chunks of data via json.

The S2 view component really want to generate it's own html via twig. It's based on having a dumb browser that can only render what is sent (complete html pages).

Twig is not over compatible with AngularJS templates especially since they both use curly brackets.

The S2 form html generator adds an additional layer of complexity since it want to control the input element naming to allow mapping posted data back to objects. AngularJS wants to use it's own form elements and json.

I let each do what it does best. I only have one S2 twig template which is the main entry point to the application. The template serves up the javascripts needed to get AngularJS going.

I then let AngularJS request partial templates formatted the way AngularJS want to see them. S2 does not process the partials and never sees them.

S2 is used to implement a REST like web api. It fetches data and serves it as json. The JMSSerializer package comes in handy here. AngularJS controllers take care of handling the data on the browser side and mapping it to the AngularJS forms.

The missing pieces of the puzzle is using the S2 form component for validation. At present, I don't use the form component at all. The serializer helps me to map json data onto my objects (replaces the $form->handleRequest functionality) and then calls the validator directly (replaces the formIsValid).

It works reasonably well though I do miss the form component sometimes. Being able to easily reuse form types is nice.

I'd really like to have the S2 form component be able to process json directly. Which it probably can be made to do. Just need to dig into it more.

===============================================

Here is an example of a AngularJS form mapped to a AngularJS model

<form name="form" novalidate class="simple-form css-form">
ID: {{ person.id }}<br />

Full Name:  <input type="text"  ng-model="person.name_full"  name="person_name_full" required /><br />
<div ng-show="form.person_name_full.$dirty && form.person_name_full.$invalid">Invalid:
    <span ng-show="form.person_name_full.$error.required">Full Name is required.</span>
</div>

First Name: <input type="text"  ng-model="person.name_first" /><br />
Last Name:  <input type="text"  ng-model="person.name_last"  /><br />

Email: <input type="email" ng-model="person.email" name="person_email" required /><br />
<div ng-show="form.person_email.$dirty && form.person_email.$invalid">Invalid:
    <span ng-show="form.person_email.$error.required">Email is required.</span>
    <span ng-show="form.person_email.$error.email">This is not a valid email.</span>
</div>

The big challenge with using twig to generate this is the element name property. Dumb html forms cannot be nested and require each name to be unique. This, S2 implements it's own rather verbose naming convention which is partially based on element order and thus difficult to predict.

AngularJS needs the names to deal with client side validation which in turn can make your application more responsive and use less bandwidth.

S2 needs the names to do server side validation. It's a basic conflict.

AngularJS supported nested forms and nested json data. So we can keep the names simple.

This is why I don't think the current S2 form component plays nicely with AngularJS. And why having a S2 form component that does support AngularJS would be great.

Sign up to request clarification or add additional context in comments.

6 Comments

You can change the curly braces to whatever you like, in AngularJS
About curly braces I use {% verbatim %}{{ someexpression }}{% endverbatim %} inside my twig templates.
Yep and yep on the workaround. However, if I writing an AngularJS app then I want to use the common AngularJS conventions as much as possible. Having to change my default braces just to support PHP TWIG is really not acceptable especially since some other server side template generator might have a conflict with whatever I choose for my customized tags.
Also yep on using verbatim but then your twig code becomes littered with these blocks and if you forget then it can be hard to debug.
in twig i use {{ '{{ angular_var }}' }} to render angularjs vars
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.