Introduction to Entityforms Part 2

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.

Fig 1.0

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”.

Fig 1.1

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.

Fig 1.2

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.

Fig 1.3

8. Select “Accounts” from the Value drop-down list within the “Data value” field-set and click on Save.

Fig 1.4

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.

Fig 1.5

At this point, we have finished configuring our submission rule. The rule edit page should look like the image below.

Fig 1.6

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”.

Fig 1.7

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.

Fig 1.8

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.

Fig 1.9

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.

Fig 1.10

10. Go to the edit page for the form and select the rule from the “Validation rules” field in the “Validation settings” vertical tab.

Fig 1.11

If you have any questions, please leave a comment.

16 thoughts on “Introduction to Entityforms Part 2”

  1. 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!

    1. 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.

  2. 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.

  3. 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.

    1. 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.

  4. 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.

    1. 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.

  5. I was wondering, what would be the internal drupal path for entity forms? I need that to add to the list for captchas.

  6. 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?

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.