Reputation: 255
I'v creatated a custom validator:
class MyValidator extends AbstractValidator
{
const ERROR_CONST = 'error';
protected $dbAdapter;
protected $messageTemplates = array(
self::ERROR_CONST => "Error msg for '%value%'."
);
public function __construct($dbAdapter)
{
$this->dbAdapter = $dbAdapter;
}
public function isValid($value, $context = null)
{
$this->setValue($value);
/**
* Do validation against db
*/
if(/* Not valid */){
$this->error(self::ERROR_CONST);
return false;
}
return true;
}
}
The validation work, I've been able to test it. What doesn't work is the output of the error message using
echo $this->formElementErrors($form->get('action'));
All that is outputted is an empty UL. Is this a translator issue? When I do a get_class on $this->getTranslator() in the validator I get the validator class name. When I var_dump $this->getTranslator() it outputs null. Do I need to set a translator for this to work and where would be the best place to set that translator so that it's system wide for my own validators?
Upvotes: 2
Views: 445
Reputation: 73
Because you define a __construct
method for your validator class, the parent __construct
is not implicitly called:
http://php.net/manual/en/language.oop5.decon.php (see the note)
You should modify your __construct
method:
public function __construct($dbAdapter)
{
$this->dbAdapter = $dbAdapter;
//parent::__construct($options);
parent::__construct(null); // or (void)
}
As you can see, $messageTemplates
and $messageVariables
are "loaded" from AbstractValidator::__construct
, for being used in some methods ( error
included):
Upvotes: 2
Reputation: 1528
Maybe you forgot to add messageVariables ?
/**
* Message variables
* @var array
*/
protected $messageVariables = array(
'value' => 'value',
);
Upvotes: 0