Update: This tutorial uses the 7.x-1.x version of Entityforms. The notification and validation settings have changed in 7.x-2.x, which is the stable version.
In part 1 of this series, we created a basic “complaints” form using Entityforms and attached a few fields to the form. We also setup email notifications for when a form is submitted using “Entityforms Notifications” module.
In this final part, we’ll look into the Rules integration with Entityforms. We’ll learn about and create an access, submission and validation rule. After we’ve played around with the different types of rules you should have a better understanding of how they work.
As the name suggests, submission rules are executed whenever a submission is entered into the system. A great example of this type of rule is the email notification that we configured in part 1. If you need to run some custom logic when a submission is entered, then look at creating this type of rule.
In this section, we’ll setup a rule that sends an email if a user selects “Accounts” from the Department drop-down list.
1. Go to Structure -> “Entityform Types” and click on the “Submission Rules” tab.
2. To save time we’ll clone the “Email Entityform Admin” rule. Click on the “Email Entityform Admin” clone link.
3. Change the name to “Email Entityform Admin Accounts” and click on “Save changes”.
4. Click on the “Add condition” link and select “Entity has field” from the condition drop-down and click on Continue.
entityform from the “Data selector” field,
field_department from the Field drop-down and click on Save.
6. Click on the “Add condition” link and select “Data comparison” from the condition drop-down and click on Continue.
entityform:field-department from the “Data selector” field and click on Continue.
8. Select “Accounts” from the Value drop-down list within the “Data value” field-set and click on Save.
9. Click on Edit within the “Send mail with Variable” action and enter in an email address into the “To” field and click on Save.
At this point, we have finished configuring our submission rule. The rule edit page should look like the image below.
Configure Submission Rule On Form
Now that we have created our new submission rule, we need to configure our “complaints” form to run the rule.
1. Go to Structure -> “Entityform Types” (admin/structure/entityform_types) and click on “edit” within the Complaints row.
2. Click on the “Notifications settings” vertical tab and select the “Email Entityform Admin Accounts” option from the “Submission rules” field and click on “Save entityform type”.
Now, if a user submits a form with “Accounts” selected in the Department drop-down list, then an email will be sent to the email addresses specified in the action.
Access And Validation Rules
Access and validation rules work a bit differently to a submission rule. For validation rules you need to create an action that sets the
validate_form variable to FALSE if validation has failed. Likewise, for access rules you’ll need to create an action that sets the
show_form variable to TRUE if the form should be displayed.
In the section below, I’ll only show you how to setup a validation rule, however once you know how to create this type of rule you also know how to create an access rule. Just remember that the variable should be
validate_form for a validation rule or
show_form for an access rule.
Setup Validation Rule
Creating a validation rule is exactly the same as a submission rule. The only thing we’ll do differently is create an action which sets the
validate_form variable to FALSE. This variable is used by Entityforms to figure out if the form has passed validation.
1. Go to Structure -> “Entityform Types” and click on the “Validation Rules” tab.
2. Click on the “Add a validation rule” link, select Rule from the “Component plugin” drop-down list and click on Continue.
3. Enter in a name for your validation rule and click on “Save changes”.
4. Create your required conditions for the rule.
5. Click on “Add action” and select “Set a data value” from the “Select the action to add” drop-down list.
validate-form from the “Data selector” field and click on Continue.
7. Leave the Value checkbox unchecked, this means the action will return a FALSE.
8. Finally, we’ll have to create a “Show a message on the site” action which should inform the user why the form hasn’t been submitted.
9. Once the rule has been setup, the action section should look like the image below.
10. Go to the edit page for the form and select the rule from the “Validation rules” field in the “Validation settings” vertical tab.
If you have any questions, please leave a comment.