shadowzen
shadowzen

Reputation: 77

How to apply classes to labels using Zend\Form and add line endings within the rendered form?

I've followed the tutorial for Zend Framework 2 and I see on the rendered form that the convention for binding the label is to wrap the input element in the label tags as opposed to using the 'for' attribute. While the tutorial didn't cover it, I deduced (correctly) that the attributes array allows for setting a class on the input element itself but what I really want to do is apply a class onto the label defined under options.

Here's an example used in the tutorial (from AlbumForm.php):

$this->add(array(
    'name'       => 'title',
    'attributes' => array(
        'class' => 'required', // <- I added this
        'type' => 'text',
    ),
    'options' => array(
        'label' => 'Title',
    ),
));

That will render as this:

<label><span>Title</span><input name="title" class="required" type="text" value=""></label>

Optionally, I could live with having the class applied to the span tag but I'd prefer to use the class on the label and then use css child selectors for the span and input elements

I've re-read the tutorial plus the comments on the appropriate section and even dug into the Zend\Form API documentation itself and I don't see how I could apply a class attribute to the label in declaring a form element in this manner.


Another minor nitpick with form rendering is that it appears to be rendered inline with no line breaks between form elements. I've gotten around this by adding a line break in the view scripts (add.phtml and edit.phtml) like so:

echo $this->formRow($form->get('title')) . "\n";

However, this seems like a pain to have to do with every echoed form statement within the view and using the formCollection() also renders the entire form output inline. For legibility purposes I'd like to at least have line breaks where appropriate when viewing the source (I'd wish for proper indentation, too, but that seems like a tall order since even IDE's get it wrong much of the time)

So, is there a built-in option for either of these concerns that I'm missing or, alternatively, a way to define a factory or helper? If I'd need to write a helper, I'd want it to apply to all modules.

Upvotes: 0

Views: 6401

Answers (1)

Rob Allen
Rob Allen

Reputation: 12778

To add a class to the label use the 'label_attributes' key in your configuration:

$this->add(array(
    'name'       => 'title',
    'attributes' => array(
        'type' => 'text',
    ),
    'options' => array(
        'label' => 'Title',
        'label_attributes' => array(
            'class' => 'required',
        ),
    ),
));

To add new lines, you can either write your own FormRow view helper or render the label and element separately within your view script like this:

$form = $this->form;
$form->prepare();
echo $this->form()->openTag($form) . "\n";

echo $this->formLabel($form->get('title')) . "\n";
echo $this->formInput($form->get('title')) . "\n";
echo $this->formElementErrors($form->get('title'));

// other elements
echo $this->form()->closeTag($form) . "\n";

Upvotes: 7

Related Questions