【例子介绍】GUMP is a standalone PHP data validation and filtering class that makes validating any data easy and painless without the reliance on a framework
【相关图片】
【源码结构】
$is_valid = GUMP::is_valid(array_merge($_POST, $_FILES), [ 'username' => 'required|alpha_numeric', 'password' => 'required|between_len,4;100', 'avatar' => 'required_file|extension,png;jpg', 'tags' => 'required|alpha_numeric', // ['value1', 'value3'] 'person.name' => 'required', // ['person' => ['name' => 'value']] 'persons.*.age' => 'required' // ['persons' => [ // ['name' => 'value1', 'age' => 20], // ['name' => 'value2'] // ]] ]); // 1st array is rules definition, 2nd is field-rule specific error messages (optional) $is_valid = GUMP::is_valid(array_merge($_POST, $_FILES), [ 'username' => ['required', 'alpha_numeric'], 'password' => ['required', 'between_len' => [6, 100]], 'avatar' => ['required_file', 'extension' => ['png', 'jpg']] ], [ 'username' => ['required' => 'Fill the Username field please.'], 'password' => ['between_len' => '{field} must be between {param[0]} and {param[1]} characters.'], 'avatar' => ['extension' => 'Valid extensions for avatar are: {param}'] // "png, jpg" ]); if ($is_valid === true) { // continue } else { var_dump($is_valid); // array of error messages }
评论