How to validate date fields, help me out.

Certified Associate Developer

Hi Community Team, 

I need to set validations for 3 date field components. The following use cases are as follows: Commonly No filed is null or empty.

1. Field Name: Joining date validations are : I must be able to select the date which is over past 100 years and also able to select the future date which is One year from today.

2. Field Name: Access Start date validations are : I must be able to select the date which is from join date or I must be able to select the date which is one month from today.

3. Field Name: Access End date validations are : I must be able to Validate the date which is from one year  from Access Start date.

For each filed, If the validations were violated, it returns error message.

Please help me out to solve this use case. open for suggestions.

  Discussion posts and replies are publicly visible

Parents
  • Here you go...

    a!localVariables(
      local!joiningDate,
      local!accessStartDate,
      local!accessEndDate,
      {
        a!dateField(
          label: "Joining Date",
          labelPosition: "ABOVE",
          value: local!joiningDate,
          saveInto: local!joiningDate,
          validations: a!match(
            value: local!joiningDate,
            whenTrue: todate(fv!value) < todate(subtractyears(todatetime(today()), 100)),
            then: "Selected date can't be before 100 years from now",
            whenTrue: todate(fv!value) > todate(addyears(todatetime(today()), 1)),
            then: "Date cant be in more than 1 year in future",
            default: ""
          )
        ),
        a!dateField(
          label: "Access Start Date",
          labelPosition: "ABOVE",
          value: local!accessStartDate,
          saveInto: local!accessStartDate,
          disabled: isnull(local!joiningDate),
          validations: a!match(
            value: local!accessStartDate,
            whenTrue: todate(fv!value) < todate(local!joiningDate),
            then: "Selected date can't be before joining date",
            whenTrue: todate(fv!value) > todate(addmonths(todatetime(today()), 1)),
            then: "Date cant be in more than 1 month in future",
            default: ""
          )
        ),
        a!dateField(
          label: "Access End Date",
          labelPosition: "ABOVE",
          value: local!accessEndDate,
          saveInto: local!accessEndDate,
          disabled: isnull(local!accessStartDate),
          validations: a!match(
            value: local!accessEndDate,
            whenTrue: todate(fv!value) < todate(local!accessStartDate),
            then: "Access end date can't be less than start date",
            whenTrue: todate(fv!value) > todate(
              addyears(todatetime(local!accessStartDate), 1)
            ),
            then: "Access end date can't be more than 1 year from access start date",
            default: ""
          )
        )
      }
    )

  • 0
    Certified Associate Developer
    in reply to Sanchit Gupta (Xebia)

    Hi Sanchit, I would like to update this date filed components..

    I'm getting glitch when I selected the date of Joining.. access start date is enabled.. I selected the access start date.. and now I click on clear the date of joining  filed. Access start date is disabled but showing the value in it.. I want it to be empty.. if it is disabled.

  • +1
    Certified Lead Developer
    in reply to Chandrasekhar Reddy

    Hi, You can just simply save null in Access start and Access end date when selecting date for joining date.

    Here is a small change in the above code

    a!localVariables(
      local!joiningDate,
      local!accessStartDate,
      local!accessEndDate,
      {
        a!dateField(
          label: "Joining Date",
          labelPosition: "ABOVE",
          value: local!joiningDate,
          saveInto: {
            local!joiningDate,
            /*saving null values*/
            a!save(
              local!accessStartDate,
              null
            ),
            a!save(
              local!accessEndDate,
              null
            )
          },
          validations: a!match(
            value: local!joiningDate,
            whenTrue: todate(fv!value) < todate(subtractyears(todatetime(today()), 100)),
            then: "Selected date can't be before 100 years from now",
            whenTrue: todate(fv!value) > todate(addyears(todatetime(today()), 1)),
            then: "Date cant be in more than 1 year in future",
            default: ""
          )
        ),
        a!dateField(
          label: "Access Start Date",
          labelPosition: "ABOVE",
          value: local!accessStartDate,
          saveInto: {
            local!accessStartDate,
            /*saving null values*/
            a!save(
              local!accessEndDate,
              null
            )
          },
          disabled: isnull(local!joiningDate),
          validations: a!match(
            value: local!accessStartDate,
            whenTrue: todate(fv!value) < todate(local!joiningDate),
            then: "Selected date can't be before joining date",
            whenTrue: todate(fv!value) > todate(addmonths(todatetime(today()), 1)),
            then: "Date cant be in more than 1 month in future",
            default: ""
          )
        ),
        a!dateField(
          label: "Access End Date",
          labelPosition: "ABOVE",
          value: local!accessEndDate,
          saveInto: local!accessEndDate,
          disabled: isnull(local!accessStartDate),
          validations: a!match(
            value: local!accessEndDate,
            whenTrue: todate(fv!value) < todate(local!accessStartDate),
            then: "Access end date can't be less than start date",
            whenTrue: todate(fv!value) > todate(
              addyears(todatetime(local!accessStartDate), 1)
            ),
            then: "Access end date can't be more than 1 year from access start date",
            default: ""
          )
        )
      }
    )

  • 0
    Certified Associate Developer
    in reply to ujjwalrathore

    Thank you very much for the quick response   the code is working fine.. as I expected 

Reply Children
No Data