Skip to content

Instantly share code, notes, and snippets.

@GeeH
Created September 18, 2012 09:28
Show Gist options
  • Save GeeH/3742237 to your computer and use it in GitHub Desktop.
Save GeeH/3742237 to your computer and use it in GitHub Desktop.
validator array zf2
public function create($data = null) {
$json = "{ \"artist\":\"ergsern\", \"title\":\"enruy7u\" }";
$array = \Zend\Json\Decoder::decode($json);
$artist = new Input('artist');
$artist->getValidatorChain()->addValidator(new Validator\StringLength(array('min' => '5', 'max' => '20')));
$title = new Input('title');
$title->getValidatorChain()->addValidator(new Validator\StringLength(array('min' => '5', 'max' => '20')));
$inputFilter = new InputFilter();
$inputFilter->add($artist);
$inputFilter->add($title);
$inputFilter->setData((array)$array);
if ($inputFilter->isValid()) {
echo "is valid\n";
} else {
echo "is not valid\n";
foreach ($inputFilter->getInvalidInput() as $error) {
print_r ($error->getMessages());
}
}
// var_dump($array);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment