0
votes

Usually I used Zend Form's messages in the following way:

Code in form:

$element = new Zend_Form_Element_Text('form_resource_type');
$validator = new Zend_Validate_NotEmpty();
$validator->setMessages(
     array('isEmpty' => 'Please choose type of resource')
);
$element->addValidator($validator);
$element->setRequired(true);
$this->addElement($element);

Code in view:

<?php foreach($subForm->getElementsAndSubFormsOrdered() as $element):?>
     <?php echo $element?>
     <?php foreach($element->getMessages() as    $errorMsg):?>
         <?php echo $this->escape($errorMsg);?>
    <?php endforeach;?>
<?php endforeach;?>

So, for outputting error messages I used getMessages() function. But right now under certain circumstances (in case of special combination of fields' values) I need to mark element as invalid and add custom error message. I tried to use addError($message) function, but it adds message to _errorMessages property, while getMessages output _messages Zend_Form_Element property.

I didn't find function of adding messages to the _messages property. How can I do this? Or I should not work with this property directly and change a way of outputting error messages in view?

UPD:

I use Zend Framework 1.12

2
$form->getElement('elementName')->addError('your message'); - Max Ivanov

2 Answers

0
votes

Since you are accessing the error messages from the form element. Then you can try to set message in the element by using the following statement in the controller:

$form->getElement('elementName')->addErrorMessage('custom Message');

You will then be able to print the message in your way.

0
votes

You can use markAsError() for marking an element as invalid Custom Error Messages
I think this will do the trick for you

if($error)
{
    $element->addErrorMessage('Custom Error');
    $element->markAsError();
}