How can I Destroy AUI-Form-Validator so I only have one instance

Karma Kaos

I am using Alloy UI and YUI3 and have a form which, depending on the the users option choice from a select dropdown, fires an ajax call to the server. The server returns new validation rules which I send to the AUI-Form-Validation module. The rules are changing fine but the form is outputting duplicate rules. That is, not replacing the form validation instance but appending to the old one so I have duplicate error field strings in the broswer. I think I need to destroy all instances but the latest but cannot seem to achieve this. How can I destroy/renew the old form validation so I only ever have the latest displayed in the DOM?

Here is my code (I am using on.failure as am testing locally):

<!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>
Karma Kaos

Thanks for the reply origineil, I've implemented what you mention and got it working. I used the setAttrs to reset the new rules and all is now working as expected. Here is my full working code:

YUI().use('node', 'node-base', 'event', 'transition', 'aui-io-request', 'json-parse', 'aui-form-validator', function(Y) {

  var validator = new Y.FormValidator(
   {
     boundingBox: '#myForm',
     validateOnInput: true
   });

  Y.one('.select_card_type').on('change', function(e) {
    var len = Y.one('#card-type option:checked').get('value');
    validator.resetAllFields();

    Y.io.request('<%= selectProductTypeResource.toString() %>', {
                    method: 'post',
                    on: {
                        failure: function() {
                            var rules = {
                                cardNumber: {
                                    required: true,
                                    digits: true,
                                    minLength: len,
                                    maxLength: len
                                },
                                csc: {
                                    required: true,
                                    digits: true,
                                    minLength: len,
                                    maxLength: len
                                }
                            };
                            validator.setAttrs({rules: rules});
                        }
                    }
    });
  });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can i have only one instance of Calendar object

How can I filter an ArrayList in Kotlin so I only have elements which match my condition?

How can I use text-align-last except when I have only one line of text?

How can I dynamically create a validator and bind to a form group?

How can I rewrite the programs, so that I don't have to call `flex` but only call `bison` and `cc`?

How can I inject fragment into activity so that I don't have to manually create its instance?

How can I have 2 actions within one form, one button?

How do i structure my Python submodule with a lot of files, so that i have to import only one module

How can I query only one instance of a label?

How can I use Redux to only update one instance of a component?

How can I have only one onChange for several inputs in reacthooks?

How can I have only one checkbox checked out of two?

How can I transpose my data so it only have one row per group in R?

How can I enforce a rule where only one instance of a class can have a certain parameter set to true?

How can I destroy fotorama

How can I convert this excel file so that it is not only one row?

How can I only have one item highlighted?

How can i analysis the output of the three machine learning model so that i only get the one result?

How would I use multiple radio buttons and multiple groups in one form so that only 1 button per row and column can be selected?

I have multiple layouts but how can i select only one of the layout out of those(like a quiz option)

If I have multiple datagridviews on one form, how would I allow a row selection to be active on only one at a time?

I can't make the 3/4 circle and i have to use one div only so how can i make it as the example in the link below

In Django, how can I have a function where I can only update the value instead of creating a new instance of it?

How can I use get_valid_primitives when I have only one dataframe in Featuretools?

How can i access certain Values on Form via custom Validator

How can I merge this query so that it returns only one column?

How can I check only one radiobutton in two groups if I have two radiogroups with radiobuttons

How can I issue a warning for only one instance of a module?

2 Form Submit buttons - Can I have a Javascript run when only one submit button is pressed