Calendar input date checking

Martin Shin asked on March 4, 2015 00:30

Hi! Is it possible to checking input date in calendar? I created DateTime field in table. And I need in checking for input data.

For example, if client set date early then DateTime.Now.AddDays(3) I need set automatically date to DateTime.Now.AddDays(3). Is it possible to do with macros?

Recent Answers


Trevor Fayas answered on March 4, 2015 21:17 (last edited on March 4, 2015 21:18)

Yup, it's called a Form Validation Macro Rule

In Kentico 8+, go to Macro Rules in the sidebar, then Form Validation.

I would clone the "Date From" and adjust the condition from: ToDateTime(Value) >= ToDateTime("{date}")

to: ToDateTime(Value) >= DateTime.Now.AddDays(-3)

Don't forget to remove the "Parameters" from the list as well as you won't need it, unless you want to make the rule more dynamic and remove the "date" parameter and add a "days in the past" int parameters, then adjust the condition to: ToDateTime(Value) >= DateTime.Now.AddDays(-{daysInPast})

Lastly, to apply this to your field, just go to your Page Type -> [the page object] -> Fields -> (Select Field) -> Advanced -> Validation Rules and add yours.

0 votesVote for this answer Mark as a Correct answer

Martin Shin answered on March 5, 2015 09:52 (last edited on March 5, 2015 09:53)

Hi Trevor. I'm sorry, but I don't understood you exactly. Correct me, please. I do next steps

  1. I go to "Macro Rules"
  2. I clone "Date From" rule and name it as "Date limit"
  3. I changed condition to ToDateTime(Value) >= DateTime.Now.AddDays(-3)
  4. I deleted "date" in "parameter" section
  5. I created new field in "Paramenter" section as integer and named it as "Days in the past"
  6. You say about condition... I stopped here. I don't see "condition" field here. But I selected "General condition" from "Validation rule" drop-down list. So I added ToDateTime(Value) >= DateTime.Now.AddDays(-{daysInPast}) to this field. I don't sure is it correct or not. 7.I opened Page Type -> [the page object] -> Fields -> (Select Field) -> Advanced -> Validation Rules and added "Date limit" rule and set 3 in the field.

I checked calendar, but your algorithm doesn't work. I have mistake in my actions I guess.

From other side I have strange calendar behavior (this is doesn't have relation to your method). As I know date format in calendar depends from visitor culture. In test server calndar date format is: mm/dd/yyyy. So, if I apply something like this DateTime.Now.AddDays(3), I see result, that days increased to 3. and this is correct.

But if visitor culture changed and visitor has date format like this dd/mm/yyy function DateTime.Now.AddDays(3) increases month, but not days!

I use K 8.2 with latest update.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 5, 2015 15:33 (last edited on March 5, 2015 15:35)

Very close Martin!

Starting with Step 6. The "Condition" field is the same one you entered the the Step 3.

DateTime.Now.AddDays(-{daysInPast})

INSTEAD of

ToDateTime(Value) >= DateTime.Now.AddDays(-3)

Since now you are defining in the Validation Rule 'settings' what the days in the past are.

As for your culture issue...that is a toughy. You can try adjusting to this?

ToDateTime(Value) >= ToDateTime(DateTime.Now.AddDays(-3).ToString())

or

ToDateTime(Value) >= ToDateTime(DateTime.Now.AddDays(-{daysInPast}).ToString())

Maybe the ToDateTime will convert it properly to the right culture...but even that i'm not 100% and you'll have to do some testing.

0 votesVote for this answer Mark as a Correct answer

Martin Shin answered on March 5, 2015 20:27

Hi Trevor. I playing with your script now. And I it works very strange, I guess. First of all, when I call rule here Page Type -> [the page object] -> Fields -> (Select Field) -> Advanced -> Validation RulesI can't setup integer. I get "invalid input" always. So, I leave this field and save rule.

Now calendar cheques all last days till today day. But Tomorrow not checked already. But I need check all last days and 3 days after today day. For example, today we have March, 5. I need protect input till to 8 of March.

And is it possible contact with you directly by e-mail? I guess, I do something wrong with your instructions. And I will send screen shots to you. Is it possible?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.