Added this to the documentation for ad_form:
Important note about how ad_form works: ad_form operates in two modes:
- Declaring the form
- Executing the form
Through the -extend switch, you can declare the form in multiple steps, adding elements.
But as soon as you add an action block (on_submit, after_submit, new_data, edit_data, etc.),
ad_form will consider the form complete, and execute the form, meaning validating element values,
and executing the action blocks. The execution will happen automatically the first time you
call ad_form with an action block, and after that point, you cannot -extend the form later.
Also, if you don't supply any action blocks at all, the form will never be considered finished,
and thus validation will not get executed. Instead, you will get an error when the form is rendered.
Bottom line:
- You must always have at least one action block, even if it's just -on_submit { }.
- You cannot extend the form after you've supplied any action block.
I also added a check to form::render that checks if this is an unfinished ad_form, i.e. an ad_form that doesn't have an action block, and if so throws an error.
This helps people not shoot themselves in the foot, when they add validation and don't understand why it never executes, when the reason it never executes is that the form doesn't have an action block (because it doesn't need it).
This means that all forms will have to have an action block, even if just a dummy one, and will breaks forms that don't, but after discussion, we think this is safer. If there's a general uprising, however, we'll gladly revert. it's a very small change.
The longer-term fix, I think, is to explicitly break ad_form into two parts:
- ad_form_declare
- ad_form_execute
One of the things you can't do today is to use -extend to add a new action block, because as soon as ad_form sees the first action block, it chooses to fire. I think explicitly making the distinction above would make it easier for people to figure out what's happening.
/Lars