MVVM model validation

5 posts, 0 answers
  1. Marcel
    Marcel avatar
    36 posts
    Member since:
    Apr 2011

    Posted 06 Jun 2013 Link to this post

    Hello,

    I created a small SPA using Kendo web and the MVVM pattern, but i'm having problems getting validation to work.
    So i have a view which binds to a viewmodel using kendo.bind(view, viewmodel).
    and example of a domain object (client side) would be like this:
    var User = kendo.data.Model.define({
        id: "id",
        fields: {       
            id: {
                editable: false,
                nullable: true
            },
            username: {
                type: "string",
                validation: {
                    required: true
                },
                defaultValue: ""
            },
            password: {
                type: "string",
                validation: {
                    required: true
                },
                defaultValue: ""
            }
        }
    });

    and my viewmodel:
    var vmLogin = function () {
        var _userName = "";
        var _password = "";
     
        var _user = new User();
        
        var _submitLogin = function () {
        }
     
        var _init = function () {
        }
     
        return {
            Init: _init,
            User: _user,
            Submit: _submitLogin
        }
    }

    I'm using data-bind in my view to bind to my viewmodel.
    Is it possible to apply a Kendo validator to my view using the rules from my Model ?

    Thanks in advance.
  2. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 07 Jun 2013 Link to this post

    Hello Marcel,

    Kendo Validator works on the basis of validation attributes. In case you use widgets such as editable Grid, the framework automatically attaches the attributes to the corresponding input elements.

    When working with standard HTML elements and MVVM, it is responsibility of the developer to put the attributes and initialize the validator.

    Regards,
    Alexander Valchev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. sai
    sai avatar
    11 posts
    Member since:
    Jul 2013

    Posted 12 Aug 2013 Link to this post

    thanks Alexander. It worked!
  5. Tharsis-it
    Tharsis-it avatar
    1 posts
    Member since:
    Mar 2013

    Posted 23 Apr 2014 in reply to Alexander Valchev Link to this post

    Hi Alexander, 

    When you say it is responsibility of the developer to put the attributes and initialize the validator, you mean that it should be done directly in the HTML/Template or is there some way to make it dynamic and get the values ​​specified in the model? 

    Please could you give me an example? 

    Thank you very much. 

    Alejandro -.
  6. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 25 Apr 2014 Link to this post

    Hi Alejandro,

    The validation rules defined in the model will be taken into account only if you use a widget with build-in editing such as Kendo Grid. (example)
    In case you plant to use template/html/MVVM it is responsibility of the developer to put the corresponding HTML5 validation attributes to the input elements so the validator can validate them. For more information and code samples which demonstrates how to configure and use Kendo Validator please check this help topic.

    Regards,
    Alexander Valchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top
Kendo UI is VS 2017 Ready