vivin / regula

Regula: An annotation-based form-validation framework in Javascript
126 stars 24 forks source link

Regula.bind - group bindings fail with multiple elements #37

Closed saunders99999 closed 11 years ago

saunders99999 commented 11 years ago

I can get groups to work with element, but not elements. Should I be able to use groups when binding to multiple elements?

This is Regula version 1.2.3.

WORKS FINE WITH element Regula.bind({ element: element1, constraints: [ {constraintType: Regula.Constraint.Min, params: {value: "18", message: "You must be at least 18 years old to vote", groups:["Vote"]} } ] });

ERROR WITH elements Regula.bind({ elements: [element1, element2], constraints: [ {constraintType: Regula.Constraint.Min, params: {value: "18", message: "You must be at least 18 years old to vote", groups:["Vote"]} } ] });

error in firefox console uncaught exception: regula.bind: Element 2 of 2 failed: The groups parameter must be an array of enums or strings Function received: {elements: [[object HTMLInputElement], [object HTMLInputElement]], constraints: [[object Object]]}

vivin commented 11 years ago

Strange, I believe I have a test for this. You must have uncovered a bug I missed. :) I will get to this after Monday, unfortunately I have family coming down this weekend and so I will not be able to fix this right now. Sorry about that! But I will take a look at it on Tuesday and try to figure out what is wrong.

EDIT: I just took a look at the tests and yes, it seems like I have not tested this case. I will fix it as soon as I can.

saunders99999 commented 11 years ago

There's no rush for me, I am exploring the API.

One more questions with groups, is this intended use? I apply to the same element input1

Then based on a use case, I can dynamically choose to validate group vote vs. group imbibe? I think there are other ways to do this with your API, but trying to understand the usage of group.

Thanks, Mark

    $('#input1').regula('bind', {
        constraints: [
            {constraintType: Regula.Constraint.Min,
             params: {value: "18", message: "You must be at least 18 years old to vote", groups:["Vote"] },
             overwiteConstraint: false,
             overwriteParameters: false
            },
            {constraintType: Regula.Constraint.Min,
             params: {value: "21", message: "You must be at least 21 years old to drink", groups:["Imbibe"] },
             overwiteConstraint: false,
             overwriteParameters: false                 
            }               
        ]
    });         
vivin commented 11 years ago

Groups give you a way to segment validation and constraints. So you can do the following:

regula.validate({
    groups: ["Vote"]
});

regula.validate({
    groups: ["Imbibe"]
});

regula.validate({
    groups: ["Vote", "Imbibe"]
});

Internally regula keeps track of elements and their constraints by using a nested map that is keyed as follows: group -> element-id -> constraint. So the first one will validate #input1 against @Min(value=18), the second against @Min(value=21) and the third one against @Min(value=18) and @Min(value=21).

So based on a use case, you can dynamically pass in a value to the groups parameter and control the validation behavior that way. Hope this answers your question!

On Thu, Feb 14, 2013 at 10:42 AM, saunders99999 notifications@github.comwrote:

There's no rush for me, I am exploring the API.

One more questions with groups, is this intended use? I apply to the same element input1

  • a min constraint of 18, group Vote
  • a min constraint of 21, group Imbibe

Then based on a use case, I can dynamically choose to validate group vote vs. group imbibe? I think there are other ways to do this with your API, but trying to understand the usage of group.

Thanks, Mark

$('#input1').regula('bind', {
    constraints: [
        {constraintType: Regula.Constraint.Min,
         params: {value: "18", message: "You must be at least 18 years old to vote", groups:["Vote"] },
         overwiteConstraint: false,
         overwriteParameters: false
        },
        {constraintType: Regula.Constraint.Min,
         params: {value: "21", message: "You must be at least 21 years old to drink", groups:["Imbibe"] },
         overwiteConstraint: false,
         overwriteParameters: false
        }
    ]
});

— Reply to this email directly or view it on GitHubhttps://github.com/vivin/regula/issues/37#issuecomment-13568204.

Ruin untold; And thine own sadness, Sing in the grass, When eve has forgot, that no more hear common things that gleam and pass; But seek alone to lip, sad Rose of love and ruin untold; And thine own mother Can know it as I know More than another What makes your own sadness, Set in her eyes.

$s="01:11:02:11:01:11:02:13:01:11:01:11:01:13:02:12:01:13:01:11: 04:11:06:12:04:11:01:12:01:13:02:12:01:14:01:13:01:11:03:12: 01:11:04:12:02:11:01:11:01:13:02:11:03:11:06:11:01:11:05:12: 02:11:01:11:01:13:02:11:02:12:01:12:04:11:06:12:01:11:04:12: 04:11:01:12:03:12:01:12:01:11:01:12:01:12:02:11:01:11:01:13: 02:11:01:02:11:01:12:02"; @a=split(/:/,$s);$j="";foreach$c(@a) {@n=split(//,$c);$j.=$n[0]x$n[1];} for($i=0;$i<$j=~y///c;$i+=8){print chr(unpack("N",pack("B32",substr("0"x32 .substr($j,$i,8),-32))));}

vivin commented 11 years ago

Sorry this took so long. I was able to finally take a look at this and I see what is going on. I do some funky stuff with the params map that comes in (modify the groups array into a string which is just... dumb). So I'll go ahead and fix it and make it better too. Thanks once again for bringing this to my notice!