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

Validation on datepicker in ej.grid different to format required

Hi,

I am trying to apply the date format "dd/MM/yyyy" to a date column in my grid.  The EditType is DatePicker.

When I use the datepicker to select a date it is display the date as required but when I try and save, or move to the next column it is applying the format "MM/dd/yyyy" for validation and display an error, "Please enter a valid date".

I have include my MVC code below...

I have spent hours trying to work this out and am very frustrated.

Please help!
Steve

MVC

@(Html.EJ().Grid<PersonNamesViewModel>("PersonNamesGrid")

.DataManagerID("dmPersonNames")

.Query("ej.Query().addParams('personId'," + @Model.PersonId + ")")

.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().RowPosition(RowPosition.Bottom).ShowDeleteConfirmDialog(); })

.ToolbarSettings(toolbar =>

{

toolbar.ShowToolbar().ToolbarItems(items =>

{

items.AddTool(ToolBarItems.Add);

});

})

.AllowTextWrap()

.TextWrapSettings(wrap => { wrap.WrapMode(WrapMode.Header); })

.Columns(col =>

{

col.Field(p => p.Id).IsPrimaryKey(true).IsIdentity(true).Visible(false).Add();

col.Field(p => p.PersonId).Visible(false).DefaultValue(Model.PersonId).Add();

col.Field(p => p.LastName).HeaderText(@Html.DisplayNameFor(model => model.personNamesViewModel[0].LastName).ToString()).ValidationRules(v => v.AddRule("required", true)).Add();

col.Field(p => p.FirstName).HeaderText(@Html.DisplayNameFor(model => model.personNamesViewModel[0].FirstName).ToString()).ValidationRules(v => v.AddRule("required", true)).Add();

col.Field(p => p.OtherName).HeaderText(@Html.DisplayNameFor(model => model.personNamesViewModel[0].OtherName).ToString()).Add();

col.Field(p => p.IsPreferred).HeaderText(@Html.DisplayNameFor(model => model.personNamesViewModel[0].IsPreferred).ToString()).DisplayAsCheckbox().EditType(EditingType.Boolean).Width(80).Add();

col.Field(p => p.NameCodeId)

.HeaderText(@Html.DisplayNameFor(model => model.personNamesViewModel[0].NameCodeId).ToString())

.ForeignKeyField("Value")

.ForeignKeyValue("Text")

.DataSource(Model.NameCodes.ToList())

.Width(100).Add();

col.Field(p => p.BirthDate).HeaderText(@Html.DisplayNameFor(model => model.personNamesViewModel[0].BirthDate).ToString()).EditType(EditingType.Datepicker).Format("{0:dd/MM/yyyy}").Width(100).Add();

col.Field(p => p.Age).HeaderText(@Html.DisplayNameFor(model => model.personNamesViewModel[0].Age).ToString()).Width(50).Add();

col.Field(p => p.GenderCodeId)

.HeaderText(@Html.DisplayNameFor(model => model.personNamesViewModel[0].GenderCodeDescription).ToString())

.ForeignKeyField("Value")

.ForeignKeyValue("Text")

.DataSource(Model.GenderCodes.ToList())

.Width(75).Add();

col.HeaderText("Actions").Commands(commands =>

{

commands.Type(UnboundType.Edit).ButtonOptions(new ButtonProperties()

{

ContentType = ContentType.ImageOnly,

PrefixIcon = "e-icon e-edit"

}).Add();

commands.Type(UnboundType.Delete).ButtonOptions(new ButtonProperties()

{

ContentType = ContentType.ImageOnly,

PrefixIcon = "e-icon e-delete"

}).Add();

commands.Type(UnboundType.Save).ButtonOptions(new ButtonProperties()

{

ContentType = ContentType.ImageOnly,

PrefixIcon = "e-icon e-save"

}).Add();

commands.Type(UnboundType.Cancel).ButtonOptions(new ButtonProperties()

{

ContentType = ContentType.ImageOnly,

PrefixIcon = "e-icon e-cancel"

}).Add();

}).Width(150).Add();

})

.ClientSideEvents(e => { e.Create("onCreate").ActionBegin("onBegin").BeginEdit("onBeginEdit").ActionComplete("onComplete"); })

)


6 Replies

FS Farveen Sulthana Thameeztheen Basha Syncfusion Team May 12, 2017 12:38 PM UTC

Hi Stephan, 

We have checked your code example but we are unable to reproduce the reported problem at our end. We have created sample as per your requirement in which we have applied editType as DatePicker and format to the OrderDate column. Also we have gave validation to that column. Please refer to the attached sample which can be downloaded from the below location. 


Please refer to the code example:- 

