My Love Affair With PHP - Or Why I've Never Loved Another Language So Much
There are many software languages to choose from today — but why PHP? Does it even rate a mention? Let’s think about this for a moment.
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.
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.
There’s not much more to it than this; but there are a few configuration options available. Let’s quickly step through them.
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.
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);
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);
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
There are many software languages to choose from today — but why PHP? Does it even rate a mention? Let’s think about this for a moment.
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.
Do you need to use different elements of a Zend Form, in multiple locations? Are you keen to reuse the same form class and avoid code duplication but don’t know how? Come learn about validation groups.
What does it take to be a PHP core contributor? What things do you have to do? Why would you do it? I’ve started on the journey. Here’s what I’m learning.
Please consider buying me a coffee. It really helps me to keep producing new tutorials.
Join the discussion
comments powered by Disqus