/** * @param HttpRequest $request */ public function index(HttpRequest $request) { $filterMap = array('title' => array(Filter::DFILTER_STRING, array(6, 12), Filter::DFILTER_SANITIZE_TRIM, array("require" => "标题不能为空.", "length" => "标题长度必需在6-12之间.")), 'email' => array(Filter::DFILTER_EMAIL, NULL, NULL, array("type" => "请输入正确的邮箱地址")), 'mobile' => array(Filter::DFILTER_MOBILE, NULL, NULL, array("type" => "请输入正确的手机号码")), 'id_number' => array(Filter::DFILTER_IDENTIRY, NULL, NULL, array('type' => '请输入正确的身份证号码')), 'content' => array(Filter::DFILTER_STRING, NULL, Filter::DFILTER_MAGIC_QUOTES | Filter::DFILTER_SANITIZE_HTML, array("require" => "文章内容不能为空."))); $data = array('title' => 'xiaoyang333', 'email' => '*****@*****.**', 'mobile' => '185456701250', 'id_number' => '431028198801210838', 'content' => "<span>我有一头'小毛驴'。</span>"); $data = Filter::loadFromModel($data, $filterMap, $error); __print($data); __print($error); die; }
/** * @param HttpRequest $request */ public function index(HttpRequest $request) { /** * * herosphp提供了Filter工具类来实现model的数据过滤和验证, 该类在 herosphp.filter包路径下,该类的数据过滤的算法分三步, 第一步: 数据的类型验证,判断是数字,字符,邮箱,url,身份证号码等 第二步:数据长度的验证, 判断数据的长度是否合格 第三部:数据的净化, 对数据进行净化,如去空格,转移html代码,去除SQL注入信息等 Filter类的验证的组合方式是采用位运算实现的,而不是数组,这样更节省内存。 //验证规则配置,每个待验证的字段都要配置三个值:数据类型,数据长度,数据的净化 //如果设置为NULL则此项不验证 * * */ $filterMap = array('title' => array(DFILTER_STRING, array(6, 12), DFILTER_SANITIZE_TRIM, '标题'), 'email' => array(DFILTER_EMAIL, array(6, 12), NULL, '邮箱'), 'content' => array(DFILTER_STRING, NULL, DFILTER_SANITIZE_HTML | DFILTER_MAGIC_QUOTES, '内容'), 'info' => array(DFILTER_STRING, NULL, DFILTER_SANITIZE_HTML | DFILTER_MAGIC_QUOTES | DFILTER_SANITIZE_SCRIPT, '说明')); $data = array('title' => 'xiaa', 'email' => '*****@*****.**', 'content' => " <span>我有一头'小毛驴'。</span>", 'info' => "sssss <script>alert('s');</script>"); $data = Filter::loadFromModel($data, $filterMap, $error2); __print($data); __print($error2); exit; }
/** * 获取过滤后的数据 * @param $data * @return mixed */ protected function loadFilterData(&$data) { $filterMap = $this->getFilterMap(); if (empty($filterMap)) { return $data; } $error = null; $_data = Filter::loadFromModel($data, $filterMap, $error); if ($_data == false) { WebApplication::getInstance()->getAppError()->setCode(1); WebApplication::getInstance()->getAppError()->setMessage($error); } return $_data; }
/** * 获取过滤后的数据 * @param $data * @return mixed */ protected function loadFilterData(&$data) { $filterMap = $this->getFilterMap(); if (empty($filterMap)) { return $data; } $error = null; $_data = Filter::loadFromModel($data, $filterMap, $error); if ($_data == false) { //如果开启了事物操作,则先回滚 if ($this->inTransaction()) { $this->rollback(); } AjaxResult::ajaxResult('error', $error); } return $_data; }