Menu
  • HOME
  • TAGS

KnockoutJS Validation - nested ViewModel as validatedobservable

knockout.js,knockout-validation

The isValid is added by the function validatedObservable and will therefore only be available inside the MasterViewModel. You could use ko.validation.group when you want to check if the model is valid inside the model itself: function AddUserViewModel() { var self = this; self.username = ko.observable().extend({ required: true }); var errors...

How do I supposed to use Knockout Validation and Knockcout X-editable at the same time?

knockout.js,twitter-bootstrap-3,knockout-validation,x-editable

My configuration was actually like this: self.num1 = ko.observable().extend({ number: true, min: 0.01, max: 10e10, numeric: 2 });. The last thing "numeric" is my own validation binding and it interferes with the ko validation in a way that no validation happens (no error message either). If you want to use...

How can I dynamically disable a text box using Knockout's observables?

knockout.js,knockout-mapping-plugin,knockout-validation,knockout-mvc

Since Textboxcontrol is an observable, you must change its value by calling it as a function. Plus, you must use this to access the Textboxcontrol property inside your "Init" function : var viewModel = { Name: ko.observable(''), Textboxcontrol: ko.observable(true), Register: { Init: function () { this.Textboxcontrol(false); } } }; ko.applyBindings(viewModel);...

how to add knockout validation to a previously not required field

knockout.js,knockout-validation

$parent etc is a view only feature, anyway, there are alot of errors in your code here is a working version¨ http://jsfiddle.net/Cf8Ap/7/ self.cardNumber = ko.observable().extend({ required: { message: 'number is required', onlyIf: function() { return self.showCardNumber(); } } }); ...

Knockout Validation not clearing error template

knockout.js,knockout-validation

I've actually managed to find the resolution to this via a fiddle I stumbled across. The data-bind="visible: field" bit that I took out is the all important aspect. It seems field is the knockout validation item in context (which is why I couldn't figure out where it came from). Binding...

insertValidationMessage knockout not working

jquery,knockout.js,knockout-validation