@(Html.EJ().Grid<object>("Grid") 
           .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.dataSource).UpdateURL("CellEditUpdate").InsertURL("CellEditInsert").RemoveURL("CellEditDelete").Adaptor(AdaptorType.RemoteSaveAdaptor)) 
          .AllowPaging() 
           .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); }) 
           .ToolbarSettings(toolbar => 
                 { 
                      toolbar.ShowToolbar().ToolbarItems(items => 
                         { 
                               items.AddTool(ToolBarItems.Add); 
                               items.AddTool(ToolBarItems.Edit); 
                               items.AddTool(ToolBarItems.Delete); 
                               items.AddTool(ToolBarItems.Update); 
                               items.AddTool(ToolBarItems.Cancel); 
                           }); 
               }) 
          
          .Columns(col => 
              { 
                  col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Add(); 
                  col.Field("CustomerID").HeaderText("Customer ID").Add(); 
                  col.Field("OrderDate").HeaderText("Order Date").EditType(EditingType.Datepicker).ValidationRules(v => v.AddRule("required",true)).Format("{0:dd/MM/yyyy}").Add();   
                  col.Field("ShipCity").HeaderText("Ship City").Add(); 
                  col.Field("ShipCountry").HeaderText("Ship Country").Add(); 
              })) 
     
 

We need more information to find the cause of the issue. Could you please share us the following details with us. 

1. In your code example, you doesn’t mentioned validation for date type column. Are you expecting validation for date column. 

2.Ellaborate your requirement/ issue. 

3. Screenshot/Video regarding the issue. 

4. Replication procedure and replicate the issue in the above provided sample and revert us back. 

5. Stacktrace of the issue if any script error occurs. 

The provided details will helps us to analyze and provide you solution as early as possible.  

Regards, 

Farveen sulthana T 



SA Stephen Armstrong May 25, 2017 12:13 AM UTC

Hi,

Sorry for not replying sooner.

I was able to apply a work-around to solve the issue in Chrome by applying the format "dd-MMM-yyyy".  However, I still have the problem in IE 11.

To answer your questions:
  1. The only validation we require is, if a date is entered, it is valid.  Currently entering a date using the date picker in the grid or typing it in throw the validation error.  It is not a required field.
  2. The issue is we would want our users to be able to enter a date using dd-MMM-yyyy.  They can do this (for example 15-May-2016), but when they try and save a row in the grid, the validation error appears and they cannot save it.  Even if we use the datepicker, it fails.
  3. I have added a attachment to show the issue
  4. No procedure...just trying to click the save button on the grid row
  5. No script error occurs
Just a thought , but is it possible that jQuery validation is interfering?

This is pottentially a show stopper for us, so I need help!

Thanks,
Steve

Attachment: Date_validation_problem_2f926558.zip


SA Shameer Ali Baig Sulaiman Ali Baig Syncfusion Team May 29, 2017 12:39 PM UTC

Hi Stephan, 
Thanks for the patience. 
                        We have checked with your query. We could reproduce the reported issue. In jQuery validation, date method can be used to validate the date format of any input date. But, this method is not recommended and it should not be used, since it relies on the new Date constructor, which behaves very differently across browsers and locales. We have prepared a work around sample to meet your requirement in DatePicker validation by using add method of jQuery validation. 
Open the following link in different browsers to find the issue in date method and to know more about it. 
Please, find the attached sample below, 
Use, the below links to know more about add method of jQuery validation and attached sample, 
Please let us know if you need any further assistance, 
Regards, 
Shameer Ali Baig S. 



SA Stephen Armstrong June 1, 2017 05:46 AM UTC

Hi Shameer,

Thanks for your reply.  I seem to be a lot closer, I am not getting a validation error now.

That being said, when I enter an invalid date, for example 32-32-00, I get no error, it just reverts back to the original date with no message

I am not sure how to add the validation message and make it show on the datepicker on a ej.Grid.

Cheers,
Steve


SA Stephen Armstrong June 2, 2017 12:53 AM UTC

Hi,

I figured it out.  I was loading JQuery after my script, so it was not finding the methods.

I also added a custom message for the new validator method:

$.validator.setDefaults({
    ignore: [],
    errorClass: 'e-validation-error',
    errorPlacement: function (error, element) {
        $(error).insertAfter(element.closest(".e-widget"));
    }
});

$.extend($.validator.messages, {
    dateFormat: 'Enter date using format dd-MMM-yyyy'
})

$.validator.methods.dateFormat = function (value, element) {
    return value != "" && /^\d\d?-\w\w\w-\d\d\d\d/.test(value);
}

Thanks for your help.

Steve


SA Shameer Ali Baig Sulaiman Ali Baig Syncfusion Team June 2, 2017 09:54 AM UTC

Hi Steve, 
We are glad to hear that the problem is resolved. Please let us know if you need any further assistance.   
We’ll be happy to help you.  
Regards, 
Shameer Ali Baig S.  


Loader.
Up arrow icon