0

私は Alloy UI と YUI3 を使用しており、選択ドロップダウンからのユーザーのオプション選択に応じて、サーバーへの ajax 呼び出しを開始するフォームを持っています。サーバーは、AUI-Form-Validation モジュールに送信する新しい検証ルールを返します。ルールは正常に変更されていますが、フォームは重複したルールを出力しています。つまり、フォーム検証インスタンスを置き換えるのではなく、古いものに追加するので、ブラウザにエラー フィールド文字列が重複しています。最新のものを除くすべてのインスタンスを破棄する必要があると思いますが、これを達成できないようです。古いフォーム検証を破棄/更新して、DOM に最新のものだけを表示するにはどうすればよいですか?

これが私のコードです(ローカルでテストしているので、on.failureを使用しています):

<!DOCTYPE html>
<html>
<head>
    <title>Form Validation</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="http://cdn.alloyui.com/2.5.0/aui-css/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>

    <div class="container-fluid">
        <div class="row-fluid">
            <div class="span12">

                <form id="myForm">

                    <select id="card-type" name="card-type" class="select_card_type">
                        <option value="7" selected="selected" label="Seven"></option>
                        <option value="2" label="Two"></option>
                        <option value="5" label="Five"></option>
                    </select>

                    <div class="control-group card_number">
                        <label class="control-label" for="cardNumber">Card number</label>
                        <div class="controls">
                            <input name="cardNumber" id="cardNumber" type="text" />
                        </div>
                    </div>

                    <div class="control-group csc">
                        <label class="control-label" for="picture">Card security code</label>
                        <div class="controls">
                            <input name="csc" id="csc" type="text">
                        </div>
                    </div>

                    <input class="btn btn-info" type="submit" value="Submit">

                </form>
            </div><!--/span-->
        </div><!--/row-->

        <hr>

        <footer>
            <p>&copy; Company 2013</p>
        </footer>

    </div><!--/.fluid-container-->

    <script src="http://cdn.alloyui.com/2.5.0/aui/aui-min.js"></script>
    <script>
        YUI().use('node', 'node-base', 'event', 'transition', 'aui-io-request', 'json-parse', 'aui-form-validator', function(Y) {

            var rules;
            function Validator(rules) {
                this.rules = rules;
                this.fieldStrings = {
                    cardNumber: {
                        required: 'Type your card number in this field.'
                    },
                    csc: {
                        required: 'Please provide your csc.'
                    }
                };

                this.val = new Y.FormValidator(
                    {
                        boundingBox: '#myForm',
                        fieldStrings: this.fieldStrings,
                        validateOnInput: true,
                        rules: this.rules
                    }
                );
            }
            Y.one('.select_card_type').on('change', function(e) {

                var len = Y.one('#card-type option:checked').get('value');

                Y.io.request('<%= selectProductTypeResource.toString() %>', {
                    method: 'post',
                    on: {
                        failure: function() {
                            rules = {
                                cardNumber: {
                                    required: true,
                                    digits: true,
                                    minLength: len,
                                    maxLength: len
                                },
                                csc: {
                                    required: true,
                                    digits: true,
                                    minLength: len,
                                    maxLength: len
                                }
                            };
                            if (typeof (validator) === 'object') {
                                validator.val.destroy(true); // not working
                            }
                            validator = new Validator(rules);
                        }
                    }
                });
            });
        });</script>
</body>
</html>
4

2 に答える 2

0

ルールを追加するのではなく、複数のバリデーターをバインドしています。ルールをクリアできたとしても、古いバリデーターはまだバインドされており、ルールに対して検証していません。

「破棄」する必要がないようにするには、バリデーターを一度作成してから、それに応じてルールを変更します。

AlloyUI はget/setメソッドを使用してプロパティを変更します。

  • ルールを変更するには、getter を呼び出して - var rules = validator.get('rules')- 変更します。
  • 新しいルールを設定するには、setter を呼び出します。validator.set('rules', {cardNumber: {required: true}})

実演するフィドルの例をセットアップしました。クリックするCreate New ValidatorSubmit、説明したのと同じ動作が表示されます。


githubのaui-form-validator ソース

于 2014-06-07T15:01:37.570 に答える