Esempio n. 1
0
    public static function errors()
    {
        return static::$errors;
    }
}
Event::on('core.check.init', function () {
    Check::method(['required' => function ($value) {
        return is_numeric($value) && $value == 0 || empty($value) ? 'This value cant\' be empty.' : true;
    }, 'alphanumeric' => function ($value) {
        return preg_match('/^\\w+$/', $value) ? true : 'Value must be alphanumeric.';
    }, 'numeric' => function ($value) {
        return preg_match('/^\\d+$/', $value) ? true : 'Value must be numeric.';
    }, 'email' => function ($value) {
        return filter_var($value, FILTER_VALIDATE_EMAIL) ? true : 'This is not a valid email.';
    }, 'url' => function ($value) {
        return filter_var($value, FILTER_VALIDATE_URL) ? true : 'This is not a valid URL.';
    }, 'max' => function ($value, $max) {
        return $value <= $max ? true : 'Value must be less than ' . $max . '.';
    }, 'min' => function ($value, $min) {
        return $value >= $min ? true : 'Value must be greater than ' . $min . '.';
    }, 'range' => function ($value, $min, $max) {
        return $value >= $min && $value <= $max ? true : "This value must be in [{$min},{$max}] range.";
    }, 'words' => function ($value, $max) {
        return str_word_count($value) <= $max ? true : 'Too many words, max count is ' . $max . '.';
    }, 'length' => function ($value, $max) {
        return strlen($value) <= $max ? true : 'Too many characters, max count is ' . $max . '.';
    }, 'same_as' => function ($value, $fieldname) {
        $x = isset(Check::$data[$fieldname]) ? Check::$data[$fieldname] : '';
        return $value == $x ? true : 'Field must be equal to ' . $fieldname . '.';
    }]);
});
Esempio n. 2
0
 Check::method(['required' => ['validate' => function ($value) {
     return is_numeric($value) && $value == 0 || !empty($value);
 }, 'message' => "This value cannot be empty."], 'alphanumeric' => ['validate' => function ($value) {
     return (bool) preg_match('/^[0-9a-zA-Z]+$/', $value);
 }, 'message' => "Value must be alphanumeric."], 'numeric' => ['validate' => function ($value) {
     return (bool) preg_match('/^\\d+$/', $value);
 }, 'message' => "Value must be numeric."], 'email' => ['validate' => function ($value) {
     return (bool) filter_var($value, FILTER_VALIDATE_EMAIL);
 }, 'message' => "This is not a valid email."], 'url' => ['validate' => function ($value) {
     return (bool) filter_var($value, FILTER_VALIDATE_URL);
 }, 'message' => "This is not a valid URL."], 'max' => ['validate' => function ($value, $max) {
     return $value <= $max ? true : false;
 }, 'message' => "Value must be less than {{arg_1}}."], 'min' => ['validate' => function ($value, $min) {
     return $value >= $min;
 }, 'message' => "Value must be greater than {{arg_1}}."], 'range' => ['validate' => function ($value, $min, $max) {
     return $value >= $min && $value <= $max;
 }, 'message' => "This value must be in [{{arg_1}},{{arg_2}}] range."], 'words' => ['validate' => function ($value, $max) {
     return str_word_count($value) <= $max;
 }, 'message' => "Too many words, max count is {{arg_1}}."], 'length' => ['validate' => function ($value, $length) {
     return strlen($value) == $length;
 }, 'message' => "This value must be {{arg_1}} characters."], 'min_length' => ['validate' => function ($value, $min) {
     return strlen($value) >= $min;
 }, 'message' => "Too few characters, min count is {{arg_1}}."], 'max_length' => ['validate' => function ($value, $max) {
     return strlen($value) <= $max;
 }, 'message' => "Too many characters, max count is {{arg_1}}."], 'true' => ['validate' => function ($value) {
     return (bool) $value;
 }, 'message' => "This value must be true."], 'false' => ['validate' => function ($value) {
     return !$value;
 }, 'message' => "This value must be false."], 'same_as' => ['validate' => function ($value, $fieldname) {
     $x = isset(Check::$data[$fieldname]) ? Check::$data[$fieldname] : '';
     return $value == $x;
 }, 'message' => "Field must be equal to {{arg_1}}."], 'in_array' => ['validate' => function ($value, $array_values) {
     return in_array($value, $array_values);
 }, 'message' => "This value is forbidden."]]);