Simple Form Rendering with The FormRow Helper


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

About Matthew

Matthew Setter Matthew Setter is a PHP & Zend Framework specialist. If you're in need of a custom software application, need to migrate an existing legacy application, or want to know your current application's GPA - get in touch.

Want To Be A Zend Framework Guru?

Drop your email in the box below, and get awesome tutorialsjust like this one — straight to your inbox, PLUS exclusive content only available by email.