The init method only supports overriding configuration properties, so if you want to override methods you need to do it directly on the ko.validation object. So your initialization code should look like this: ko.validation.init({ // registerExtenders: true, messagesOnModified: false, insertMessages: true, parseInputAttributes: true, messageTemplate: null, grouping: { deep: true, observable:...

Custom mapping the child array of objects and adding validation

knockout.js,knockout-mapping-plugin,knockout-validation

There are several different ways to achieve this. One approach is to use the arrayMap utility function to call your function for each entry in player.Trophies this.Trophies = ko.observableArray(ko.utils.arrayMap(player.Trophies, myTrophy)); Then change myTrophy to return an object var myTrophy = function (trophy) { return { Name: ko.observable(trophy.Name).extend({required: true}), Year: ko.observable(trophy.Year).extend({required:...

Validation message not appearing

javascript,knockout.js,knockout-validation

Updated example You're missing the value binding on the select element. It should be <select data-bind="value: name, options: $root.model.lookups.grandSlams, optionsCaption:'Select...'"></select> Updated viewmodel if you want to validate on addSchedule (you can probably remove this.actions.showErrors() from save now, I left it just in case) var viewmodel = { model: { lookups:...

Formatting a number in a TextBox onBlur and onLoad ? using Knockout

knockout.js,knockout-validation,knockout-kendo

You can create two inputs: Input 1 is readonly & visible by default and contains the formatted value. When Input 1 is clicked, hide it, show and focus Input 2 with the real value that can be edited. When Input 2 is 'blurred', hide it and show input 1 with...

knockout validation on twitter boostrap modal poup

knockout.js,knockout-validation

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

Knockout validation only working on one of two date fields

asp.net-mvc-4,knockout.js,knockout-validation

You are making your date custom binding "validation compatible" only in its init function which will be only called when the binding is first used in the HTML. That is why the validation was only worked for the second input. In order to fix this you have to move the...

Knockout-validation not displaying error message for select multiple

javascript,knockout.js,knockout-validation

The validation plugin only amends the value and checked binding to make them automatically "validation" compatible. So the selectedOptions is not automatically validated so you need to call makeBindingHandlerValidatable with the selectedOptions before the ko.applyBindings to make the validation work with your multi select: ko.validation.makeBindingHandlerValidatable('selectedOptions'); ko.applyBindings(new viewModel); Demo JSFiddle. As...

Knockout Validation on drop down list always displaying error message

javascript,knockout.js,knockout-2.0,knockout-validation

To remove the "initlial" validation message you need to initialize your State property with an empty string: self.State = ko.observable("").extend({ required: { message: "You must select a state." } }); Demo JSFiddle. You need to do this because when writing ko.observable() it will be initialized with undefined however when knockout...

knockout validation multiple messages

knockout.js,knockout-validation

I should've read the source code in the first place - might be useful for someone else though callback({ isValid: isValid, message: _this.voucherDetails.voucherState.display() }); This does the trick!...

Knockout validation require unknown knockout js file

knockout.js,requirejs,knockout-validation

Knockout and also the Knockout-Validation plugin support both the CommonJS and the AMD module pattern, so there is no need to shim them. You get the error because the Knockout-Validation plugin explicitly depends a module named "knockout" (see on GitHub) so to fix it you need to rename your 'knockout-3.2.0'...

KnockoutJS Live Validation Localization

knockout.js,knockout-validation

Your code updates the localization, but the new messages will work only on the next update. Replace switchLanguage with this: this.switchLanguage = function() { // If I localize later, nothing is changed. ko.validation.localize({required: 'eh... sorta?'}); for (var prop in this) if (ko.isObservable(this[prop]) && typeof(this[prop].valueHasMutated) === 'function') this[prop].valueHasMutated(); }; Fiddle...

Kockoutjs Validation triggers errors messages on blind

knockout.js,knockout-validation

Your two examples are not quite the same.... if you would use the old validation plugin in your new code: http://jsfiddle.net/ex9zLfde/ it would also display the validation message initially. So the problem is not with the plugin version but with your actual code in your showAllMessages computed: self.showAllMessages = ko.computed(function...

Multiple charts rendering on top of each other

javascript,knockout.js,knockout-validation,durandal-2.0,chartjs

After moving $(window).resize(respondCanvas) out of my AJAX success callback, here is a simplified version of what I have that is working: var chartData = {}; var max = 0; var steps = 10; function respondCanvas() { var c = $('#summary'); var ctx = c.get(0).getContext("2d"); var container = c.parent(); var $container...

Get bindings from observable

knockout.js,knockout-validation

After a while I came with something like this: function getPath(source, propertyName /*, value, result, noDescend */) { var result = arguments[3] || {}; var valueToFind = arguments[2]; var noDescend = arguments[4]; function merge(dst, name, src) { var result = {}; result[name] = src; return $.extend(true, dst, result); } for...

Knockout validation - Config position (top or bottom) span error

knockout.js,knockout-validation

My temporary solution: Overriding InsertValidationMessage. $ko.validation.insertValidationMessage = function (element) { var span = document.createElement('SPAN'); span.className = "validationMessage"; element.parentNode.insertBefore(span, element); return span; }; This functionality is already in the plans of the Knockout Validation team. Fiddle ...

Why is this pattern not working with knockout validation? (delimited zip codes)

javascript,regex,knockout.js,knockout-validation

The RegularExpressionAttribute which is used in ASP.NET MVC implicitly checks for a full length match starting from the first index. So it is essentially converting your the pattern to: /^(\d{5}(\s?)*,?(\s?)*)+$/ However the KO validation is only working with the provided pattern without any additional logic, there you need to manually...

How to know which field is invalid in knockout validation?

javascript,knockout.js,knockout-validation

I guess you should be looking for something like this // Getting errors var errors = ko.validation.group(this, { deep: true, observable: false }); // New method: getting extended details var details = errors.getDetails(); for (var i = 0; i < details.length; i++) { var d = details[i]; /* Every element...

How do I call an observable property within its own validation extender?

javascript,knockout.js,knockout-validation

The solution was to call the extend function on a new line - can anyone explain why? _self.CountryOfPractice = ko.observable(model.CountryOfPractice); _self.CountryOfPractice.extend({ required: { message: "You must select a country", onlyIf: function () { return (_self.ConfirmationOption() === '4' && (_self.CountryOfPractice() === null || _self.CountryOfPractice() === undefined)); } } ...

Knockout X-Editable success handler is not called

twitter-bootstrap,knockout.js,knockout-validation,x-editable

I couldn't do what you were trying to do with the success binding (I don't think you can do it) but I instead just used Knockout with a subscribe. Declare Applicant ViewModel as we need employeeId to be an observable to subscribe to: var Applicant = function (model) { var...

When will be the extender function will get called

javascript,knockout.js,knockout-validation,knockout-mvc

Although it is explicitly not stated in the documentation but any custom extender definition has to come before the first time you use it. So move the ko.extenders.logChange part outside of your ViewModel function: ko.extenders.logChange = function(target, option) { alert("log change function") target.subscribe(function(newValue) { alert("subscribe function: "+option + ": "...

Knockout Validation not working with a DatePicker bindingHandler

javascript,jquery-ui,knockout.js,knockout-2.0,knockout-validation

The validation plugin only hooks into the value, checked, textinput and selectedOptions bindings. If you want to make your custom binding trigger the validations you need to call the validation.makeBindingHandlerValidatable method of the plugin and pass in the name of your custom binding: ko.bindingHandlers.datepicker = { init: function (element, valueAccessor,...

Knockout binding with custom extender and validation

knockout.js,knockout-mapping-plugin,knockout-validation

Well you should notice here that onLoad your textbox value is not a number(100 €) and you are trying to compare against a number using max:130 gives you always false. Using Custom validation we can deal this issue effectively Custom Validation Rules : ko.validation.rules['maxval'] = { validator: function (val, otherVal)...

Knockout Validation on Collections of validatable objects

javascript,jquery,validation,knockout.js,knockout-validation

So after a lot of trail and error I've found a solution. Not sure if it's the BEST solution, but it works fine. I've gotten rid of ko.validation.rules['collectionValidator'] and added a validator grouping. self.HrAdminsErrors = ko.validation.group(self.HrAdmins, {deep: true, live: true}); The operational code is at the following fiddle: http://jsfiddle.net/3b3o87dy/6/...

show validation message after user enters invalid value and focused out and then on keyup,using knockout validation

javascript,knockout.js,knockout-validation

Your fiddle example is using Knockout 2.3. If you use Knockout 3+ you can use the textInput binding. The textInput binding links a text box () or text area () with a viewmodel property, providing two-way updates between the viewmodel property and the element’s value. Unlike the value binding, textInput...