Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required Really Confuses Me. #204

Open
hjc opened this issue Feb 5, 2014 · 1 comment
Open

Required Really Confuses Me. #204

hjc opened this issue Feb 5, 2014 · 1 comment

Comments

@hjc
Copy link

hjc commented Feb 5, 2014

So, to me, the behavior of the required rule is just odd.

Here's an example, here's my validation schema:

    {
            payment_state: [{
                oneOf: _.pluck(Data.Lookup.States, 'val')
                , msg: "PAYMENT ADDRESS: PLEASE ENTER A VALID ADDRESS STATE"
            }, {
                required: true
              , msg: "PAYMENT ADDRESS: PLEASE ENTER A VALID ADDRESS STATE"
            }]
          , payment_zip: [{
                required: true
              , msg: "PAYMENT ADDRESS: PLEASE ENTER A VALID ZIP"
            }, {
                length: 5
                , msg: "PAYMENT ADDRESS: PLEASE ENTER A VALID ZIP (5 DIGITS LONG)"
            }, {
                pattern: /^\d{5}$/ //'number'
              , msg: "PAYMENT ADDRESS: PLEASE ENTER A VALID ZIP (FULLY NUMERIC AND 5 DIGITS LONG)"
            }, {
                fn: function(val, field, newAttrs) {
                    var valid_zip = Backbone.Validation.closures.zip_state(val, field, newAttrs);

                    if (valid_zip !== false) {
                        return valid_zip;
                    }
                }
            }]
          , cc_last_4: [{
                required: false
            }, {
                pattern: 'number'
              , msg: "YOUR CREDIT CARD'S LAST FOUR DIGITS MUST BE NUMBERS"
            }]
          , number: [{
                required: true
                , msg: "PLEASE ENTER A CREDIT CARD NUMBER"
            }, {
                pattern: 'number'
              , msg: "YOUR CREDIT CARD NUMBER MUST BE ONLY NUMBERS"
            }, {
                maxLength: 19
              , msg: 'VALID CREDIT CARD NUMBERS DO NOT HAVE MORE THAN 19 DIGITS'
            }, {
                minLength: 12
              , msg: "VALID CREDIT CARD NUMBERS HAVE AT LEAST 12 DIGITS"
            }]
          , cvc: [{
                required: true
              , msg: "PLEASE ENTER YOUR CARD'S CVC NUMBER (SECURITY CODE)"
            }, {
                maxLength: 4
              , msg: "CVC NUMBERS (SECURITY CODE) DO NOT HAVE MORE THAN 4 DIGITS"
            }, {
                minLength: 3
              , msg: "CVC NUMBERS (SECURITY CODE) MUST HAVE AT LEAST 3 DIGITS"
            }, {
                pattern: 'number'
              , msg: "YOUR CREDIT CARD'S CVC (SECURITY CODE) MUST BE ONLY NUMBERS"
            }]
          , exp_date: [{
                futureDate: true
              , msg: "YOUR CREDIT CARD HAS EXPIRED"
            }, {
                required: true
              , msg: "PLEASE ENTER AN EXPIRATION DATE FOR YOUR CREDIT CARD"
            }]
          , "cred-name": [{
                required: true
              , msg: "PLEASE ENTER A BILLING NAME FOR YOUR CREDIT CARD"
            }]
    }

(The zip's fn rule does a synchronous XHR to our server to see if the zip belongs to the state).

This is for a model representing a purchase. As you can see, all fields other than cc_last_4 are required. This is because when making a purchase we require all of your credit card information (except cc_last_4, which Stripe gives us). We then send that to Stripe and do not store it ourselves. As such, when you go to edit a purchase or save it at all, we don't have the CC info and it cannot be sent back to the server. That's fine, no issues there. Here's the confusing part.

If a rule other than the ones for cred-name, exp_date, cvc, and number fails, then the required rules are triggered for all of the above fields, and they, of course fail because we don't have them in the DB (all of this works fine). Here's the weird part, if NO other rules fail at ALL, the above 5 fields do NOT have their required rule failed, but it is run (tested by making one required rule a function with a log).

This behavior confuses the hell out of me. Can you explain it?

Note: I can use preValidate to get around any issues, which is what I'm doing. I'm just really curious.

@chiefGui
Copy link
Collaborator

I don't know if I understood well, but what you're saying exactly is wether an input fail using another rule which isn't required, the required is triggered for all the fields?

Am I right? Because if so, it could be an older version of Backbone.Validation or a bug.

Please, confirm this information for me to continue investigating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants