Placing OR condition

Hi,

How can I use an OR condition inside the validation.

Basically, I would like to post a message if the age is less than 1 or greater than 100

a!integerField(
label: "Age",
labelPosition: "ABOVE",
value: ri!employee2.age,
saveInto: ri!employee2.age,
refreshAfter: "UNFOCUS",
validations: if(
ri!employee2.age < 1 |or ,
"You must enter proper age",
""
)

  Discussion posts and replies are publicly visible

  • Hi there,

    Is this what you are looking for?

    a!integerField(
      label: "Age",
      labelPosition: "ABOVE",
      value: ri!employee2.age,
      saveInto: ri!employee2.age,
      refreshAfter: "UNFOCUS",
      validations: if(
        or(
          tointeger(index(ri!employee2, "age", null)) < 1,
          tointeger(index(ri!employee2, "age", null)) > 100
        ),
        "You must enter proper age",
        ""
      )
    )

    Acacio B.

  • Thank you. This works for me.

    I would like to know why you have used "index". Can't we just use like below.

    Any comments from your end will be appreciated. Thanks in advance.

    validations: if(
    or(
    tointeger(ri!employee2.age) < 1,
    tointeger(ri!employee2.age)  > 100
    ),
    "You must enter proper age",
    ""
    )

  • Great that works.

    Using index() you can handler the nulls and avoid some errors, I normally use but you can check if it is necessary in your case.

    Regards,

    Acacio B

  • 0
    Certified Senior Developer
    in reply to Hari Kishore Reddy

    As Acacio Barrado already said:
    Index() is more NULL stable than "." notation. 

    Insert just "null" into your rule input: ri!employee2 and check what happens ;) then try it with the index() solution.

    on top, you dont need a tointeger function for ".age". the age parameter has a data type probably of type integer.

  • HI!

    Usage of index function really depends if your rule input is a dictionary or a CDT.

    If it is dictionary then you don't need to use it. ri!employee2.age will return null regardles if there is an attribute age in the dictionary or not. Using index(ri!employee2, "age", "no age") will return null if there is no age attribute in the dictionary, "" if the value of the age in the dictionary is null or the value of age if there is one and it is different than null.

    If the rule input is a CDT then things are a bit different. If there is an attribute named "age" in the CDT the functon will return its value whether it is null or different than null. If there is no attribute named "age" in the CDT then it will return the "default" value  specified in the index function. Also referencing  the age attribute (ri!employee2.age) on a CDT that does not contain an attribute of that name will throw an expression evaluation error.

    So using index has meaning if your rule input is defined as CDT.

    Important question is knowing if your age attribute will always have value or will it be null.

    In most cases you will need to use tointeger if your attribute will return a null value.

    Comparison of null value with an integer constant will end up in expression evaluation error if the null is not of Number type. And null will be of Number type only if it comes from a CDT that has the named attribute of type Number (Integer) and its value is null.

    In all other cases null will return an expression evaluation error during comparison with an integer.

    Using tointeger(null) will convert the null value to Integer Null so it can be compared without an error.

    tointeger(null) < 1 = true

    If in doubt, use tointeger() and index(). However if there is no doubt code will be simpler and more clear without reduntant function calls.