I am using zendframework 2 and My translations are not working here in form class where the form is formed and there is validation, elsewhere in whole applications they are working properly.
I have pasted all the code in my file with namespaces.
<?php
namespace Services\Form;
use Zend\Form\Form;
use Zend\Form\Element;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ProfilePicForm extends Form implements ServiceLocatorAwareInterface
{
protected $serviceLocator;
public function setServiceLocator(ServiceLocatorInterface $sl)
{
$this->serviceLocator = $sl;
}
public function getServiceLocator()
{
return $this->serviceLocator;
}
public function init()
{
$routeMatch = $this->getServiceLocator()->getServiceLocator()->get('Application')->getMvcEvent()->getRouteMatch();
$translator = $this->getServiceLocator()->getServiceLocator()->get('viewHelperManager')->get('translate')->getTranslator();
$action = $routeMatch->getParam('action');
// Form
parent::__construct('profile_pic_form');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype','multipart/form-data');
$profile_pic_form_csrf = new Element\Csrf('profile_pic_form_csrf');
$profile_pic_form_csrf->setCsrfValidatorOptions(array('timeout'=>'3600'));
$this->add($profile_pic_form_csrf);
$profile_pic = new Element\File('profile_pic');
$this->add($profile_pic);
// Validation
$inputFilter = new InputFilter();
$profile_pic = new Input('profile_pic');
$profile_pic->getFilterChain()
->attach(new \Lib\MyLib\Filter\RenameUpload(array(
'target' => SERVICE_PROFILE_PIC_UPLOAD_PATH.'/profile-pic.*',
'use_upload_extension' => true,
'randomize' => true
)));
$required = true;
$profile_pic->setRequired($required);
$validator = new \Zend\Validator\File\UploadFile();
$validator->setOptions(array(
'messageTemplates' => array(
\Zend\Validator\File\UploadFile::FILE_NOT_FOUND => 'Please select picture.'
)));
$profile_pic->getValidatorChain()->attach($validator,true);
$validator = new \Zend\Validator\File\Size(array('max' => 250*1024));
$validator->setMessage(**$translator->translate('MyAccountPictureErrorMessage1')**);
$profile_pic->getValidatorChain()->attach($validator,true);
$validator = new \Zend\Validator\File\Extension('png,jpg');
$validator->setMessage(**$translator->translate('MyAccountPictureErrorMessage2')**);
$profile_pic->getValidatorChain()->attach($validator,true);
$inputFilter->add($profile_pic);
$this->setInputFilter($inputFilter);
}
this is my controller function. public function profileAction() { $this->layout('ajax-layout');
$var = new \stdClass();
$viewmodel = new ViewModel();
$this->authPlugin()->checkLogin();
$this->servicePlugin()->checkSid();
$this->layout()->setVariable('allowedFeatures', $this->featurePlugin()->getAllowedFeatures());
$this->languagePlugin()->translate();
$var->userInfo = $this->authPlugin()->getUserInfo();
if($this->params()->fromRoute('sid') !== null){
$var->sid = $this->params()->fromRoute('sid');
}
elseif ($this->params()->fromRoute('id') !== null) {
$var->sid = $this->params()->fromRoute('id');
}
// ----------------------- i m here --------------------------
// $var->sid = $this->params()->fromRoute('sid');
$var->profilePicForm = $this->getServiceLocator()->get('FormElementManager')->get('\Services\Form\ProfilePicForm');
$var->serviceNameForm = $this->getServiceLocator()->get('FormElementManager')->get('\Services\Form\ServiceNameForm');
$var->service = $this->getServices()->fetchServiceById($var->sid);
// Fetch payment history
$var->paymentHistory = $this->getServiceLocator()->get('Services\Model\PaymentTransactionService')->fetchPaymentTransactionsByServiceId($var->sid);
$var->timezones = $this->getTimeZoneTable()->listAll();
$viewmodel->setVariables(array('var' => $var));
return $viewmodel;
}