How to add html5 attributes to Zend field?

Hi all

Been doing a small piece of work using the Zend Framework, basically setting up a small form. Finding it very hard to add attributes without throwing errors to the input.

$form->addElement('Text', 'surname')

The output

<div id="field_surname" class="form-group"><label class="required" for="surname">Surname:*</label>
<div class="input text">
<input type="text" class="form-control Text " value="" id="surname" name="surname"></div></div>

I’d like is to add the word ‘required’ inside the input, html5 attribute that sits alone.

<input type="text" class="form-control Text " value="" id="surname" name="surname" required>

I did try:


This adds class=“required”, I don’t need class just the word required.

Does anybody know how to do this?

Thanks, Barry

There is a tutorial on how to do it at

Cheers felgall.
I’ve already looked over this intensively before I came here. They don’t have any code/snippets showing what I’m after. Quite limited regarding code examples.

Everything seems to be key value pairs.


I’m also looking to add a select with options for a simple form select dropdown.


Thanks, Barry

On the page I linked to it gives the following example for setting “required”

// Make this element required:

Yes, this is one way of doing it server side, I’m already using:

$form->setRequired(array('first_name', 'surname', 'email'));

The idea is to add the html5 attributes so I don’t need to submit the form unless the inputs are valid, I’m trying to add further validation client side, the php zend required is the fallback.

As mentioned above, I just need a simple way of adding ‘required’ to the end:

<input type="text" class="form-control Text " id="surname" required>

Just can’t find any information on this, I thought it would be fairly straight forward.
Any ideas?

And have you came accross select > options?

Thanks, Barry

Presumably that code will also take care of the client side update once HTML 5 becomes a standard - assuming that “required” makes it into the standard (given that the HTML5 pattern attribute makes the required attribute completely unnecessary and in fact allows conflicting instructions to be entered).

Exactly felgall. Though, whether it does or not, I still need a way to add this/these attribute/s today :smile:

Any suggestions?

Thanks Barry

Well since the way you are generating the HTML currently doesn’t support it the only option would be to use a different way to generate the HTML that does support it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.