Validate dates and times

Hello! I need help :)

I have a doubt that I do not know how to do it.

I want to validate the hours field and the minutes field in this way:

• From April 1 to September 30, effective hours will be 7.

• From October 1 to March 31, the effective hours will be 7 and the minutes will be 33.

in my code:

a!sideBySideItem(
item:

a!integerField(
labelPosition: "ABOVE",
value: ri!Regis.hours,
saveInto: {ri!Regis.hours},
align: "LEFT"
),
width: "MINIMIZE"
),
a!sideBySideItem(
item:
a!textField(
labelPosition: "ABOVE",
value: " Hours ",
readOnly: true
),
width: "MINIMIZE"
),
a!sideBySideItem(
item:

a!integerField(
labelPosition: "ABOVE",
value: ri!Regis.minute,
saveInto: {ri!Regis.minute}
),
width: "MINIMIZE"
),
a!sideBySideItem(
item:
a!textField(
labelPosition: "ABOVE",
value: " Minute",
readOnly: true
)
)

thanks so much!!

  Discussion posts and replies are publicly visible

  • Hi Alastr, as this is not clear from the query whether you want the validation at the text field level (i.e. when the value gets populated) OR while submitting the form, you can validate this in different ways. A simple way is as follows (just for reference):

    Use the validations option available for the text field component and use if conditions to validate the hours and minutes within the range. You can have the date range in a constant OR local variables. For example - validations: if(and(day(now())>=1,month(now())>=4,ri!Regis.hours>7), "Can't be more than 7", {})

  • The effective hours and minutes will be an automatically modified and initialized field with values 
    ​​depending on the creation date of the input task:
    • From April 1 to September 30, effective hours will be 7.
    • From October 1 to March 31, the effective hours will be 7 and the minutes will be 33.

    thanks

  • load(
    local!date,
    local!hours,
    local!minute,
    local!validationData,
    {
    a!dateField(
    label: "Date",
    value: local!date,
    saveInto: {
    local!date,
    a!save(
    local!validationData,
    if(
    isnull(
    local!date
    ),
    {},
    if(
    and(
    local!date >= todate(
    concat(
    "04/01/",
    year(
    local!date
    )
    )
    ),
    local!date <= todate(
    concat(
    "09/30/",
    year(
    local!date
    )
    )
    )
    ),
    {
    hours: 7,
    minutes: 0
    },
    {
    hours: 7,
    minutes: 30
    }
    )
    )
    ),
    a!save(
    local!hours,
    null
    ),
    a!save(
    local!minute,
    null
    )
    }
    ),
    a!sideBySideLayout(
    items: {
    a!sideBySideItem(
    item: a!integerField(
    labelPosition: "ABOVE",
    value: local!hours,
    saveInto: {
    local!hours
    },
    align: "LEFT",
    readOnly: if(
    isnull(
    local!date
    ),
    true(),
    false()
    ),
    validations: if(
    isnull(
    local!hours
    ),
    {},
    if(
    local!hours > tointeger(
    local!validationData.hours
    ),
    "Not Valid",
    {}
    )
    )
    ),
    width: "MINIMIZE"
    ),
    a!sideBySideItem(
    item: a!textField(
    labelPosition: "ABOVE",
    value: " Hours ",
    readOnly: true
    ),
    width: "MINIMIZE"
    ),
    a!sideBySideItem(
    item: a!integerField(
    labelPosition: "ABOVE",
    value: local!minute,
    saveInto: {
    local!minute
    },
    readOnly: if(
    isnull(
    local!date
    ),
    true(),
    false()
    ),
    validations: if(
    isnull(
    local!minute
    ),
    {},
    if(
    local!minute > tointeger(
    local!validationData.minutes
    ),
    "Not Valid",
    {}
    )
    )
    ),
    width: "MINIMIZE"
    ),
    a!sideBySideItem(
    item: a!textField(
    labelPosition: "ABOVE",
    value: " Minute",
    readOnly: true
    )
    )
    }
    )
    }
    )

  • hours and minutes are auto populated or user will enter it?

  • the hours and minutes fields must be initialized when loading the page with 7 hours From April 1 to September 30 7 hours and 33 minutes From October 1 to March 31

  • from where you will get the date ,from start time of task ?

  • In that case (where you want the fields to be auto populated), you can do that using if condition for your minute field as your hour filed is always going to be 7 hours. Use the following in display value for the Integer Field and should be fine. You can refine the following to make it more compact by having the value to check against in some local variables or constants to use in IF condition:

    For example, in display value field for your minutes field you can use:

    if(
    OR(
    todate(now()) >= todate("4/1/",year(now())),
    todate(now()) <= todate("9/30/",year(now()))
    ),
    0,
    30
    ),

  • depending on the current date of when the page will be,  I just have to enter each field  7 hours From April 1 to September 30 7 hours and 33 minutes From October 1 to March 31

  • load(
      local!date: today(),
      local!hours,
      local!minute,
      local!validationData: if(
        and(
          local!date >= todate(
            concat(
              "04/01/",
              year(
                local!date
              )
            )
          ),
          local!date <= todate(
            concat(
              "09/30/",
              year(
                local!date
              )
            )
          )
        ),
        {
          hours: 7,
          minutes: 0
        },
        {
          hours: 7,
          minutes: 33
        }
      ),
      {
        a!sideBySideLayout(
          items: {
            a!sideBySideItem(
              item: a!integerField(
                labelPosition: "ABOVE",
                value: local!hours,
                saveInto: {
                  local!hours
                },
                align: "LEFT",
                validations: if(
                  isnull(
                    local!hours
                  ),
                  {},
                  if(
                    local!hours > tointeger(
                      local!validationData.hours
                    ),
                    "Not Valid",
                    {}
                  )
                )
              ),
              width: "MINIMIZE"
            ),
            a!sideBySideItem(
              item: a!textField(
                labelPosition: "ABOVE",
                value: " Hours ",
                readOnly: true
              ),
              width: "MINIMIZE"
            ),
            a!sideBySideItem(
              item: a!integerField(
                labelPosition: "ABOVE",
                value: local!minute,
                saveInto: {
                  local!minute
                },
                validations: if(
                  isnull(
                    local!minute
                  ),
                  {},
                  if(
                    local!minute > tointeger(
                      local!validationData.minutes
                    ),
                    "Not Valid",
                    {}
                  )
                )
              ),
              width: "MINIMIZE"
            ),
            a!sideBySideItem(
              item: a!textField(
                labelPosition: "ABOVE",
                value: " Minute",
                readOnly: true
              )
            )
          }
        )
      }
    )

    so when your form is loading ,we are saving date in a local variable based on local date local variable ,we are getting validation data,

    like if date is in between 04/01/year and 09/30/year the the   validation data will be 

    {
    hours: 7,
    minutes: 0
    }

    other than that the validation data will be

    {
    hours: 7,
    minutes: 33
    }

    so if user enter more values than that then showing a validation message  like "Not Valid "

     

  • As this example that has happened to me, I should be alone. It gives me back 7 what I write. Now I should measure up to the date of October 1 to March 31 and keep me 7 and 33

    if (
    or (
    todate (now ())> = todate ("4/1 /", year (now ())),
    todate (now ()) <= todate ("9/30 /", year (now ()))
    ),
    7,
    30
    )