Ejemplo n.º 1
0
        return true;
    }
    if (preg_match('/[^0-9 \\-]+/', $value)) {
        return false;
    }
    $value = preg_replace('/\\/D/', '', $value);
    $check = 0;
    $even = false;
    for ($n = strlen($value) - 1; $n >= 0; $n--) {
        $digit = intval($value[$n]);
        if ($even && ($digit *= 2) > 9) {
            $digit -= 9;
        }
        $check += $digit;
        $even = !$even;
    }
    return $check % 10 === 0;
});
Validator::addMethod('equalTo', function (ValidatorContext $context, $value, $param) {
    if ($context->optional($value)) {
        return true;
    }
    $valid = false;
    $parts = $context->parseSelector($param);
    if ($parts !== null) {
        $name = $parts['name'];
        $model = $context->getValidator()->getModel();
        $valid = $value === isset($model[$name]) ? $model[$name] : null;
    }
    return $valid;
});
Ejemplo n.º 2
0
<?php

require 'vendor/autoload.php';
use Cdmckay\Pajama\Validator;
use Cdmckay\Pajama\ValidatorContext;
Validator::addMethod('regex', function (ValidatorContext $context, $value, $param) {
    return $context->optional($value) || preg_match('/' . $param . '/', $value);
});
$rules = json_decode(file_get_contents(__DIR__ . '/rules.json'), true);
$response = array();
Validator::validate(array('model' => $_POST, 'rules' => $rules, 'validHandler' => function () use(&$response) {
    $response['successful'] = true;
}, 'invalidHandler' => function (Validator $validator) use(&$response) {
    $response['successful'] = false;
    $response['invalid_field_names'] = array_keys($validator->invalidFields());
}));
header('Content-Type: application/json');
echo json_encode($response);