Prevent Knockout validation code to execute on load

1,124 views
Skip to first unread message

Ankush Brahmbhatt

unread,
Jul 19, 2013, 5:38:27 AM7/19/13
to knock...@googlegroups.com
Hello,
 
I have implemented a very basic required validation on "first name" largely based on the example suggested on knockout website (http://knockoutjs.com/documentation/extenders.html) - Live example 2: Adding validation to an observable.
 
My problem is that I don't want the validation to fire when the form is first loaded.
 
You will see the demo of issue I am facing at this link  http://jsfiddle.net/tCP62/22/
 
Please can anyone help me resolve this.
 
Regards,
Ankush

Ankush Brahmbhatt

unread,
Jul 19, 2013, 5:48:00 AM7/19/13
to knock...@googlegroups.com
Please Note that the JSFiddle link that i have provided to demo showing the problem - works in "Chrome" and does not work in IE. 

Baz

unread,
Jul 19, 2013, 2:01:45 PM7/19/13
to knockoutjs
If I comment out the initial validation "validate(target());" in your fiddle is seems to work as expected.


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

Ankush Brahmbhatt

unread,
Jul 19, 2013, 3:30:12 PM7/19/13
to knock...@googlegroups.com
Hello, Many thanks for your reply.
 
Yes. That worked. I was not able to see the problem in the grand scheme of things where I was implementing similar thing for my project.
 
This was immensely helpful. Thank you very much.
 
Regards,
Ankush
Message has been deleted

Ankush Brahmbhatt

unread,
Jul 19, 2013, 7:09:44 PM7/19/13
to knock...@googlegroups.com
Now I have another problem. I want to invoke the validation on save button click so that controls that have error get highlighted. Based on the above, I could invoke "validate" before save and check for "hasError" property. However what if I have two validations on "firstName" - say "required" as well as that firstName should start with "A" for which there is another extender. In this scenario, do I have to name the validate method differently on each extender and invoke all the validate methods for each observable that needs to be validated? Any help is very much appreciated

Baz

unread,
Jul 20, 2013, 3:30:04 PM7/20/13
to knockoutjs
Complex validation is interesting, and requires a more sophisticated architecture than simple validation. You have to keep track of multiple possible errors (not just one hasError) and things like that. You should take a look at this project for ideas or to use it: https://github.com/Knockout-Contrib/Knockout-Validation

Ankush Brahmbhatt

unread,
Jul 22, 2013, 3:28:45 AM7/22/13
to knock...@googlegroups.com
Thank you. I will have a look at the validation plugin.
Reply all
Reply to author
Forward
0 new messages