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 video, 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.
Basic knowledge of Rules is required if you want to follow along with this video. If you haven’t used Rules before, it is well worth learning.
This video continues with the same “complaints” form from part 1, if you’re following along make sure you have watched “An Introduction to Entityforms Part 1”.
Resources
Transcript
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 video, 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.
Basic knowledge of Rules is required if you want to follow along with this video. If you haven’t used Rules before, it is well worth learning.
This video continues with the same “complaints” form from part 1, if you’re following along make sure you have watched “An Introduction to Entityforms Part 1”.
Submission Rules
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.
Go to Structure, “Entityform Types” and click on “Submission Rules”. To save time we’ll clone the “Email Entityform Admin” rule. Click on the clone link and change the name to “Email Entityform Admin Accounts” and click on “Save changes”.
Now we need to create two conditions that will be used to check if “Accounts” is selected. Click on “Add condition” and select “Entity has field”. Select entityform
from the “Data selector” field, and field_department
from the Field drop-down and click on Save.
Click on “Add condition” again and select “Data comparison”, then click on Continue. Select entityform:field-department
and click on Continue. Select “Accounts” from the Value drop-down list and click on Save.
Finally, we need to modify the existing action. Click on Edit and enter in an email address into the “To” field and click on Save. Now that we have created our new submission rule, we need to configure our “complaints” form to run the rule.
Go to Structure, “Entityform Types” and click on “edit”. Click on “Notifications settings” 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, 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 fails. 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, 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.
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.
Go to Structure, “Entityform Types” and click on “Validation Rules”. Click on the “Add a validation rule” link, select Rule from the “Component plugin” drop-down list and click on Continue. Enter in a name for your validation rule and click on “Save changes”.
For this rule I’ll create two conditions that will load and check the Name field. Click on “Add condition” and select “Entity has field”. Select entityform from the “Data selector” field, and field_name from the Field drop-down and click on Save. Click on “Add condition” again and select “Data comparison”, then click on Continue. Select entityform:field-name and click on Continue. Enter in ivan into the Value text-area and click on Save.
Click on “Add action” and select “Set a data value”. Select validate-form
from the “Data selector” field and click on Continue. Leave the Value checkbox unchecked, this means the action will return as FALSE. Click on Save.
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. Click on “Add action” and select “Show a message on the site”. Enter in a message value, select Error as the message type and click on Save. Then save the rule.
Now we need to configure entityforms to fire off the validation rule. Go to Structure, “Entityform Types” and click on edit. Click on “Validation settings” and select the rule that we just created and click on “Save entityform type”.
Click on “Submit link” and if you try and submit a form with the name ivan, the rule should stop you from submitting the form. If you try John, the form should submit.