We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Using Required Annotation in the create/update button of a grid

Hi,

I have a grid in javacsript, with editSettings editMode : "dialogtemplate" and a dialogEditorTemplateID.
I use create and  udpate buttons of the grid to display a Dialog for inserting or updating datas.
I  use a Required dataAnnotation with an error message in my model to handle the obligation of setting a value in some fields but
 it didn't work so those fields aren't require and i don't know how display the error message from my annotation.

I put the Required annotation like this :

[Required(ErrorMessage = "My error")]
public String name {get;set;}

In my view i imported the following script :
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

Thanks for the help,     

T. Cholet

3 Replies

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team March 14, 2016 09:41 AM UTC

Hi Cholet,

We understood that you would like to perform Validation using Data Annotation Validators.

Supplier class which holds the data annotation properties of the required columns of Grid and its class name can be given as type of object for the Grid as shown below. This will validate after submitting the form based on the data annotation properties. Refer to the following code example

@(Html.EJ().Grid<Supplier>("FlatGrid")

      .Datasource(ds=>ds.URL("/Home/DataSource")

              .InsertURL("/Home/Insert")

              .UpdateURL("/Home/Update")

              .RemoveURL("/Home/Delete")

          .Adaptor(AdaptorType.UrlAdaptor))

         . . . ..

      .Columns(col =>

      {

          col.Field("Id").HeaderText("Unique ID").IsPrimaryKey(true).Width(75).Add();

            . . .

      })

      )


namespace CRUDandValidation.Models

{

    using System.ComponentModel.DataAnnotations;


    public class Supplier

    {

        [Required(ErrorMessage = "ID is must")]

        public int Id { get; set; }

         [RegularExpression(@"^[a-zA-Z0-9 ]{1,10}$")]

        public string PostalCode { get; set; }

         [EmailAddress(ErrorMessage = "Invalid email address")]

        public string EmailAddress { get; set; }

        [Url]

        public string WebAddress { get; set; }

}


We have prepared a sample that can be downloaded from the following location.

Sample: http://www.syncfusion.com/downloads/support/forum/123372/ze/DataAnnotationValidation-1122801197

Regards,
Seeni Sakthi Kumar S.


CH Cholet March 16, 2016 10:40 AM UTC

It works thank you !
Is it possible to do it with javascript ?


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team March 17, 2016 03:10 PM UTC

Hi Cholet

JavaScript Grid does not support data annotations. But you can enable validations using ValidationRules property of Grid Colums. Refer to the following code example.

<div id="Grid"></div>

<script type="text/javascript">

    $(function () {

        $("#Grid").ejGrid({

            dataSource: ej.DataManager({

                url: "/Home/DataSource",

                insertURL: "/Home/Insert",

                updateURL: "/Home/Update",

                removeURL: "/Home/Delete"

            }),

            allowPaging: true,

             . ..  . . .

            columns: [

                    { field: "Id", isPrimaryKey: true, validationRules: { required: true, messages: { required: "ID is must" }} },

                    { field: "CompanyName", validationRules: { required: true, minlength: 3 } },

                    { field: "PostalCode", validationRules: { range: [0, 1000] } }

                     . . . . .


            ]

        });

    })
</script>


Refer to the following Help document.

http://help.syncfusion.com/js/api/ejgrid#members:columns-validationrules

You could also customize the validation rules and frame your own rules. Refer to the following online demo.

http://js.syncfusion.com/demos/web/#!/azure/grid/editing/customvalidation

We have prepared a sample that can be downloaded from the following location.

Sample: http://www.syncfusion.com/downloads/support/forum/123372/ze/CRUDandValidation373767504

Regards,
Seeni Sakthi Kumar S.

Loader.
Up arrow icon