2

Is it possible to pass array value with square brackets for binding handler?, ie:

<div data-bind="validator: [{class: RequiredValidator}, {class: EmailValidator}]"></div>

It works fine for one object:

<div data-bind="validator: {class: RequiredValidator}"></div>

Class value is not observable, just javascript object.

It throws Message: Unexpected token ) error.

Or I need some other syntax? I could wrap it with object, but prefer not.

I took snapshot of project with this issue, available here: http://balin.maslosoft.com/array-validators/dev/validator.php

Open console, and object validators will show configuration, while array will fail.

Here is fiddle with minimal example: http://jsfiddle.net/piotr/fu8d0hm3/

3 Answers 3

2
+100

It works for these. Might the problem be in your binding handler?

ko.bindingHandlers.validator = {
  init: function(el, va) {
    var value = va();
    console.debug(value);
  }
};

vm = {
  something: ko.observable('hi')
};

ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div data-bind="validator: ['one']"></div>
<div data-bind="validator: [something()]"></div>
<div data-bind="validator: [{class: something()}, {class:'whatever'}]"></div>

Sign up to request clarification or add additional context in comments.

10 Comments

It works here. But for some reason it doesn't work in my project. Here is snapshot available balin.maslosoft.com/array-validators/dev/validator.php - open console for output of validator binding handler.
Your with bindings are failing because you're using "app.model" when the app structure is outside your viewmodel.
app is global variable, it is accessible. Try for example type something in first input and then check in console: app.model.txt1.text. Knockout fails clearly at validator: [....
The first message in the console is Uncaught SyntaxError: Unable to process binding "with: function (){return ko.getObservable($data,"app.model.txt3")||app.model.txt3 }" Message: Unable to parse bindings.
While this does not answer questions, your comments pointed me in right directions, thanks.
|
2

All you need to set a VALID value to the key you are passing . As in you case RequiredValidator is not defined so keep that in quotes to resolve the issue .

view:

<div data-bind="validator: [{class: 'RequiredValidator'}, {class: 'EmailValidator'}]"></div>

viewModel:

ko.bindingHandlers.validator = {
  init: function(element, valueAccessor) {
    console.log(valueAccessor()); //check console window for o/p
  }
}
ko.applyBindings();

check sample here

3 Comments

In ko quotes are not nessesary, ie. validator: {class: RegExpValidator} works fine. Your example works too. However in my case it does not work even with quotes, weird.
ok i get it @PeterM . so you need set valid value to key value pairs . ex: class:hello is undefined where as class:'hello' works fine ,
Yes, I have valid value. I've uploaded full example here: balin.maslosoft.com/array-validators/dev/validator.php open console for results.
0

It turned out that it was problem with knockout-es5 modification for two-way bindings.

Original plugin is not affected. I've created pull request to address this issue.

The problem was binding preprocessing, which produced invalid code if array value was passed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.