Jump to content

Field-template context on frontend forms


Pete
 Share

Recommended Posts

Hi folks
 
I'm just playing with a form in a frontend template that pulls fields from a backend template and outputs them as form fields on the frontend (all will become clear soon :)).
 
Unfortunately, the template-specific versions of the field labels won't display - it just reverts to the default. Any ideas?
 
This is what I'm doing in my frontend template:

foreach ($templates->get('your-details')->fields as $field) {
    echo "<label for='$field->name'>$field->label</label>";
    $inputfield = $field->getInputfield($page);
    echo $inputfield->render();
}

 
The default label for the field "fullname" is "Full Name" and on this specific template in the admin it's "Your Name", but using the code above it outputs "Full Name" despite the template-specific label.
 
I also tried doing this before the code above:

$newpage = new Page();
$newpage->template = 'your-details';
 
...
 
$inputfield = $field->getInputfield($newpage);

 
but that didn't work either.
 
I suspect the answer is something simple as usual :)

Link to comment
Share on other sites

@Martijn - there is no page. I'm just getting a blank template and echoing fields for a frontend form so the form doesn't relate to a page. The form fields do relate to the template they came from though so it looks like it should work.

@kongondo - none of that seems to apply in this situation. I did try a few things from that page but they didn't work.

Part of the problem is probably that the page I'm outputting this on doesn't have the same template as the fields I'm outputting in my code, but it looks like it should work anyway.

Keep the suggestions coming though :)

Link to comment
Share on other sites

Nice one Reno. I just tested and it works!

My quick mash - I am sure it could be improved.

$template = $templates->get("your-details");
foreach ($template->fields as $field) {
    $fieldlabel = $template->fieldgroup->getField($field, true)->label;
    echo "<label for='$field->name'>".$fieldlabel."</label>";
    $inputfield = $field->getInputfield($page);
    echo $inputfield->render();
}
 
  • Like 6
Link to comment
Share on other sites

Cheers all!

My code now looks like this and works perfectly:

$template = $templates->get('your-details');
foreach ($template->fields as $field) {
    echo "<label for='$field->name'>" . $template->fieldgroup->getField($field, true)->label . "</label>";
 
    ...

EDIT: Which I see is the same as what Adrian posted above - it's nice when you come to the same conclusion separately sometimes though as you know it's definitely correct that way ;)

  • Like 2
Link to comment
Share on other sites

Well I kind of meant I wasn't being very clear at the beginning and hopefully the code example would make it clear. I guess I made it less clear by saying "all will become clear" :D

Link to comment
Share on other sites

It is a nice way when you need to roll your own forms on the frontend (in my case it's a bit complicated/customised for FormBuilder or that would have been my choice) and you don't want to write the form from scratch.

This way means you can build your form in the admin as a template and then just iterate through the fields on the frontend and style them how you like.

In the particular site I'm doing this on, there are a lot of one-to-many relationships happening, so there's one long form where I'm outputting the fields from multiple admin templates - in some cases with an "Add" button so I can add another set of fields with some JS - and then it'll all get saved when submitted using those separate templates.

Link to comment
Share on other sites

  • 3 months later...

This method looks very good, collecting the field and values in one time.

But what about the title field which is always required? You can't leave it out from the template.

So, how to present that on the front-end?

-- EDIT --

I just used continue; to skip Title

                 $template = $templates->get("update_profile");
                        foreach ($template->fields as $field) {
                            $fieldlabel = $template->fieldgroup->getField($field, true)->label;
                            $inputfield = $field->getInputfield($page);
                            if($fieldlabel == "Title") continue;
                            echo "<label for='$field->name'>".$fieldlabel.$inputfield->render()."</label>";
                        }

I am curious as to see how you all use this. Meaning: a new form for adding details is just displaying all fields.

But after it is submitted there is a chance people like to change all, or some. So how do you proceed from there?

Link to comment
Share on other sites

hey cstevensjr, I'm not using form builder. What do you mean with save to a configuration page?

I am using the mentioned code on a profile page - available only to the logged in user.

It post the selected values to this member's page - and that page is visible to members only.

The easiest way would be to collect what was chosen before (I guess).

As example - I can get those values by using :

$member_page = $pages->get("template=member,name={$user->name}");

And then their values:

$member_page->fieldname->title

But it would be cool to have those returned in the select option.

It's just, the following code that make me wonder how or were this markup is done, since it displays the field as intended. Meaning, it has the <select> and <option> and just output's it like it suppose to:

$inputfield = $field->getInputfield($page);
echo $inputfield->render()
Link to comment
Share on other sites

OK.

You have a form named "Company Contacts" that you have someone fill out.  Form fields are "Company Name", "Address", "Telephone Number" and "Contact Name".

You then have an existing page template "Contacts" that have fields "Title", Company Address", "Telephone" and "Contact".  

As the last part of your form logic, you through code:

  1. Create a new page using the "Contacts" template.  The parent page is called "Contact List"
  2. Map the Form Field information with the Page Field data, most likely variables, as such:
  • "Company Name" maps directly to "Title".
  • "Address" maps directly to "Company Address".
  • "Telephone Number" maps directly to "Telephone".
  • "Contact Name" maps directly to "Contact".
  • You then save the page.  In the future, you can go to this page and edit/modify the data.
Link to comment
Share on other sites

Maybe I need to explain some more...

This code is now on my profile.php, displaying the form fields which are set via "update_profile" template:

                    $template = $templates->get("update_profile");
                        foreach ($template->fields as $field) {
                            $fieldlabel = $template->fieldgroup->getField($field, true)->label;
                            $inputfield = $field->getInputfield($page);
                            if($fieldlabel == "Title") continue;
                            echo "<label for='$field->name'>".$fieldlabel.$inputfield->render()."</label>";
                        }

After submitting the form, it posts the values to the user member page. The member.php is the template that just outputs the values.

A side note: on any other current page I could access the user member values by using this :

$member_page = $pages->get("template=member,name={$user->name}");

And then this :

$member_page->fieldname->title

But the question was: how to get the values which could be on the member page already, into the field on the form (pre-selected or something)

Using this code on the form:

$inputfield = $field->getInputfield($page);
echo $inputfield->render()

I already have field values displayed on the member page using this:

$page->contract->title

I did not really understand what you meant by mapping, but if it is what I already do (displaying those values)... how to make them appear in the form?

Link to comment
Share on other sites

normally it would be:

$inputfield = $field->getInputfield($page);

$selected = $member_page->get($field->name) == $inputfield ? " selected='selected' " : '';

and then do:

<option$selected value='{$field->name}'>{$field->title} </option>";

but now:

echo $inputfield->render()

has already the <option> tag....

-- EDIT --

think I need better vision. lol.

Martijn gave a good hint, but I did not read the code through. It stated:

// when a value is required and the value is empty and a default value is specified, we use it.
$this->attr('value', $value);

So the solution was simply setting it to what Martijn mentioned:

$inputfield->value = $member_page->get($field->name);

And then use render()

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