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.
Basic knowledge of Rules is required if you want to follow along with this tutorial. If you haven’t used Rules before, it is well worth learning.
This tutorial continues with the same “complaints” form from part 1, if you’re following along make sure you have read “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.
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.
5. Select 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.
7. Select 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.
6. Select 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.
Thanks for the write up on the module. Very clear and concise tutorial.
If you are interested in ideas for the 2.x version of Entityforms you can check out this issue: http://drupal.org/node/1896712
Thanks for the link.
Hi Ivan. Thanks for this tuto. Very useful!
I wonder how can I setup an entityform with a survey (to be used by anonymous users) and then, on submit, create a user with the data collected in the form. (I need two different “registration” forms in my project). So, what I did: I created a simple entityform with fields… and added a submission rule to create a new entity of type user, but nothing is created when i see my users list. I can see the submissions, that’s ok, but no new users at all.
Can you please point me in some right direction?
Thanks in advance!
Good question.
First, I would check for created users from the “users” table in the database and not from the Users list page.
Second, make sure your rule is actually firing off. To test this, just add an action that displays a message.
Third, make sure you are assigning a role to the user entity.
I also did a quick google search and found this link:
http://groups.drupal.org/node/244508
Hope this helps.
Khiustin you can also turn on debugging in Rules and it will give you a detailed list of what it is doing.
Nice tutorial!! Very handy!
Do you know a way to customize the email body using the fields filled by the users?
I’d like to send a nice formatted HTML.
By default, Drupal sends emails as text and not HTML. There are modules that you can use to send HTML emails.
Take a look at Mime Mail (https://drupal.org/project/mimemail) or HTML Mail (https://drupal.org/project/htmlmail).
Thanks for this tutorial.
I have an entity form with an e-mail field. Is there any way to check if the e-mail address being submitted has already been used in a previous submission. If it has been used 1. prevent the present submission and 2. display a message.
I tried data comparison. However I am not certain with what to compare the present submission. I am also not certain how to prevent the form from being submitted. I figured about displaying the message.
I have never done any type of duplicate check using Entityforms, but it “should” be possible using Rules.
You’ll have to create a rule that loads the existing submissions and loops through them. While the rule is looping through the existing submissions, then do a data comparison.
Here is a screencast about lists and loops in Rules:
http://nodeone.se/en/lists-and-loops
Hope this helps.
Your article is quite insightful. Well done! However I was wondering if there is a way to re-arrange the placement of the fields in the form created with entity forms. I would like to place some of my fields side by side instead of beneath one another.
Also, even though I enabled Entity Notifications, it doesn’t show within the entity form settings and so I am unable to indicate the email addresses submissions should go to. Is there something else I’m missing? I have Variable and Variable Admin modules enabled and I have just cleared my site cache.
Your article is quite insightful. Well done! However I was wondering if there is a way to re-arrange the placement of the fields in the form created with entity forms. I would like to place some of my fields side by side instead of beneath one another.
You should be able to float fields side-by-side using CSS. But, it depends how customised you want the form to be.
Also, even though I enabled Entity Notifications, it doesn’t show within the entity form settings and so I am unable to indicate the email addresses submissions should go to. Is there something else I’m missing? I have Variable and Variable Admin modules enabled and I have just cleared my site cache.
Not sure what the problem could be. Look for PHP errors and try setting up the module on a fresh Drupal 7 site.
I was wondering, what would be the internal drupal path for entity forms? I need that to add to the list for captchas.
The “Submit link” URL on admin/structure/entityform_types is the internal Drupal path.
I really appreciate the work you put into this article, it;s very helpful. The problem I have is that I’m not able to select “validate-form” from the data selector. There is no such position there. Do you maybe have any idea why it happens? I also have no “Validation settings” neither “Notifications settings” in the edit page. I’m using up to date Drupal installation with all up to date modules (it’s today’s installation so it’s fresh). Can you help me with that?
I think you’re using the 7.x-2.x version of Entityforms. Is this right?
This tutorial is only for the 7.x-1.x version.
Yes, you’re right, that was the issue. Thank you!