0

I tried to add a validation from my controller like below. but it always shows this

 if ($request->getPost('ownerType') == "Company") {

        $form->getInputFilter()->get('companyName')->getValidatorChain()->addValidator('required');
    }

shows error. I confused with below error.

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

My controller is here

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

What should to change in my controller to get appropriate solution.

Md Mehedi Hasan
  • 1,733
  • 1
  • 21
  • 34
  • 1
    I could find a solution from here.. http://stackoverflow.com/questions/15782107/zend-framework-2-removed-form-element-causes-validation-to-fail. This line is my solution $form->setValidationGroup('name', 'email', 'subject', 'message'); You put all your validation into entity and which one you validate you can just mention above code. – Md Mehedi Hasan Apr 24 '13 at 06:07

1 Answers1

0

If you encounter such a clear error, simply check out the sources ;)

First one to check would be Zend\Validator\ValidatorInterface. The Error shows you, that a Class implementing this interface is excepted. Looking at the code you'll see, the function wants a Class, not just a string.

But since you're used to ZF a little it becomes clear that you know there's other ways to add stuff. So let's take a look at Zend\InputFilter\InputFilter#add(). You'll see that the first param of the add() function indeed asks for a class implementing ValidatorInterface. But it also accepts some other stuff:

/**
 * Add an input to the input filter
 *
 * @param  array|Traversable|InputInterface|InputFilterInterface $input
 * @param  null|string $name
 * @return InputFilter
 */
public function add($input, $name = null)
{
    //...
}

It also accepts array, Traversable, InputInterface and InputFilterInterface. So choices are there.

Now, i have never done this myself and i sincerely hope this works (if not i suck!), but assuming you're using the array-syntax, all you have to do is this:

[...]->getValidatorChain()->add(array(
    'type' => 'Zend\Validator\NotEmpty'
));

Let me know if this worked out for you ;)

Sam
  • 16,435
  • 6
  • 55
  • 89