小编PWa*_*sch的帖子

将ServiceLocator注入并使用到抽象的Zend\Form基类中

我创建了一个名为的抽象表单类AbstractApplicationForm.我希望通过Zend\ServiceManager\ServiceLocatorAwareInterface访问转换器来将服务定位器注入其中:

namespace Application\Form;

use Zend\Form\Form;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

abstract class AbstractApplicationForm 
    extends Form 
    implements ServiceLocatorAwareInterface
{
    protected $serviceLocator;
    protected $translator;

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }       

    public function getTranslator()
    {
        if (!$this->translator) {
            $this->translator = $this->getServiceLocator()->get('translator');
        }

        return $this->translator;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的申请表扩展了这个类,如下所示:

namespace Trade\Form;

use Zend\Captcha;
use Zend\Captcha\Image;
use Zend\Form\Element;
use Application\Form\AbstractApplicationForm;

class MemberForm extends AbstractApplicationForm
{
    public function init()
    {
        $this->setAttribute('method', 'post');

        // Add …
Run Code Online (Sandbox Code Playgroud)

zend-form zend-framework2

1
推荐指数
1
解决办法
2331
查看次数

标签 统计

zend-form ×1

zend-framework2 ×1