0

Would you please guide me how to display validation message when I click 'Add New Color' and 'Save' button on empty textbox. After starting typing, validation starts showing message. Fiddler Link

self.SaveChanges = function (data, event) {
        var isValid = true;
        for (var prop in data) {
            if (data.hasOwnProperty(prop) && !data[prop].isValid()) {
                isValid = false;
                data[prop].error;
            }
        }
        if (isValid) {
            if (self.OperationMode() == 'A') {
                self.IntColors.push(data);
            }
            self.modalVisible(false);
        }


    };

Thanks in advance!

GSerjo
  • 4,725
  • 1
  • 36
  • 55
Santosh
  • 19
  • 2
  • 6
  • There is an approach here - http://stackoverflow.com/questions/14217921/knockout-validation-only-if-a-specific-button-is-pressed – PW Kad May 09 '14 at 19:47
  • Thank you PW Kad. I tried followed that approach too. In my case, data is another View Model in the observableArray of Master View Model. And data has the View Model to be validated right? And if I do data.errors which is undefined. – Santosh May 09 '14 at 20:39

1 Answers1

0

I made this work as below. Thank you all for helping. Updated JSFiddler

self.SaveChanges = function (data, event) {
            var result = ko.validation.group(data, { deep: true });
            if (!data.isValid()) {
                result.showAllMessages(true);
            }
            else {
                self.modalVisible(false);
            }
        };
Santosh
  • 19
  • 2
  • 6