[SOLVED] Code field validation

3 years 6 months ago - 3 years 4 months ago #320675 by ericmuc
[SOLVED] Code field validation was created by ericmuc
Hi,
I have two date fields and the date2 may not be < than date1 during entering.

So I tried to add php code in the date2 field and switched on the ajax.

I am searching for an example code for this validation, can you send me a link or info which code structure I have to add here? I did'nt found something here.

Thanks a lot, best regards
Eric

Please Log in to join the conversation.

3 years 6 months ago #320678 by krileon
Replied by krileon on topic Code field validation
The initial setup should be as follows to be able to access both fields during your code validation.

Code Validation: Enable
Ajax Validation: Enable
Additional Fields: DATE_FIELD_1

The above would be configured on DATE_FIELD_2. The additional fields is crucial as without that you won't get the current value of DATE_FIELD_1 and will only get the already stored value. Now your code can simply substitute the values of both fields using [DATE_FIELD_1] and [DATE_FIELD_2]. Please be sure to always treat substitutions as strings so surround them in quotes as needed.

Since this is a simple is X greater than or equal to Y check you can just use a strtotime to check this. Example as follows.

return strtotime( '[DATE_FIELD_2]' ) >= strtotime( '[DATE_FIELD_1]' );


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You: ericmuc

Please Log in to join the conversation.

3 years 6 months ago #320683 by ericmuc
Replied by ericmuc on topic Code field validation
Hi,
Great, this information I needed, now it works. I had only change to
return strtotime( '[value]' ) >= strtotime( '[DATE_FIELD_1]' );
Thanks a lot, best regards
Eric

Please Log in to join the conversation.

3 years 6 months ago #320684 by ericmuc
Replied by ericmuc on topic Code field validation
Hi,
No I have a question hereto:

The error message should be language sensitive. But with a language string there is no translation.

How can I get the error message in the appripriate language?

Thanks, best regards
Eric

Please Log in to join the conversation.

3 years 6 months ago #320685 by ericmuc
Replied by ericmuc on topic Code field validation
Hi,
Sorry, but I have an additional question.

I realised that the date2 also may not be greater than today.

How can I add this second validation in code field validation? Can you give me a hint how to use this here?

Thanks, best regards
Eric

Please Log in to join the conversation.

3 years 6 months ago #320686 by ericmuc
Replied by ericmuc on topic Code field validation
Hi,
Sorry for a further question:

I have following situation:

1) the user takes a wrong date2, the error message appear
2) he don't change the date2 appropriate and let it unchanged so wrong
3) he changed some other field contents
4) he saved

Result: all changes in the fields are lost, because the date2 was not appriate.

So is it be possible to blcok the save button until date2 has noc validation error anymore and is set right?

Thanks, best regards
Eric

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.233 seconds

Facebook Twitter LinkedIn