Simple Form Rendering with The FormRow Helper

PHP 

The formRow view helper renders the label, element, and errors, based on the element passed to it. Let’s have a quick look at how it works.


tl;dr

In short, the formRow view helper renders the label, element, and errors, based on the element passed to it.

Recently I was working through some code in the ZF2 foundations book, where I’ve encouraged being very explicit about how to render form elements in view templates.

Specifically I suggested using the formLabel, formElement, and formElementErrors view helpers as below.

$element = $form->get('title');
echo $this->formLabel($element);
echo $this->formText($element);
echo $this->formElementErrors($element);

Taking this approach gives you complete control over exactly where your elements will render; so any layout is then possible. But as a result, there’s more code to develop, debug, and maintain.

Whilst there’s nothing wrong with this approach, it’s a bit of a long way to do it. Rob Allen suggested the FormRow helper instead.

I’ve not spent a lot of time with it, but on reading through the code, realised it offers a lot of the same functionality achieved by being explicit, but for much less effort.

So in today’s post, I’m going to take you through the formRow view helper and show you how to use it. Below is the same code, but using the formRow helper:

$element = $form->get('title');
echo $this->formRow($element);

In it, we still have to retrieve the element to pass to it, but after that, it’s just one call, not three. We don’t have to make sure that we use the correct element view helper, as formRow determines that automatically. We don’t have to explicitly use formElementErrors, as that’s automatically handled as well.

Configuration Options

There’s not much more to it than this; but there are a few configuration options available. Let’s quickly step through them.

Label Position

By default, it renders the label around the element, prepending the label, with errors enabled. If you want to change this, then specify prepend as the second argument. If you’ve prepended an earlier element, remember that view helpers are shared.

$element = $form->get('title');
echo $this->formRow($element, ‘append’); // or prepend

So any settings applied in an earlier invocation will be used for the remainder. So if you have, say, 3 elements and just want to append the third one, then pass prepend in the third, to restore the original functionality.

Disable Wrapping of Elements by Labels

This takes a bit more effort, but not much. To have the label and element rendered separately, you need to call the setAttribute method on the element, specifying an id, as follows:

$element = $form->get('title');
$element->setAttribute('id', 'title');
echo $this->formRow($element);

Display of Errors

Errors are displayed by default. But if for some reason you don’t want to render them, then pass false as the third argument.

$element = $form->get('title');
echo $this->formRow($element, null, false);

Wrapping Up

And that’s the essentials of using the formRow view helper. Whilst simpler than using formLabel, formElement, and formElementErrors; it’s also more efficient in a number of cases.

So give it a try and save yourself time and effort when rendering forms in Zend Framework 2.If you’ve already bought a copy of Zend Framework 2 Foundations, an update will be available shortly, to include the formRow view helper.

Are you already using it? Have you found any issues with it? Share your thoughts in the comments


You might also be interested in these tutorials too...

Tue, Apr 8, 2014

Building and Executing SQL Queries In Zend

Whilst there are many ways for building and executing SQL queries in Zend Framework 2, the two that I usually use, and which are also used in the ZF2 manual, are closures and the selectWith function. I previously wrote a three part series, showing how to get started using the \Zend\Db\Sql classes with Zend Framework 2, but I didn’t cover how to actually run them. So in today’s tutorial, let’s do that.


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus