0
我試圖從我的控制器添加一個驗證,如下所示。但它始終顯示此如何從控制器在zend框架2中添加表單驗證2
if ($request->getPost('ownerType') == "Company") {
$form->getInputFilter()->get('companyName')->getValidatorChain()->addValidator('required');
}
顯示錯誤。我與下面的錯誤混淆。
Catchable fatal error: Argument 1 passed to Zend\Validator\ValidatorChain::addValidator() must implement interface Zend\Validator\ValidatorInterface, string given, called in E:\xampp\htdocs\hossbrag\module\WebApp\src\WebApp\Controller\JobController.php on line 177 and defined in E:\xampp\htdocs\hossbrag\vendor\zendframework\zendframework\library\Zend\Validator\ValidatorChain.php on line 100
我的控制器是這裏
<?php
namespace WebApp\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use WebApp\Entity\User;
use Zend\View\Model\JsonModel;
use vendor\mpdf\mpdf;
class JobController extends AuthenticatedController
{
public function createAction()
{
$form = new \WebApp\Form\JobpostingForm();
$form->get('companyName')->setValueOptions($company);
$checkAgreement = true;
$request = $this->getRequest();
if ($request->getPost('ownerType') == "Company") {
$form->getInputFilter()->get('companyName')->getValidatorChain()->addValidator('required');
}
}
}
我應該在我的控制器更改爲得到合適的解決方案。
我可以從這裏找到一個解決方案.. http://stackoverflow.com/questions/15782107/zend-framework-2-removed-form-element-causes -validation到不能倒。這行是我的解決方案$ form-> setValidationGroup('name','email','subject','message');你把所有的驗證都放到了實體中,並且你驗證了哪一個可以提到上面的代碼。 – 2013-04-24 06:07:02