New Posts New Posts RSS Feed: Validation
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Validation

 Post Reply Post Reply
Author
pierom View Drop Down
Newbie
Newbie


Joined: 19-Sep-2012
Location: Italia
Posts: 9
Post Options Post Options   Quote pierom Quote  Post ReplyReply Direct Link To This Post Topic: Validation
    Posted: 25-Sep-2012 at 10:40am
I have many int32 entity properties. Binding to input fields they are converted to string when modified so the validation fails.
Browsing the web I found that this is a problem using ko.
The first solution I found was implement a ko extenders (copied from thei site):
 ko.extenders.numeric = function (target, precision) { ....
and apply to all numeric fields before save:

               function salva() {
                    if (em.hasChanges()) {
                        oModelRuntime.RIGHE_VISIBILI_AP.extend({ numeric: 0 });
                        var isValid = oModelRuntime.entityAspect.validateProperty("RIGHE_VISIBILI_AP");
                        alert(isValid);
                        em.saveChanges()
                            .then(function (saveResult) {
But I have hundreds of fields to apply the extender.
The second, and for me best, solution was to modify your source:

        function intRangeValidatorCtor(validatorName, minValue, maxValue) {
            ctor.messageTemplates[validatorName] = core.formatString("'%displayName%' must be an integer between the values of %1 and %2",
                minValue, maxValue);
            return function () {
                var valFn = function (v, ctx) {
                    if (v == null) return true;
                    if (typeof v === "string")  // added
                        v = parseInt(v, 0);
                    if ((typeof v === "number") && Math.floor(v) === v) {
                        if (minValue != null && v < minValue) {
                            return false;
                        }
                        if (maxValue != null && v > maxValue) {
                            return false;
                        }
                        return true;
                    } else {
                        return false;
                    }
                };
                return new ctor(validatorName, valFn);
            };

Let me know if this is a correct approach except more refinements. If the property type is int32 (number) for me is logical that the validator try to convert if the input value is a string.
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 25-Sep-2012 at 12:25pm
We like your idea of being able to apply a numeric validation to a string value; we'll implement this shortly; keep your eye on the release notes (we're releasing small improvements every few days it seems).
 
We know from metadata when a "field" is numeric. We might think about extending our KO tracking implementation so that it generates KO observables that are extended in the manner you describe as is suggested by the KO documentation. Maybe offer a plugin point so you could define how you want to generate the KO properties for numerics.
 
You could try this yourself. One of the beauties of the adapter pattern is that you can take our adapter and roll your own variation.
 
On the other hand, the current metadata do not capture the numeric field's precision nor how it should be rounded and this kind of intervention at such a deep level seems intrusive.
 
Another option: Breeze will soon have a post-create method that you can define to do anything to entities immediately after they are created or materialized by a query. That could be a place for you to put your own utility to extend the numeric KO properties based on your own model-specific reasoning.  I think we'd lean that way for now.
Back to Top
pierom View Drop Down
Newbie
Newbie


Joined: 19-Sep-2012
Location: Italia
Posts: 9
Post Options Post Options   Quote pierom Quote  Post ReplyReply Direct Link To This Post Posted: 01-Oct-2012 at 5:11am
I noticed that in version 0.60 was implemented "allowstring = true" for int32 validator.

Is it possibile to set this option for the default int32 validator instead to assign it for every properties? 

In the documentation you write:
  • var myValidator = Validator.number( { allowString: true}))
  • myProp.validators.push(myValidator)
But using "push" the validator is added instead of replaced. So the previous one remains active and the validation fails the same.
I use: MyProp.validators[0]=myValidator.


Back to Top
jtraband View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 19-Sep-2012
Posts: 55
Post Options Post Options   Quote jtraband Quote  Post ReplyReply Direct Link To This Post Posted: 01-Oct-2012 at 4:24pm
We hadn't thought about replacing the standard numeric validators with custom validators with the same names but providing this ability IS a good idea.  We'll try to get something like this into one of the next few releases.

Of course, right now you can remove any validator from any validators collection and add a new custom one yourself.

What we didn't understand from your original post was what the underlying data model looks like for this app.  Are the underlying model properties numbers or strings?  I am assuming they must be strings in which case you must be adding the 'numeric-allowString' validators yourself on the client.  Numeric validators will only get added automatically if the data type of the property is actually numeric, in which case, I didn't understand how a 'numeric-allowString' validator would ever work for you.

Not sure how clear this answer was. :)
Back to Top
pierom View Drop Down
Newbie
Newbie


Joined: 19-Sep-2012
Location: Italia
Posts: 9
Post Options Post Options   Quote pierom Quote  Post ReplyReply Direct Link To This Post Posted: 01-Oct-2012 at 10:59pm
The underlying model properties are numbers (int32).
The problem arises when the viewmodel is bind with ko to a view (text editors). The values returned from the bind are strings  so the default validator without allowString=true failed. 
With your last update I substitute the default validator for every involved property. Not a big problem except that I have about 300 int32 properties!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down