vivin / regula

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

Conditional validations using Regula #35

Closed saunders99999 closed 11 years ago

saunders99999 commented 11 years ago

This is a question, not a direct issue.

How do I do conditional validations with Regula? My use case is to run a validation on an input field if a check box is checked. Similar cases exist to run a validation based on a radio button or select value... Run validations on a 2nd group on input fields if a checkbox is unchecked, ...

Between compound constraints and grouping of constraints, it seems Regula has the plumbing to do this.

With the existing API, I could see

As an alternative, I would like to see the usage of compound constraints where I could specify the constraint once and then run Regula validation in one shot. I'm not sure this is possible with the current implementation as compound constraints can be applied only to a single field.

Ideally, I would like to

saunders99999 commented 11 years ago

Here's a more succinct way to capture the above...

checkbox input

when a user either leaves the input field or does a page submit, perform validation such that checkbox checked, input blank - validation warning checkbox checked, input filled in - no validation warning checkbox unchecked, input blank or filled in - no validation warning

What is the best way to use Regula to accomplish this?

If this is a better conversation in email, I am at saunders99999@gmail.com.

Thanks, Mark

vivin commented 11 years ago

Hello Mark,

You can do this by creating a custom constraint:

regula.custom({ name: "ConditionalRequired", defaultMessage: "The {label} is required.", validator: function() { if(document.getElementById("CheckboxId").checked) { //check to see if input is non-empty } } });

You can make this a little more generic and applicable for re-use by specifying the id of the checkbox as a parameter:

regula.custom({ name: "ConditionalRequired", defaultMessage: "The {label} is required.", params: ["checkboxId"], validator: function(params) { if(document.getElementById(oarams["checkboxId"]).checked) { //check to see if input is non-empty } } });

Hope this helps!

Vivin

On Sat, Feb 9, 2013 at 2:59 AM, saunders99999 notifications@github.comwrote:

Here's a more succinct way to capture the above...

checkbox input

when a user either leaves the input field or does a page submit, perform validation such that checkbox checked, input blank - validation warning checkbox checked, input filled in - no validation warning checkbox unchecked, input blank or filled in - no validation warning

What is the best way to use Regula to accomplish this?

If this is a better conversation in email, I am at saunders99999@gmail.com .

Thanks, Mark

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

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))));}

saunders99999 commented 11 years ago

@vivin thanks for the reply.

I was trying what you suggested in a few ways last Friday. I got stuck on...

//check to see if input is non-empty.

Here, the ConditionalRequired was in the middle of being processed in a regula.validate. Here, I was trying to

After calling my nested regula.validate on the input field, I couldn't get the response to return. Are nested validates supported? What is the way to invoke a validate from another validate? I didn't want to re-write the Required validation here to check the input field was not empty. I wanted to use what Regula already provides.

If I went astray, just let me know. I thought my approach wasn't intended API usage.

Thanks, Mark saunders99999@gmail.com

vivin commented 11 years ago

You make an excellent point that I realized as I was writing the solution. :) Yes, there is no way currently to perform a conditional validation using in-built regula constraints, and there is unfortunately no way to refer to another validation function from within the existing one. So for the time being, you would have to duplicate what Required already does. It's strange that this limitation didn't occur to me before! However, this is something I would like to fix - I think I might be able to do this by exposing the "core" validation functions somehow. I will need to think about the best way to do this. Please go ahead and open an issue regarding this; I think this will be a very useful feature.

On Sun, Feb 10, 2013 at 1:32 PM, saunders99999 notifications@github.comwrote:

@vivin https://github.com/vivin thanks for the reply.

I was trying what you suggested in a few ways last Friday. I got stuck on...

//check to see if input is non-empty.

Here, the ConditionalRequired was in the middle of being processed in a regula.validate. Here, I was trying to

  • programatically add a @Required https://github.com/Requiredconstraint to the input field if the radio button was checked
  • call regula.validate on the input field
  • after finishing the nested validate, review the returned results
  • then clean up the @Required https://github.com/Required constraint on the input field so it didn't get invoked on any future Regula.validates within the dependency group.

After calling my nested regula.validate on the input field, I couldn't get the response to return. Are nested validates supported? What is the way to invoke a validate from another validate? I didn't want to re-write the Required validation here to check the input field was not empty. I wanted to use what Regula already provides.

If I went astray, just let me know. I thought my approach wasn't intended API usage.

Thanks, Mark saunders99999@gmail.com

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

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))));}