php 验证所传参数为必填的时候的验证逻辑

此段代码摘自lumen框架: xx/vendor/illuminate/validation/Validator.php
/** * Validate that a required attribute exists. * * @param string $attribute * @param mixed $value * @return bool */ protected function validateRequired($attribute, $value) { if (is_null($value)) { return false; } elseif (is_string($value) && trim($value) === '') { return false; } elseif ((is_array($value) || $value instanceof Countable) && count($value) < 1) { return false; } elseif ($value instanceof File) { return (string) $value->getPath() != ''; } return true; }
原文地址:https://www.cnblogs.com/mingaixin/p/5354840.html