views:

801

answers:

2

When I create a html form like this:

$form = new Zend_Form();
$form->setMethod('post');
$form2->addElement('textarea', 'Name with Space');

The HTML becomes:

...
<textarea name="NamewithSpace" id="NamewithSpace" rows="24" cols="80"></textarea>
...

Mention that the input name becomes camelcase!

When I call $form->getValues(); after a post with filled textarea the result is:

array('Name with Space' => NULL); // Whitespace name! But value empty!

When I call $this->getRequest(); after a post with filled textarea the result is:

array('NamewithSpace' => 'filled in value'); // Camelcase name! Value filled, but name changed!

How can I access the filled in values with the setted name 'Name with Space'?

I'm using ZF 1.7.6.

+1  A: 

Unfortunately I don't think you can! For an application where you simply must have an element name that's not acceptable in ZF, you'll have to modify the ZF source.

In ZF 1.8.1 the regular expression you need to change to allow space (and any other characters) is in line 424 of Zend/Form/Element.php

One possible (better) solution would be to create a custom element and override the filterName method, however this isn't very practical if you wish to change several element types.

There must be a better solution surely?!

David Caunt
Sure ther must be a better soluten/behavior! Currently I change the values back from camelcase to whitespace separated valuse by$key = trim(preg_replace('~([A-Z]{1})([a-z]+)~', ' $1$2', $key));Not very nice, nor stable.
powtac
Indeed! Modifying the library seems to be the best way at the moment :( I wrote a quick shell script to make it a little easier when I upgrade ZF. It might be worth looking at the issue tracker on the ZF website for more info
David Caunt
A: 

The $form->getValues() will always show you the original keys and values that you set on the form object, as these aren't updated after the form is posted. However, you could use that to your advantage with something like:

$textarea = $form->getElement('Name with Space');
$key      = str_replace(' ', '', trim($textarea->getName()));

Using that key on the request object, should give you the access to the value you're after. It's a bit of a hack, but looks like it might work.

Kieran Hall