Sending, saving and updating a contact form.
Created 7 years ago by william


  • As a visitor on a public page, i see a contact form.
  • As a visitor i am able to fill out the contact form and submit it.
  • As an admin, i receive a notification in my email when the form has been submitted.
  • As an admin, i am able to view the form in the backend as well as edit it (or maybe fill out an extra field such as notes).

Displaying a form, using a handler to send the email as well as the builder saveForm is all taken care of and works. I am also able to see all submissions in the backend as an admin.

Question 1 If i want to show the admin an extra field that the public user don't see, i guess i basically just skip that field for the front end. Is that the preferred way of doing it?

Question 2 In my ContactFormBuilder (extending FormBuilder) i have this:

protected $actions = [
            'submit' => [
                'redirect' => false,
                'text' => 'Send'

Works fine, outputting a send button to the front end. However, it also appears for the admin. Again, would i sort this out by building up the form in the front end using twig and outputting only the fields i want ? Or is there another way to control what buttons to be shown to the public user and what buttons to render for the administrator?

Question 3: In my ContactFormBuilder, i have my own handler set

protected $handler = ContactformFormHandler::class;

This is where i save the form as well as send the email. However, its not only called when a front end user submits the form, but also when an admin edit/saves the form in the backend. What is the best approach here. In its simplest scenario, i guess you could check the user and see if he is the admin and in that case not send the email, and just save the form.

But it feels messy, is there a good way to separate the view/logic from public user and the admin?

william  —  7 years ago

Regading #3, maybe its enough to check getFormMode() ?

ryanthompson  —  7 years ago

Regarding 1: Yes that's a perfect way of doing it (probably two builders or at least a fields handler).

Regarding 2: Again just duplicate your builder - extend the one with a public version.

Regarding 3: You guessed it! Duplicate your builder.

You may have to set the protected $model = YourModel::class; on the duplicate one since the name won't pick up your model automatically.