Issues

ZF-1292: Make FormElement to also accept $value as an array.

Description

This is just an idea, so I'm not proposing any code yet until you consider it valuable :-) I think it would be useful to be able to pass an array as the $value argument to any form helper because it will automate the process to generate universal forms for adding/editing records e.g


$post = $this->_request->getPost();

$this->formText("first_name", $post);

This code would generate a blank field if $post is null or insert the $post['first_name'] value if it's available. It's a common practice (at least for me) to have the same xhtml form for adding and editing certain records. Therefore, the ability to pass an array as $value, which then would lookup the element that matches $name, would definitely simplify creation of such forms. Please let me know what you think about this and I'll write the code if you agree :-)

Comments

Attached is a patch against SVN trunk which enables $value argument to be an array from which it attempts to extract data for the value XHTML attribute. The $value array must contain a key matching $name and, if it does not, blank ('') value is used instead. This patch doesn't change behavior of existing API and it just adds the new functionality. I have used it for a few weeks with one of my projects and it didn't cause any problems.

I've scheduled this for the 1.1.0 release, which will include a number of other view helper changes as well.

I've just re-read the issue, and I don't agree with the implementation; if you can pass an array containing the element name as a value, you can certainly pass the specific key in the element:


$this->formText('first_name', $post['first_name']);

If you're worried that the element name is not in the array, then you should pre-initialize the array, or use a ternary:


$this->formText('first_name', ((isset($post['first_name']) ? $post['first_name'] : ''));

Yes, it's more verbose, but it's also more explicit and doesn't lead to unexpected results.

Now, if you want support for array syntax in the element names, that's another matter.

Array notation works already, and solutions using existing functionality have been provided in comments.