If you want to write your own validator
$ele = new Element/TextArea("json_submit");
$ele->addValidator(new JsonValidator(["message" => "contain valid json"]));
class JsonValidator extends Validator implements ValidatorInterface
{
function validate($validator, $field)
{
return json_decode($validator->getValue($field));
}
}
If you plan to use it in a model.
class MyModel extends \Model
{
public function validation()
{
$this->validate->(new JsonValidator(["field" => "myField", "message" => "contain valid json"]));
}
}
if you want to use it as a combined validation
class MyValidation extends Validation
{
public function initialize()
{
$this->add('name', new PresenceOf(array(
'message' => 'The name is required'
)));
$this->add('email', new PresenceOf(array(
'message' => 'The e-mail is required'
)));
$this->add('email', new Email(array(
'message' => 'The e-mail is not valid'
)));
}
}
$validation = new MyValidation();
$validation->validate($_POST);