2

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;
}
Bakhtawar GIll
  • 407
  • 5
  • 16
  • How you call this form in your controller ? – Greco Jonathan May 11 '16 at 12:39
  • From what I see you choose a way for build your forms that does'nt fit the common way to do it. I advise you to try a simpler form using some album samples to see what is different with your code. – Greco Jonathan May 12 '16 at 12:21
  • can u tell me the common way or some related help – Bakhtawar GIll May 12 '16 at 12:57
  • any feedback about my answer ? – Greco Jonathan May 13 '16 at 10:43
  • echo $translator->translate('MyAccountPictureErrorMessage1'); exit(); this is working right after i make $translator object but not in set message function, your answer give errors and ask me to apply abstract methods from that class. – Bakhtawar GIll May 13 '16 at 11:02
  • Interface 'Services\Form\TranslatorAwareInterface' not found . kindly please tell, how can i fix this, i guess if this gets fixed. problem will be solved. – Bakhtawar GIll May 13 '16 at 11:08
  • and please tell me, do i have to add in some kind of namespaces to make these interfaces work. – Bakhtawar GIll May 13 '16 at 11:13
  • Catchable fatal error: Argument 1 passed to Zend\Validator\AbstractValidator::setTranslator() must implement interface Zend\Validator\Translator\TranslatorInterface, instance of Zend\I18n\Translator\Translator given, called in E:\xampp\htdocs\xnspy-web-app\xnspy-cp\vendor\ZF2\Validator\AbstractValidator.php on line 139 and defined in E:\xampp\htdocs\xnspy-web-app\xnspy-cp\vendor\ZF2\Validator\AbstractValidator.php on line 400 – Bakhtawar GIll May 13 '16 at 12:05
  • what is this error and how to solve? – Bakhtawar GIll May 13 '16 at 12:05
  • And TranslatorAwareInterface is not initialized if you instanciate a new Validator without ServiceLocator. how to initialize this? – Bakhtawar GIll May 13 '16 at 12:16
  • what i understand is there is some problem with size class in validator because...... $validator= new \Zend\Validator\StringLength(array('min'=>6, 'max' => 20)); $validator->setMessage($translator->translate('MyAccountErrorMessage3')); $password->getValidatorChain()->attach($validator,true); this is working fine with same declarations. – Bakhtawar GIll May 18 '16 at 08:47

2 Answers2

0

This is happening because of your validator.
I already talked about this problem, when you call new validators without the service locator : https://stackoverflow.com/a/36500438/3333246

To fix that you need to set the translator in your options because:

class Size extends AbstractValidator

abstract class AbstractValidator implements
    Translator\TranslatorAwareInterface,
    ValidatorInterface

And TranslatorAwareInterface is not initialized if you instanciate a new Validator without ServiceLocator.

So your validators need options declared like this in your code:

$validator = new \Zend\Validator\File\Size(array('translator' => $translator, 'max' => 250*1024));
            $validator->setMessage('MyAccountPictureErrorMessage1');

No need to translate the message now, the validator will translate it for you.

For my comment, about your code, nevermind it's not related to your problem. It's just personal in fact.

EDIT:

You don't need this translator :

$translator = $this->getServiceLocator()->getServiceLocator()->get('viewHelperManager')->get('translate')->getTranslator();

But this one

$translator = $this->getServiceLocator()->get('translator');
Community
  • 1
  • 1
Greco Jonathan
  • 2,517
  • 2
  • 29
  • 54
0

I have found another way to do this job, i have made an ajax call and on its response i show the divs having the translations.

Bakhtawar GIll
  • 407
  • 5
  • 16