Skip to content

Instantly share code, notes, and snippets.

@fragsalat
Last active February 8, 2017 22:19
Show Gist options
  • Save fragsalat/146c58aa8a5fa7d49423 to your computer and use it in GitHub Desktop.
Save fragsalat/146c58aa8a5fa7d49423 to your computer and use it in GitHub Desktop.
This is a custom validator for phalcon framework to validate each entry inside a array
<?php
namespace greatshot\system\validation\validator;
use Phalcon\Validation;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;
/**
* This class aims to apply a validator to a list of values
*
* Usage: new ArrayValidator([
* 'message' => 'The given value is not a list',
* 'validator' => new Email(['message' => 'Invalid email'])
* ])
*
* @package greatshot\system\validation\validator
*/
class ArrayValidator extends Validator implements ValidatorInterface {
/**
* Executes the validation
*
* @param mixed $validation
* @param string $attribute
* @return bool
*/
public function validate(Validation $validation, $attribute) {
$list = $validation->getValue($attribute);
$validator = $this->getOption('validator');
$message = $this->getOption('notAnArrayMessage') ?: '';
// Check if the value is empty or is not an array or doesn't contain id or name
if (empty($list) || !is_array($list)) {
$validation->appendMessage(new Message($message, $attribute, 'array'));
return false;
}
// Add validator for each list entry
$listValidation = new Validation();
foreach ($list as $index => $value) {
$listValidation->add($index, $validator);
}
// Validate the list
$messages = $listValidation->validate($list);
if (count($messages)) {
// Add error messages to parent validation
foreach ($messages as $message) {
$message->setField($attribute . '[' . $message->getField() . ']');
$validation->appendMessage($message);
}
return false;
}
return true;
}
}
@jesus-santos
Copy link

Usage: $this->validator->add("field_name", new ArrayValidator([
"message" => "Value is not an Array",
"validator" => new PresenceOf([
"message" => "Field Name is missing"
])
]));

public function validate($validation, $attribute) {
    $list = $validation->getValue($attribute);
    $validator = $this->getOption('validator');
    $message = $this->getOption('notAnArrayMessage') ?: '';
    if (empty($list) || !is_array($list)) {
        $validation->appendMessage(new Message($message, $attribute, "array"));
        return false;
    }
    // Add validator for each $list element
    $listValidation = new Validation();
    foreach ($list as $index => $value) {
        $listValidation->add($index, $validator);
    }

    //Validate $list
    $messages = $listValidation->validate($list);
    if (count($messages)) {
        foreach ($messages as $message) {
            $message->setField($attribute . '[' . $message->getField() . ']');
            $validation->appendMessage($message);
        }
        return false;
    }
    return true;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment