Form Validation with Macro

Sandro Jankovic asked on August 9, 2016 17:43

Hello,

I could use a hand setting up some form validation. We are setting up event registration through a form that has a checkbox for each event date. The idea is to check if a specific day is already at capacity when a checkbox is selected. I setup the following macro to get a count of existing registrations and now I'm wondering how to properly use this to trigger the error message when the Count of registrations is higher than the Max that I set in the code:

{%
    Max = 1;
        Total = GlobalObjects.Classes["BizForm.EventTest"].Items.ItemsAsFields.cb3;
    Count = 0;
            foreach (Response in Total) {
            if (Response == True){
                Count += 1
            };
        }

    if(Count < Max){
    return true;
    }
    else{
    return false;
    }
#%} 

This will probably be re-written but I am just trying to get an idea of how to use a macro for form validation in this way.

Any help would be appreciated!

Best Regards,
Sandro

Correct Answer

Jan Hermann answered on August 15, 2016 15:00

Not "this" but the field name even on the field you test:

checkboxfield.Value

0 votesVote for this answer Unmark Correct answer

Recent Answers


Chetan Sharma answered on August 9, 2016 18:06

Hi Sandro,

I don't have complete context here however I think you can achieve this OOTB in Kentico where you don't allow event over booking by checking a property in the webpart.

Thanks, Chetan

0 votesVote for this answer Mark as a Correct answer

Sandro Jankovic answered on August 9, 2016 18:08

Hi Chetan,

Thank you for your reply. The events are setup across multiple days with different capacities for each day - we are using checkboxes to allow users to specify which day they would like to register for. We were advised by Kentico Support to use this approach (or a custom method) but I am not clear on how to actually use this as a General Condition to trigger validation (or ignore it) based on the macro results.

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on August 9, 2016 18:48

Seems like this would make more sense to have a page type that would contain different booking events. That way you can have a main event, and choose the events(which in this case could be the different registrations or days). May be more setup for the end user, but also would be out of the box functionality. So for each main event(new page type) you could select the events to allow sign up for, Maybe through a uniselector or something along those lines.

The only downside is that the end user then has to create the registrations after the event and attach them. Plus your transformations will need to handle multiple (booking events)registrations underneath, so it may make sense to take what you have and see if you can figure it out. Just throwing out an alternative for a last resort.

You could always clone the kentico webpart for event registration and customize it to check those properties and use that approach if you can't figure out the macro.

0 votesVote for this answer Mark as a Correct answer

Jan Hermann answered on August 10, 2016 08:59

Hi Sandro,

why so complex macro :) Try this one:

{% SiteObjects.Forms.EventTest.Items.Where("Response=1").Count>0 %}

2 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on August 10, 2016 14:25

Jan, I must admit that Kentico's documentation require a lot of overhaul around these.

0 votesVote for this answer Mark as a Correct answer

Sandro Jankovic answered on August 12, 2016 19:17

Hi Jan,

Thanks for that, I'll update the macro once I figure out how to actually use it. The problem I'm coming across with this approach is that validation is running even if the checkbox is not selected. Is there any way to check the current value of the checkbox in the macro - like this.Value?

0 votesVote for this answer Mark as a Correct answer

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