How do I get hold of element for which validation has failed

30 views
Skip to first unread message

Raja Malik

unread,
Sep 21, 2013, 4:19:54 AM9/21/13
to regula-v...@googlegroups.com
Hello,

While using Regula, once the validate method is executed and we receive validationResults. How will we know the element for which the validation has failed.

Because I want to show the validation message for the respective input elements.

Thanks
Raja

Vivin Suresh Paliath

unread,
Sep 21, 2013, 2:17:40 PM9/21/13
to Raja Malik, regula-v...@googlegroups.com
Hello Raja,

Each ConstraintViolation object has a failingElements property, which is an array of elements that have failed validation. For a failing constraint on a single input element, the array will only contain the element for which validation failed, for a form-specific constraint, the array will contain all elements in that form, which failed validation.

Vivin


--
You received this message because you are subscribed to the Google Groups "Regula" group.
To unsubscribe from this group and stop receiving emails from it, send an email to regula-validat...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



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

Raja Malik

unread,
Sep 21, 2013, 2:21:59 PM9/21/13
to Vivin Suresh Paliath, regula-v...@googlegroups.com
Hello Vivin,

I debuged the code and found that although I am getting the validations but failingElements array is not having any element, it is empty? Is there something wrong with my code?

Thanks
Raja
--
Thanks
Raja Malik

Vivin Suresh Paliath

unread,
Sep 21, 2013, 2:23:00 PM9/21/13
to Raja Malik, regula-v...@googlegroups.com
Can you post your code? The failingElements array should never be empty.

Raja Malik

unread,
Sep 21, 2013, 2:31:33 PM9/21/13
to Vivin Suresh Paliath, regula-v...@googlegroups.com
Here it is:

<form id="signInForm">
<section id="signInSection">
<input class="large" 
type="text" 
name="username" 
placeholder="Username" 
autofocus
data-constraints="@Required"/><br>
<input class="large" 
type="password" 
name="password" 
placeholder="Password"
data-constraints="@Required"><br>
<button id="signInBtn" class="small">Sign In</button><br>
<label><input type="checkbox"/>Remember me.</label>
<a href="#">Can't access my account.</a>
<img src="../images/cancel.jpg" alt="Cancel Sign In">
</section>
</form>

See attached screen shot also, which shows the elements array is empty.

//validation starts
regula.bind();
var validationResults = regula.validate();

for(var i = 0; i < validationResults.length; i++) {
              var validationResult = validationResults[i];
//here we have to show the message as a placeholder in the corresponding input box.
//with the input box shadow in red color and message in red color.
alert(validationResult.toSource());
//$('#signInSection').find('input').first().attr('placeholder',validationResult.message);
//$('#signInSection').find('input').first().css('border-color', '#FF0000');
        }



--
Thanks
Raja Malik

Screenshot from 2013-09-22 00:00:14.png

Vivin Suresh Paliath

unread,
Sep 21, 2013, 2:36:15 PM9/21/13
to Raja Malik, regula-v...@googlegroups.com
I see that you're trying to essentially stringify a structure that contains DOM elements. That usually will not work. What do you get when you alert(validationResult.failingElements.length)?

Raja Malik

unread,
Sep 21, 2013, 2:39:24 PM9/21/13
to Vivin Suresh Paliath, regula-v...@googlegroups.com
I get 1.
--
Thanks
Raja Malik

Vivin Suresh Paliath

unread,
Sep 21, 2013, 2:41:28 PM9/21/13
to Raja Malik, regula-v...@googlegroups.com
That means that you do have the failing element inside the failingElements array :). To access that element, all you have to do is var element = validationResult.failingElements[0];.

Raja Malik

unread,
Sep 21, 2013, 2:43:59 PM9/21/13
to Vivin Suresh Paliath, regula-v...@googlegroups.com
Thats cool.

I was fooled by .toSource(). Thank you very much for your support.

I hope I can bother you in future if I need your help ;)


--
Thanks
Raja Malik

Vivin Suresh Paliath

unread,
Sep 21, 2013, 2:45:48 PM9/21/13
to Raja Malik, regula-v...@googlegroups.com
It appears that toSource() just ignores circular structures. If you had tried to use JSON.stringify, it would have error'ed out with a message about being unable to stringify circular structures. Glad I was able to help, and I'm happy to help if you have more issues. :)
Reply all
Reply to author
Forward
0 new messages