Jump to content

Getting template name from a text field for form builder


thetuningspoon
 Share

Recommended Posts

I'm stumped. I'm playing around with Ryan's formbuilder. For the template that loads the form, I would like to make the form template to be loaded something which can be set on the backend through a text field (or, ideally, a drop-down selection box with the templates pre-populated, but I can't even begin to think of how to do that).

So here is what I'm doing. The 3rd 2nd line down is the one that I'm having trouble with. form_name is the textfield which holds the name of the template I want to load. I get the error: "Fatal error: Exception: You must specify a Template"

$form = $modules->get('FormTemplateProcessor');
$form->template = $templates->get($page->form_name); // required
$form->requiredFields = array('fullname', 'email');
$form->email = 'mspooner@solutioninnovators.com'; // optional, sends form as email
$form->parent = $page; // optional, saves form as page
echo $form->render(); // draw form or process submitted form
Link to comment
Share on other sites

(or, ideally, a drop-down selection box with the templates pre-populated, but I can't even begin to think of how to do that)

There isn't a dropdown field in the back end, but you can do the same with a Page FieldType. You will have to create a simple page (only title) for each option under the same parent, create a Page FieldType pointing at that parent, and put it on your template. Use the titles of these pages as your options.

The page object will be returned as a result of a call to the field, so you will need to do this (considering that form_template is the page field):

$form->template = $templates->get($page->form_name->title);

Honestly, I still think that this method is not intuitive at all. But this was discussed already in other threads, and I do agree that it works well and that it's very easy and fast to create pages as options.

The 3rd line down is the one that I'm having trouble with

I'm assuming you meant the 2nd line, because I don't see any logic if it's the 3rd.

Link to comment
Share on other sites

Thanks for your help, diogo, and for explaining the Page FieldType option.

But going back to using a simple textbox, I still have the problem of getting the $page->form_name call to work in this context. If I use it elsewhere on the page to simply echo the content of the field, it works fine. But when I put it in the context of the code block above (2nd line, sorry), I receive an error.

Link to comment
Share on other sites

The error you're getting does not come from 2nd line, but from the last. $form->render() checks whether a template has been set or not and now it apparently isn't. As the code looks allright and you're saying that echoing the content "works fine" (I'm presuming something is actually displayed), the problem must be that no template can be found with the given name.

So, double check that the template name in $page->form_name actually matches an existing template - I'm betting it doesn't. A simple test checking that the chosen template was successfully retrieved would be a good thing to have here. Even if you changed from textfield to a some kind of selection from existing templates, there's always the possibility that a previously selected template does not exist anymore.

Link to comment
Share on other sites

What does this do?

echo $templates->get($page->form_name); 

form_name is a field on the current page which holds the name of the template I want to load. That's what it's supposed to do, at least. If I hardcode the template name in, it works fine. But this bit of code doesn't seem to work.

Link to comment
Share on other sites

@everfreecreative: I think Ryan meant what does that given echo output - did you try that exact thing Ryan wrote? If $templates->get() succeeds, it returns a template object and echoing that object should give an id. If it echoes nothing, then the problem is that a template can't be found. Then again, if it does echo a number (I think that's what it resolves to), the problem is somewhere else :). You could also try the same echo with hardcoded template name and tell us results from both (at least this one should output a template id).

  • Like 2
Link to comment
Share on other sites

It sounds like the template you are trying to retrieve does not exist. I have a feeling there are some extra characters in $page->form_name, and that's why Soma's suggest to trim() and strip_tags() may be a good one. It's very possible you've got some textformatter on there putting "<p>" tags around it, or doing something. So make sure no textformatters are manipulating the field (can be seen on the details page). You can examine exactly what is in $page->form_name by doing this:

echo "'" . htmlentities($page->form_name) . "'";

I'm guessing you've got something in there more than just the form_name, or perhaps it's completely blank.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...