ZF2 transalation在表单类中不起作用

时间:2016-05-11 08:13:28

标签: validation zend-framework2 translation

我正在使用zendframework 2并且我的翻译在表单类中没有形成,表单形成并且有验证,在整个应用程序的其他地方它们正常工作。

我已将命名空间中的所有代码粘贴到我的文件中。

<?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); 
        }

这是我的控制器功能。 public function profileAction(){         $这 - &GT;布局( 'Ajax的布局');

    $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;
}

2 个答案:

答案 0 :(得分:0)

这是因为您的验证员而发生的 当你在没有服务定位器的情况下调用新的验证器时,我已经讨论了这个问题: https://stackoverflow.com/a/36500438/3333246

要解决此问题,您需要在选项中设置翻译器,因为:

class Size extends AbstractValidator

abstract class AbstractValidator implements
    Translator\TranslatorAwareInterface,
    ValidatorInterface

如果您在没有TranslatorAwareInterface的情况下实例化新Validator,则ServiceLocator未初始化。

因此,您的验证器需要在您的代码中声明如下所示的选项:

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

现在无需翻译消息,验证器会为您翻译。

对于我的评论,关于你的代码,没关系它与你的问题无关。事实上,这只是个人的。

编辑:

您不需要此翻译器:

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

但是这个

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

答案 1 :(得分:0)

我找到了另一种方法来完成这项工作,我已经做了一个ajax调用,并且在其响应中我显示了具有翻译的div。