4

I have a few different forms on my site for different kinds of enquiries, each represented by an Entry Type with different fields in a single Enquiries channel. How can I get the entries/saveEntry form to save as a specific entry type?

My existing form:

<input type="hidden" name="action" value="guestEntries/saveEntry">
<input type="hidden" name="redirect" value="/hire-us/get-a-quote/success">
<input type="hidden" name="sectionId" value="61">

I have two entry types in that channel, ID 111 and 112. When I add the line:

<input type="hidden" name="typeId" value="111">

then submit the form, I get an error message saying "Unable to find the template 'hire-us/enquiryPlanningEvent'". It appears that Craft is looking for a template named after the entry type I'm trying to save, but obviously there isn't one. What's the solution to this?

5
  • If you enable Dev Mode Craft will give you a full stack trace when that error occurs. That may give you more insight into why it’s happening. Commented Oct 22, 2014 at 18:26
  • No stack trace I'm afraid Brandon - I'm seeing this error: i.imgur.com/aYj2a3d.png Commented Oct 28, 2014 at 17:46
  • Do you have any idea how that template is getting invoked? Maybe via the redirect param? Commented Oct 28, 2014 at 21:45
  • I did solve this - but I don't know why the error I got was happening. Basically if the Title field is expecting a value (i.e. not set automatically), and I submit the form without setting a title, then it redirects me to a template with the same handle as the Entry Type I was trying to submit to (regardless of the redirect value). Bug? Commented Oct 30, 2014 at 10:40
  • Actually it's not just if the Title field is missing, it happens if any required field has no data. Filing a bug report. Expected behaviour is that it should reload the page and call the errorList. Commented Oct 30, 2014 at 10:53

3 Answers 3

4

This has been solved via a support ticket, which I'm posting here for others etc.

The first time you load the template, the URI is mapping to your "Get A Quote" entry, which gets assigned to the entry variable when your template is loaded.

When you submit the form, the same URL is requested again, once again matching your "Get A Quote" entry, but this time the guestEntries/saveEntry controller action is summoned. When there's a validation error, Guest Entries will pass the new entry back to the template as an entry variable, overriding the "Get A Quote" entry that the URL matched. So, then the template gets routed to hire-us/index.html like it normally did, but this time entry.type is enquiryWeddingDance instead of getAQuote, since it's a different entry altogether. The template tries to include hire-us/enquiryWeddingDance.html, but that doesn't exist, so you get the error.

To fix this, I've just released an update to the Guest Entries plugin, which adds a new entryVariable config setting. You can use that to customize the variable name that Guest Entries will set the invalid entry to (i.e. 'guestEntry' or 'invalidEntry'), so it doesn't conflict with the entry variable that the request is getting routed to.

After setting the Guest Entries variable as suggested in the documentation, it's now working as expected without conflicts. Thanks Brandon!

Usage:

{% if guestEntry is defined %}{{ errorList(guestEntry.getErrors('variableName')) }}{% endif %}
1

I think the way you are trying to add entries requires authentication, if you look on the entry form page in the craft docs, you will see this snippet of text:

The user submitting the entry will also need to have the permission necessary to create entries for the section they are posting to

So I think you would need to use the guest entries plugin (from P&T), unless i'm mistaken, so maybe give that a try and see if it fixes the issue?

Guest Entries plugin on Github

The difference in markup is here:

<input type="hidden" name="action" value="guestEntries/saveEntry">

This is the way I have done this on a site i've built and not had any issues, entries get saved and to the specified type. :)

3
  • Thanks Alec - I've installed GuestEntries (updated question) but it's not solved the problem. When I submit the form, I get taken to the Template Error page in the question (i.imgur.com/aYj2a3d.png). It's something to do with the way I'm redirecting structure templates... Commented Oct 28, 2014 at 17:49
  • Alec - I'm guess the sites you've done in the past have used a different template routing structure. The error here isn't being caused by permissions, it's because the form is looking for a template that doesn't exist. No idea why it's looking for a template in the first place! Commented Oct 28, 2014 at 17:57
  • Added a new answer as it's completely unrelated to the guest entries answer :) Commented Oct 28, 2014 at 18:03
1

Based on the comments from my other answer and seeing the image you sent.

Look in the template hire-us/index.html where you are trying to include a template dynamically with the entry.type variable, what that is generating does not correspond to a template you have, so it can't find the template to include and therefore you are getting the error. As the error States craft is unable to find the template

Try removing the include altogether and just put {{ entry.type }} in the template and go through the process again so you can see what's being generated and get the name of the template you need to create to satisfy the include.

Hope that helps

2
  • Hi Alec, thank you for all your help, but it turns out it was simpler than expected. Answered my own question, it was because the Title field was missing >< Commented Oct 28, 2014 at 18:26
  • Ahh no worries, glad you got things sorted in the end! :) Commented Oct 28, 2014 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.