Date validation or NULL

Date validation or NULL

lm0@logic1.com.aulm0@logic1.com.au Posts: 11Questions: 3Answers: 0

I need validate the date OR accept a null, currently I have :

            .Field(new Field("DeliveryDate").Validator(Validation.DateFormat(
                    Format.DATE_ISO_8601,
                    new ValidationOpts { Message = "Please enter a date in the format yyyy-mm-dd" }
                ))
                .GetFormatter(Format.DateSqlToFormat(Format.DATE_ISO_8601))
                .SetFormatter(Format.DateFormatToSql(Format.DATE_ISO_8601))
                )

I have added Format.NullEmpty() but cant get this to work,
Any recommendations please?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    Currently what you need to do is provide a custom method to perform this action. For example:

                            .SetFormatter((val, data) => (string)val == "" ?
                                null :
                                DateTime.ParseExact((string)val, "ddd, d MMM yy", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd"))
    

    Unfortunately I think there is a bug in the current release in this area. Editor 1.4.1 will be released tomorrow with the fix, or I can send you the updated dll if you need it sooner.

    What I think I'll do is add the ability to provide multiple formatters so you would be able to do:

    .SetFormatter( Format.NullEmpty() )
    .SetFormatter( Format.DateFormatToSql(Format.DATE_ISO_8601) )
    

    It is very tempting to put that into 1.4.1, but I should wrap everything else for the release up first before adding new things! It might be in the following release...

    Allan

  • lm0@logic1.com.aulm0@logic1.com.au Posts: 11Questions: 3Answers: 0

    Thank you for fast response,
    I have tried the custom method and get ->
    "The parameterized query '(@DeliveryDate nvarchar(4000),@Rego nvarchar(6),@ClaimNumber nva' expects the parameter '@DeliveryDate', which was not supplied."

    I presume this is the bug that will be fixed in 1.41 this week.

    I get the same error using Format.NullEmpty() on decimal fields.
    I will retry in 1.41

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Yup - that's the one! Sorry about the bug - 1.4.1 will fix it!

    Allan

This discussion has been closed.