AngularJS在表单字段名称中与[]不匹配

时间:2015-06-10 09:56:23

标签: angularjs forms validation

我在Symfony2表单中使用AngularJS进行验证。表单字段的名称由Symfony2给出,在这种情况下,作为&#34; license [domain]&#34;,并且验证工作很棒,但AngularJS看起来并不适合使用名称中的大括号。< / p>

嫩枝:

<div class="col-md-12 has-feedback" ng-class="{
    'has-error': !license.{{ form.domain.vars.full_name }}.$valid,
    'has-success': license.{{ form.domain.vars.full_name }}.$valid
     }">   
     {{ form_widget(form.domain, {
         'attr': {
                 'ng-model': "licenseDomain",
                 'class': "form-control",
                 'ng-pattern': "/^[0-9a-z-]+$/"
                  }
         }) }}
     <span class="glyphicon form-control-feedback" aria-hidden="true" ng-class="{'glyphicon-ok': licenseForm.{{ form.domain.vars.full_name}}.$valid,
'glyphicon-remove': !licenseForm.{{ form.domain.vars.full_name }}.$valid }"></span>

{% verbatim %}
<tt>{{ license }}</tt><br/><br/>
<tt>license.{% endverbatim %}{{ form.domain.vars.full_name }}{% verbatim %}.$valid = {{license.{% endverbatim %}{{ form.domain.vars.full_name }}{% verbatim %}.$valid }}</tt><br/>
<tt>{% endverbatim %}{{ form.domain.vars.full_name }}{% verbatim %}.$valid = {{{% endverbatim %}{{ form.domain.vars.full_name }}{% verbatim %}.$valid }}</tt><br/>
<tt>license.domain.$valid = {{license.domain.$valid }}</tt><br/>
<tt>license.licenseDomain.$valid = {{license.licenseDomain.$valid }}</tt><br/>
<tt>license.$valid = {{license.$valid}}</tt><br/>
{% endverbatim %}
</div>

渲染TT部分

<tt class="ng-binding">
    {"$error":{"required":[{"$validators":{},"$asyncValidators":{},"$parsers":[],"$formatters":[null],"$viewChangeListeners":[],"$untouched":true,"$touched":false,"$pristine":true,"$dirty":false,"$valid":false,"$invalid":true,"$error":{"required":true},"$name":"license[domain]","$options":null}]},"$name":"license","$dirty":false,"$pristine":true,"$valid":false,"$invalid":true,"$submitted":false,
         "license[domain]":{"$validators":{},"$asyncValidators":{},"$parsers":[],"$formatters":[null],"$viewChangeListeners":[],"$untouched":true,"$touched":false,"$pristine":true,"$dirty":false,"$valid":false,"$invalid":true,"$error":{"required":true},"$name":"license[domain]","$options":null}}
</tt><br><br>    
<tt class="ng-binding">license.license[domain].$valid = </tt><br>
<tt class="ng-binding">license[domain].$valid = </tt><br><tt class="ng-binding">license.domain.$valid = </tt><br>
<tt class="ng-binding">license.licenseDomain.$valid = </tt><br>
<tt class="ng-binding">license.$valid = false</tt><br></div>

验证工作完美,但不是对该领域的引用。如您所见,最后一个TT呈现许可证。$ valid为false,第一个(许可证转储)显示&#34;许可证[域名]&#34;对象,但我尝试在两者之间引用它的方式都没有。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我终于设法用 []代替。引用来引用它。

<强> 呼....

TWIG:

<tt>license["{% endverbatim %}{{ form.domain.vars.full_name }}{% verbatim %}"].$valid = {{license["{% endverbatim %}{{ form.domain.vars.full_name |e }}{% verbatim %}"].$valid }}</tt><br/>

呈现TT:

<tt class="ng-binding">license["license[domain]"].$valid = true</tt>

看起来非常难看,但...... :(