0

我正在使用 Angular js 在 symfony2 中处理注册表单。我的疑问是我们能否以 symfony2 形式进行 Angular js 验证?因为我无法验证表单,而且我不知道如何将表单值发布到 symfony 控制器..请帮助..我被困在项目中..

请检查我的registration.html.twig 代码..

<body ng-app="LoginApp">
    <div id="register" class="animate form" ng-controller="RegisterController as registerCtrl">
            {{ form_start(form, {'attr': {'name':'registerFrm', 'id':'registerFrmId','novalidate': '', 'ng-submit':'registerCtrl.registerFrm()'}}) }}
                Your username
                {{ form_row(form.username) }}
                <span class="error" ng-show="submitted && registerFrm.adminuser[username].$error.required">Username is required!</span>

                Your email
                {{ form_row(form.email) }}
                <span class="error" ng-show="submitted && registerFrm.adminuser[email].$invalid">Invalid email</span>

                Your password
                {{ form_row(form.plainPassword.first) }}
                <span class="error" ng-show="submitted && registerFrm.adminuser[plainPassword][first].$error.required">Password is required!</span>

                Please confirm your password 
                {{ form_row(form.plainPassword.second) }}
                <span class="error" ng-show="submitted && registerFrm.adminuser[plainPassword][second].$error.required">Password is required!</span>

                <p class="signin button"> 
                    <input type="submit" name="signup" ng-model="signup" ng-click="submitted=true" value="Sign up" /> 
                </p>
            {{ form_end(form) }}
        </div>

        <script>
            var App = angular.module('LoginApp',[]);

            App.controller('RegisterController', ['$scope',function($scope) {

            }]);
        </script>
    </body>

我的 symfony 表单生成器

$builder
->add('username', TextType::class , array( 
    'label'  => false,
    'attr'   =>  array(
        'ng-model' => 'formData.username',
        'id' => 'usernamesignup',
        'placeholder' => 'myusername690',
        'required' => false)
    ))
->add('email', TextType::class , array( 
    'label'  => false,
    'attr'   =>  array(
        'ng-model' => 'formData.email',
        'id' => 'emailsignup',
        'placeholder' => 'mymail@mail.com',
        'ng-pattern' => '/^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/',
        'required' => false)
    ))
->add('plainPassword', RepeatedType::class, array(
    'type' => PasswordType::class,
    'first_options'  => array(
        'label' => false,
        'attr'   =>  array(
            'ng-model' => 'formData.password',
            'id' => 'passwordsignup',
            'placeholder' => 'eg. X8df!90EO',
            'required' => false)
        ),
    'second_options' => array('label' => false,
    'attr'   =>  array(
            'ng-model' => 'formData.confirm_password',
            'id' => 'passwordsignup_confirm',
            'placeholder' => 'eg. X8df!90EO',
            'required' => false)
        ),
    )
);  

报名表图片

4

2 回答 2

1

Hey Dmitry Grachikov,

Please change your code

<span class="error" ng-show="submitted && registerFrm.adminuser[username].$error.required">Username is required!</span>

to

<span class="error" ng-show="submitted && registerFrm['adminuser[username]'].$error.required">Username is required!</span>

Your code having syntactic JS error.

If you want to reference a property named adminuser[username], you should do it like this:

registerFrm['adminuser[username]'].$error

registerFrm.adminuser[username] is incorrectly interpreted as (registerFrm.adminuser)[username]

于 2016-09-20T09:42:02.637 回答
0

最简单的方法是在 twig 中呈现您的表单,以获取带有字段名称的表单的 html。之后,您必须在表单提交请求中使用完全相同的字段名称,symfony 表单组件将处理它。

于 2016-06-22T08:13:10.953 回答