Reputation: 19
How can I change the attributes of a input form?
I create with this a input (productform.php):
$this->add(array(
'name' => 'categoryId',
'attributes' => array(
'id' => 'categoryId',
'type' => 'hidden',
'value' => '',
),
));
In a previous page I link to the form and set the special value in the url (....com/form/3
).
In the indexcontroller.php I get the form with $form = new ProductForm();
and want edit the value and set the special value from the url.
My idea was the $form->setAttribute('categoryId', 'value');
but that not working.
Thanks.
indexcontroller.php
...
$form = new ProductForm();
$form->setHydrator(new CategoryHydrator());
$form->bind(new Product());
$form->setAttribute('categoryId', 'value');
....
productform.php
...
class ProductForm extends Form
{
public function __construct()
{
parent::__construct('productForm');
$this->setAttribute('action', 'newproduct');
$this->setAttribute('method', 'post');
$this->add(array(
........
Upvotes: 0
Views: 51
Reputation: 2673
$form->get('categoryId')->setValue("value");
Update
So if you just want to fill input, you mean placeholder
attribute in html. You can use setAttribute
method.
$form->get('categoryId')->setAttribute('placeholder', 'text to show');
Upvotes: 2
Reputation: 9857
The form view helper will not allow arbitrary HTML attributes to be set on the form. This is because it would result in invalid HTML.
If you take a look at Zend\Form\Helper\AbstractHelper
there are two properties $validGlobalAttributes
and $validTagAttributes
which define the allowed tags.
In the case of the form view helper (Zend\Form\View\Helper\Form)
The 'valid tag attributes' will be the method
, action
etc
As you require something custom (for JS possibly?); I would change it to a data-
attribute.
$form->setAttribute('data-categoryId', 'value');
The data-
is a valid HTML5 attribute which is useful for adding 'domain data' to HTML elements and is really the 'correct' way to do what you require.
Upvotes: 0