Reputation: 3148
In ZF1 it was possible to mark the form invalid using the code:
$form->fieldname->addError('error message');
How can I do it in ZF2? I tried
$form->get('elementName')->setMessages(array('error message'));
but it doesn't make the form invalid.
Upvotes: 3
Views: 727
Reputation: 1014
I wonder the same question and I dont know how to do it easy too with default Zend 2 forms.
I have no idea why it was necessary to hide manual form state manipulating and break obvious addError functionality too.
But may be it's appropriate to you to use proxy-way like this:
Create own form basic class (may be write it better later):
class BasicForm extends Form
{
protected _isValid = null;
public function isValid()
{
return isset($this->_isValid) ? $this->_isValid : parent::isValid();
}
public function setValid($value)
{
$this->_isValid = isset($value) ? (bool)$value : null;
return $this;
}
}
Instantiate your real forms from this custom form class intead of default Zend Form class:
class SomeYourForm extends BasicForm
...
So you will be able to set this form valid state to true or false by overlaying of this property.
May be it helps for someone too.
Upvotes: 1