ZF2 - Error messages returned by Fieldset element remain untranslated

142 Views Asked by At

I am using ZF2 Fieldset for some of the form elements. But the issue I am facing is, I have an element of type Email in my fieldset file. Whenever this element is kept empty, the error message is returned, but its not the translated one. Instead it returns the Raw message.

SampleForm.php

use Zend\Form\Form;
use Zend\I18n\Translator\TranslatorAwareInterface;
use Zend\I18n\Translator\TranslatorAwareTrait;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterAwareTrait;
use SampleFieldset;

class SampleForm extends Form implements
    InputFilterAwareInterface,
    TranslatorAwareInterface
{
    use TranslatorAwareTrait;
    use InputFilterAwareTrait;

    /**
     * Prepare form elements
     * @param SampleFieldset $sampleFieldset
     */
    public function __construct(
        SampleFieldset $sampleFieldset,
    ) {
        parent::__construct('sampleform');

        $this->add(
            array(
                'attributes' => array(
                    'type' => 'textarea',
                    'id' => 'comments'
                ),
                'name' => 'comments',
                'options' => array(
                    'label' => 'Comments',
                ),
            )
        );

        $this->add($sampleFieldset);

        $this->add(
            [
                'name' => 'submit',
                'type' => 'Submit',
                'options' => [
                    'label' => 'Submit',
                ],
            ]
        );
    }
}

SampleFormFactory.php

use SampleForm;
use SampleFieldset;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\MutableCreationOptionsInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class SampleFormFactory implements FactoryInterface, MutableCreationOptionsInterface
{
    /** @var array */
    protected $options = [];

    /**
     * {@inheritDoc}
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $rootLocator = $serviceLocator->getServiceLocator();

        $form = new SampleForm(
            $serviceLocator->get(SampleFieldset::class),
        );

        return $form;
    }

    /**
     * {@inheritDoc}
     */
    public function setCreationOptions(array $options)
    {
        $this->options = $options;
    }
}

SampleFieldset.php

use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterAwareTrait;
use Zend\Validator\Hostname;

class SampleFieldset extends Fieldset implements InputFilterAwareInterface
{
    use InputFilterAwareTrait;

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct('sample');

        $this->add(
            [
                'type' => 'Zend\Form\Element\Email',
                'name' => 'email',
                'attributes' => [
                    'type' => 'email',
                    'id' => 'email'
                ],
                'options' => [
                    'label' => 'Email address',
                    'label_attributes' => [
                        'data-required' => 'true',
                        'class' => 'label_med',
                    ],
                ],
            ]
        );
    }
}

Now whenever the form is submitted, Since the email field is left empty, NotEmpty validated and message returned is Value is required and can't be empty. I want this messages to be returned on the translation form.

Note: If I keep the comment element empty and submit, then the error message returned is properly translated. But this is not the case for Email element.

0

There are 0 best solutions below