hundredminds / php-validation
PHP 5.3 类,用于简单的表单验证,由 blackbelt/php-validation 分支而来
Requires
- php: >=5.3
This package is not auto-updated.
Last update: 2024-09-24 02:54:24 UTC
README
此类遵循 Zend Framework 命名规范,易于作为 Zend_Validation 的替代品直接使用。如果您在项目中不希望使用笨重的 Zend_Form,可以选择使用此工具进行快速且无痛苦的表单验证。
快速示例
以下示例展示了如何使用自定义异常抛出验证异常。然后可以从调用方法中检索错误消息。在控制器中验证数据不是良好的实践,这应该在模型中处理。这只是一个快速示例。
<?php class ExampleController extends Zend_Controller_Action { /** * Your controller action that handles validation errors, as you would * want these errors passed on to the view. * * @access public * @return void */ public function indexAction() { try { // validate the data $validData = $this->_validate($_POST); // validation passed because no exception was thrown // ... to something with the $validData ... } catch (Validator_Exception $e) { // retrieve the overall error message to display $message = $e->getMessage(); // retrieve all of the errors $errors = $e->getErrors(); // the below code is specific to ZF $this->_helper->FlashMessenger(array('error' => $message)); $this->_helper->layout->getView()->errors = $errors; } } /** * Your user-defined validation handling. The exception section is * very important and should always be used. * * @access private * @param array $post * @return mixed */ private function _validate(array $post = array()) { $validator = new Validator($post); $validator ->required('You must supply a name.') ->validate('name', 'Name'); $validator ->required('You must supply an email address.') ->email('You must supply a valid email address') ->validate('email', 'Email'); // check for errors if ($validator->hasErrors()) { throw new Validator_Exception( 'There were errors in your form.', $validator->getAllErrors() ); } return $validator->getValidData(); } }
可用的验证方法
- required($message = null) - 字段值是必需的。
- email($message = null) - 字段值必须是一个有效的电子邮件地址字符串。
- float($message = null) - 字段值必须是一个浮点数。
- integer($message = null) - 字段值必须是一个整数。
- digits($message = null) - 字段值必须是一个数字(无上界的整数)。
- min($limit, $include = TRUE, $message = null) - 字段值必须大于 $limit(数值)。$include 定义值是否可以等于限制。
- max($limit, $include = TRUE, $message = null) - 字段值必须小于 $limit(数值)。$include 定义值是否可以等于限制。
- between($min, $max, $include = TRUE, $message = null) - 字段值必须在 $min 和 $max 之间(数值)。$include 定义值是否可以等于 $min 和 $max。
- minLength($length, $message = null) - 字段值必须大于或等于 $length 个字符。
- maxLength($length, $message = null) - 字段值必须小于或等于 $length 个字符。
- length($length, $message = null) - 字段必须恰好 $length 个字符长。
- matches($field, $label, $message = null) - 一个字段与另一个字段匹配(例如密码匹配)
- notMatches($field, $label, $message = null) - 字段值必须不匹配 $field 的值。
- startsWith($sub, $message = null) - 字段必须以字符串 $sub 开头。
- notStartsWith($sub, $message = null) - 字段必须不以字符串 $sub 开头。
- endsWith($sub, $message = null) - 字段必须以字符串 $sub 结尾。
- notEndsWith($sub, $message = null) - 字段必须不以字符串 $sub 结尾。
- ip($message = null) - 字段值是一个有效的 IP,使用 filter_var 确定。
- url($message = null) - 字段值是一个有效的 URL,使用 filter_var 确定。
- date($message = null) - 字段值是一个有效的日期,可以是 DateTime() 接受的任何格式。
- minDate($date, $format, $message = null) - 日期必须大于 $date。$format 必须是页面上的格式之一 https://php.ac.cn/manual/en/datetime.createfromformat.php
- maxDate($date, $format, $message = null) - 日期必须小于 $date。$format 必须是页面上的格式之一 https://php.ac.cn/manual/en/datetime.createfromformat.php
- ccnum($message = null) - 字段值必须是一个有效的信用卡号码。
- oneOf($allowed, $message = null) - 字段值必须是$allowed值之一。$allowed可以是数组或以逗号分隔的值列表。如果以逗号分隔,除非有意用于匹配,否则不要包括空格。
- callback($callback, $message = '', $params = array()) - 定义您自己的自定义回调验证函数。$callback必须通过is_callable()检查。$params可以是任何值,或者如果必须传递多个参数,则是一个数组。
回调示例
回调函数可以以字符串或闭包的形式传递。
// numeric example
$validadator
->callback('is_numeric', 'Field is not numeric.')
->validate('number_field');
// closure example
$validator
->callback(function($val) {
return $val < -1 || $val > 1;
}, 'Number must be less than -1 or greater than 1.')
->validate('number_field_2');
验证数组和数组索引
此验证类已被扩展,允许验证数组以及多维数组的嵌套索引。
验证特定索引
要验证数组的特定索引,请使用点表示法,即。
<?php // load the validator $validator = new Validator($_POST); // ensure $_POST['field']['nested'] exists $validator ->required('The nested field is required.') ->validate('field.nested'); // ensure we have the first two numeric indices of $_POST['links'][] $validator ->required('This field is required') ->validate('links.0'); $validator ->required('This field is required') ->validate('links.1');
可用的预验证过滤
您可以对数据应用预验证过滤器(例如trim、strip_tags、htmlentities)。这些过滤器也可以自定义,只要它们通过is_callable()检查即可。
- filter($callback)
过滤示例
// standard php filter for valid user ids. $validator ->filter('intval') ->min(1) ->validate('user_id'); // custom filter $validator ->filter(function($val) { // bogus formatting of the field $val = rtrim($val, '/'); $val .= '_custom_formatted'; return $val; }) ->validate('field_to_be_formatted');
致谢
- 修改者:Corey Ballou、Chris Gutierrez和Robert Fruchtman。
- 源自Tasos Bekos,其基于"Bretticus"的初始工作。
- 请参阅http://brettic.us/2010/06/18/form-validation-class-using-php-5-3/以获取原始内容。
许可证
版权所有(c)2012 http://github.com/bekos,http://github.com/blackbe.lt
特此授予任何获得此软件及其相关文档副本(“软件”)的人免费使用权,不受限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件的副本,并允许向提供软件的人授予这样做,前提是符合以下条件
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的还是隐含的,包括但不限于适销性、适用于特定目的和非侵权的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他方式,源于、因或与软件或其使用或其他方式相关。