This is a migrated thread and some comments may be shown as answers.

Using Model validation for View Model elements

4 Answers 402 Views
Validation
This is a migrated thread and some comments may be shown as answers.
Mahesh Kumar
Top achievements
Rank 1
Mahesh Kumar asked on 27 Aug 2013, 07:52 AM
Hi,

Could you please anyone advise me how can i bind model class data annotation attributes using Kendo MVVM?

I have defined the Employee model class as below,

  public class EmployeeModel
    {
        public int EmployeeId { get; set; }

        [Display(Name = "Employee Name")]
        [DataType(DataType.Text)]
        [Required(ErrorMessage = "The employee name should not be empty")]
        [StringLength(50, ErrorMessage = "The employee name should be less than 50 characters length")]
        public string Name { get; set; }

        public bool IsActive { get; set; }
    }

and i have bound this model class into view page using observable object binding. I can use HTML5 attributes for validation and validate using the below Kendo validator code, EmployeeDetails - observable object.

                               <input type="hidden" id="employeeId" data-bind="value : EmployeeDetails.EmployeeId"/>
                                Name<br />
                                <input data-bind="value: EmployeeDetails.Name" type="text" id="Name" name="Name" class="k-textbox" 
                                    placeholder="Employee name" required validationMessage="Please enter employee {0}" />
                                <div></div>
                                <span class="k-invalid-msg" data-for="Name"></span>
                             
                                <input type="checkbox" data-bind="checked: EmployeeDetails.IsActive" id="Status" />


           var validator = $("#EmployeeDetailsView").kendoValidator().data("kendoValidator");

                if (!validator.validate()) {
                    $("#Name").focus();
                    return false;
                }

Now, how can i achieve the same validation by binding the  model attributes directly to input fields with HTML5 validation attributes mentioned as above?
At the same time i have to use observable object binding for elements.

Cheers,
Mahesh

4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 29 Aug 2013, 05:47 AM
Hello Mahesh,

You should use a HTML helper in order for the unbotrusive validation attributes to be rendered based on the DataAnnotation attributes. In order for the Validator to work for the unobtrusive attributes you should include the kendo.aspnetmvc JavaScript file or add custom rules to the validator that validate the input based on the attributes.

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mahesh Kumar
Top achievements
Rank 1
answered on 29 Aug 2013, 10:32 AM
Hi Daniel,

Could you please advise how can i implement unobtrusive attributes validation using kendo.aspnetmvc JavaScript file?
Please suggest with simple example?

Cheers,
Mahesh
0
Daniel
Telerik team
answered on 30 Aug 2013, 01:15 PM
Hello Mahesh,

I attached a sample project that demonstrates using the Kendo Validator for the rules defined with the DataAnnotation attributes. 

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Mahesh Kumar
Top achievements
Rank 1
answered on 10 Sep 2013, 05:22 AM
Thanks Daniel.

Cheers,
Mahesh
Tags
Validation
Asked by
Mahesh Kumar
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Mahesh Kumar
Top achievements
Rank 1
Share this question
or