'Laminas / ZF3: Add manually Error to a field

is it possible to add manually an Error Message to a Field after Field Validation and Input Filter ?

I would need it in case the Username and Password is wrong, to mark these Fields / Display the Error Messages.

obviously in ZF/ZF2 it was possible with $form->getElement('password')->addErrorMessage('The Entered Password is not Correct'); - but this doesnt work anymore in ZF3/Laminas



Solution 1:[1]

Without knowing how you do your validation (there are a few methods, actually), the cleanest solution is to set the error message while creating the inputFilter (and not to set it to the element after it has been added to the form).

Keep in mind that form configuration (elements, hydrators, filters, validators, messages) should be set on form creation and not in its usage.

Here the form is extended (with its inputfilter), as shown in the documentation:

use Laminas\Form\Form;
use Laminas\Form\Element;
use Laminas\InputFilter\InputFilterProviderInterface;
use Laminas\Validator\NotEmpty;

class Password extends Form implements InputFilterProviderInterface {

    public function __construct($name = null, $options = []) {
        parent::__construct($name, $options);
    }

    public function init() {
        parent::init();

        $this->add([
            'name' => 'password',
            'type' => Element\Password::class,
            'options' => [
                'label' => 'Password',
            ]
        ]);
    }

    public function getInputFilterSpecification() {
        $inputFilter[] = [
            'name' => 'password',
            'required' => true,
            'validators' => [
                [
                    'name' => NotEmpty::class,
                    'options' => [
                        // Here you define your custom messages
                        'messages' => [
                            // You must specify which validator error messageyou are overriding
                            NotEmpty::IS_EMPTY => 'Hey, you forgot to type your password!'
                        ]
                    ]
                ]
            ]
        ];
        return $inputFilter;
    }
}

There are other way to create the form, but the solution is the same.
I also suggest you to take a look at the laminas-validator's documentation, you'll find a lot of useful informations

Solution 2:[2]

The Laminas\Form\Element class has a method named setMessages() which expects an array as parameter, for example

$form->get('password')
   ->setMessages(['The Entered Password is not Correct']);

Note that this will clear all error messages your element may already have. If you want to add your messages as in the old addErrorMessage() method you can do like so:

$myMessages = [
   'The Entered Password is not Correct',
   '..maybe a 2nd custom message'
];
$allMessages = array_merge(
   $form->get('password')->getMessages(),
   $myMessages);

$form
  ->get('password')
  ->setMessages($allMessages);

You can also use the error-template-name Laminas uses for its error messages as key in your messages-array to override a specific error message:

$myMessages = [
   'notSame' => 'The Entered Password is not Correct'
];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ermenegildo
Solution 2