Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I validate a DatePicker field? #1432

Open
a95tbirdsc opened this issue Dec 19, 2023 · 1 comment
Open

How do I validate a DatePicker field? #1432

a95tbirdsc opened this issue Dec 19, 2023 · 1 comment

Comments

@a95tbirdsc
Copy link

a95tbirdsc commented Dec 19, 2023

Hello,

I have implemented this code https://github.com/tabalinas/jsgrid/blob/master/demos/custom-grid-field.html into my jsGrid but how do I validate the DatePicker field?

What am I trying to do?

  • Required Field (The Date must not be blank. If blank then apply CSS class "jsgrid-invalid" to the cell. Don't submit the Insert/Edit.)
  • Valid Date (Date must be in the format of MM/DD/YYYY. This should be working with the "toLocaleDateString()" I'm using.)
  • Blank Date (Produces this error: Uncaught TypeError: this._insertPicker.datepicker(...) is null. How do I NOT produce this error?)

`

var MyDateField = function(config) {
jsGrid.Field.call(this, config);
};

  MyDateField.prototype = new jsGrid.Field({
    sorter: function(date1, date2) {
    return new Date(date1) - new Date(date2);
  },

  itemTemplate: function(value) {
    return new Date(value).toLocaleDateString();
  },
  insertTemplate: function(value) {
    return this._insertPicker = $("<input>").datepicker({ defaultDate: new Date().toLocaleDateString() });
  },
  editTemplate: function(value) {
    return this._editPicker = $("<input>").datepicker().datepicker("setDate", new Date(value).toLocaleDateString());
  },
  insertValue: function(value) {
    return this._insertPicker.datepicker("getDate").toLocaleDateString();
  },
  editValue: function() {
    return this._editPicker.datepicker("getDate").toLocaleDateString();
  }
 });

  jsGrid.fields.myDateField = MyDateField;

`

`

{ name: "RETURN_START_DATE", type: "myDateField", title: "Return Start Date", align: "left", editing: true, filtering: true, inserting: true,
                    validate: [
                      { validator: "required",
                          message: function(value, item) {
                                     toastr["warning"]("Return Start Date is required.", "Warning");
                          }
                      }
                    ]
            },
            { name: "RETURN_END_DATE", type: "myDateField", title: "Return End Date", align: "left", editing: true, filtering: true, inserting: true,
                    validate: [
                      { validator: "required",
                          message: function(value, item) {
                                     toastr["warning"]("Return End Date is required.", "Warning");
                          }
                      }
                    ]
            }

`

@BrahamMounir
Copy link

Hi,
you can define your own validator

this should work :

{
   name: "IdShift", validate: myValidatorNotNullOrEmpty(),  type: "select", align: "left", width: "10%", items: ['1', '2'],
},
 function myValidatorNotNullOrEmpty() {
    return {
        validator: function (value) {
            return value !== '' && value !== null && value !== undefined && value !== 0;
        },
        message: function (value, item, param) {
            return "this value must be not null or empty :) ";
        }
    };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants