Umbraco.Forms.Issues
Umbraco.Forms.Issues copied to clipboard
Notifications for "FormRendering" and "FormPosted" (or similar)
For usability, we try to avoid recaptcha in our forms and use other measures to avoid SPAM.
Currently, we need to create a custom view for the form rendering to inject a hidden field and then when the form is posted we've added a hack that will spin up a new thread that will wait and evaluate the value from this field and remove the record from Form.
In an effort to try to use as many "out of the box"-features as possible I wanted to ask if it would be possible to add some notifications that could facilitate the following:
-
Append a hidden field to the form and have it rendered as if it were part of the form. Here one option is to customize the form-rendering but to make upgrades easier I'm looking for a way to avoid this.
-
Evaluate the posted record before it's saved to the database and have the option to "ignore" it. Here we don't want to show an error to the users as this would help spammers to bypass our logic, we just want to ignore and log that this happened. I know that there is a
FormValidateNotification
andFormSavingNotificationHandler
but as far as I can tell this "silent ignore" that I'm looking for is not possible. I have not managed to find any notification for when the form is posted - I'm looking for something like aFormRecordPostingNotification
.
Maybe there are solutions for this but I have not managed to find them =/
Cheers!
There is a FormPrePopulateNotification
. I'll confess I've not had reason to use this or investigate it, it's just something that has been carried over version to version. I think the intention originally was more to pre-populate values, but you do have access to the Form
instance so perhaps you can manipulate the fields. But maybe you could look into that and see if it would help with what you are looking to do.
There's also a RecordSavingNotification
that is cancellable - could be that's what you could hook into for point 2.
Thank you so much for taking the time to answer @AndyButland!
I looked at these events last week and have spent some time now to look closer and create a POC.
It's possible to add fields to the form using FormPrePopulateNotification
(via form.Pages....). This works for the initial load, in this case FormPrePopulateNotification
is called before FormViewModel.Build(). But if the Form has multiple Pages it will not work for subsequent page-loads, when page two loads FormViewModel.Build() is fired before FormPrePopulateNotification
for some reason which means that the view model for the forms I already built before I have the chance to inject my fake field =/. I suspect that this also means that it would not be possible to use FormPrePopulateNotification
to pre-populate fields on pages other than the first?
The RecordSavingNotification
works as you describe and it's possible to silently ignore a submitted form. At the moment I need to grab the value of the "injected" field via the HttpContext which works fine.
The thing that prevents me at the moment is that I can't inject any value on other pages than the first page.
I think what I'm really looking for here is some kind of way to store something in the "RecordState" when the form is loaded the first time and then read this in RecordSavingNotification
.
Hi again!
I did find a temporary workaround to pass data through the form and that is to use the ITempDataDictionaryFactory
and set my value when the form loads and then remove it when the form is posted. I would prefer to be able to pass the data with the form post it self but this works as a good workaround
That's good to hear, though it does sounds like you are having to workaround this a bit. Hopefully next sprint (in a couple of weeks) we'll get chance to look into this a bit deeper to see if it can be made simpler. At least it does seem you should be able to hook into FormPrePopulateNotification
as you need for multi-page forms.