41
Views
3
Comments
Solved
How to use "NullDate()" in a validation?
Question

I'm following the guided path on "Becoming a Traditional Web Developer" and I'm working on 7.5, i.e. I'm practicing adding validations to forms and such. The exercise wants me to add a validation that verifies that a person's date of death, if specified, does not precede the date of birth:



I've tried using NullDate() for this as can be seen below, but it does not appear to be working out well.



So I'm using:

PersonForm.Record.Person.DateOfDeath = NullDate() or
PersonForm.Record.Person.DateOfBirth > PersonForm.Record.Person.DateOfDeath

What am I not doing right?

Rank: #139
Solution

Hi Iwvk1994,


the true branch of your if node is probably going to an assign that sets the input.valid to false, right ?

So the condition of the if should be true if a wrong combination of dates is given.

As you wrote the condition now, it's true when DateOfDeath is empty, and so the validation will fail, when in fact you want the validation to pass if no DateOfDeath is given.  You only want it to fail if a DateOfDeath is given and it comes before DateOfBirth.

So that translates into 


not(PersonForm.Record.Person.DateOfDeath = NullDate()) and
PersonForm.Record.Person.DateOfBirth > PersonForm.Record.Person.DateOfDeath

Hope this helps,

Dorine

a tip : when you get more complex conditions, try to first just say in plain English what it is you want to happen.


mvp_badge
MVP
Rank: #76

Hi,

In order to validate the Date of Death, if defined, is later than the Date of Birth, your validation should be:

PersonForm.Record.Person.DateOfDeath <> NullDate() AND
PersonForm.Record.Person.DateOfDeath < PersonForm.Record.Person.DateOfBirth

This is because when the DateOfDeath is not set by the user, the bound variable will assume the Null Date value, that is 1900-01-01. So now your validation is making sure if the Date of Death is filled in by the user, it needs to be later than the Date of Birth.

Hope this helps!

Regards,

Nordin

Rank: #139
Solution

Hi Iwvk1994,


the true branch of your if node is probably going to an assign that sets the input.valid to false, right ?

So the condition of the if should be true if a wrong combination of dates is given.

As you wrote the condition now, it's true when DateOfDeath is empty, and so the validation will fail, when in fact you want the validation to pass if no DateOfDeath is given.  You only want it to fail if a DateOfDeath is given and it comes before DateOfBirth.

So that translates into 


not(PersonForm.Record.Person.DateOfDeath = NullDate()) and
PersonForm.Record.Person.DateOfBirth > PersonForm.Record.Person.DateOfDeath

Hope this helps,

Dorine

a tip : when you get more complex conditions, try to first just say in plain English what it is you want to happen.


Rank: #157

Hello,

I think that your validations are not entirely correct :)

First, the statement says that you want to verify that the DateOfDeath variable is specified. So, you want to validate that the value is different of null date and not equal.

Then, the statement says that you want to verify that the DateOfDeath variable (if specified) is later than the DateOfBirth variable. So you need to execute an and condition instead of an or.


Try to use something like this:

PersonForm.Record.Person.DateOfDeath <> NullDate() and
PersonForm.Record.Person.DateOfBirth > PersonForm.Record.Person.DateOfDeath


Let me know if you have any further questions.


Kind regards,

Rui Barradas