array */ public function verifyFormData($forms, $formData) { if (!$forms) { return ResultWrapper::success(); } foreach ($forms as $tmpForm) { $data = $formData[$tmpForm['name']]; if ($tmpForm['required']) { if (!$data) { $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']); return ResultWrapper::fail($errorMsg); } if ($tmpForm['rule']['size']) { if (mb_strlen($data) < $tmpForm['rule']['size']) { $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']); return ResultWrapper::fail($errorMsg); } } if ($tmpForm['rule']['regular']) { if (!preg_match($tmpForm['rule']['regular'], $data)) { $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']); return ResultWrapper::fail($errorMsg); } } } else { if (!$data) { continue; } if ($tmpForm['rule']['size']) { if (mb_strlen($data) < $tmpForm['rule']['size']) { $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']); return ResultWrapper::fail($errorMsg); } } if ($tmpForm['rule']['regular']) { if (!preg_match($tmpForm['rule']['regular'], $data)) { $errorMsg = $this->getErrorMsg($tmpForm['name'], $tmpForm['type']); return ResultWrapper::fail($errorMsg); } } } } return ResultWrapper::success(); } /** * * 获取表单验证的错误信息 * @param unknown $form_name * @param string $form_type * @return boolean|string */ public function getErrorMsg($form_name, $form_type = 'text') { if (!$form_name) { return false; } $errorMsg = ''; switch ($form_type) { case 'text': $errorMsg = "请填写有效{$form_name}"; break; case 'textarea': $errorMsg = "请填写有效{$form_name}"; break; case 'select': $errorMsg = "请选择{$form_name}"; break; default: $errorMsg = ""; break; } return $errorMsg; } }