Reputation: 6045
I have a observable in my function and i just want to apply required
validation to it . so i done like this
View model:
self.textareadata = ko.observable().extend({required:true});
var errors = ko.validation.group(self.textareadata);
On button click i am trying to fire up the validation like
self.click = function()
{
if(errors().length <=0)
{//post data}
else
{errors.showAllMessages();}
}
// i tried errors.length
but its failing me i.e length always zero
.
Using the above if condition on button click if i dont have any data in my text area control i get the message this field is required
but when i enter something(automatically messages clear's) and again click means here i am stuck i.e else
condition firing not expected if
.
So debugged and checked i.e i can find a index[0] of null value . This i making my if condition fail .
I try validatedobservable
if i use multiple observable validation but as it's a single one i want to do it by group
.
Fiddle attached click Here
Anything i'm missing here & Help is appreaciated .
Upvotes: 0
Views: 1004
Reputation: 19882
You need to provide array in group
self.errors = ko.validation.group([self.textareadata]);
Then use it like this
self.click = function()
{
if(self.errors().length == 0)
{
//post data
}else{
errors.showAllMessages();
}
}
Upvotes: 1