Codeigniter 4 - 表单验证
Posted
技术标签:
【中文标题】Codeigniter 4 - 表单验证【英文标题】:Codeigniter 4 - Form Validation 【发布时间】:2021-03-10 06:01:52 【问题描述】:我正在使用 CodeIgniter 4 的验证表单,我已经检查了帖子是否来了,但我每次都收到 FALSE,看看我的代码。
function login()
helper(['form', 'url']);
$validation = \Config\Services::validation();
$validation->setRules([
'email' => ['label' => 'E-mail', 'rules' => 'required'],
'password' => ['label' => 'Senha', 'rules' => 'required|min_length[6]']
]);
var_dump($this->validate($validation) ? true : false);exit;
我也试过了,
$rules = [
'email' => ['label' => 'E-mail', 'rules' => 'required'],
'password' => ['label' => 'Senha', 'rules' => 'required|min_length[6]']
];
var_dump($this->validate($rules) ? true : false);exit;
【问题讨论】:
你有没有尝试过一次在codeigniter.com/user_guide/libraries/… 上学习教程? 我做到了,兄弟... 【参考方案1】:看看我的代码
<?php
namespace Modules\Common\Controllers;
use Modules\Common\Config\Services;
use Modules\Common\Entities\AdvertisementEntity;
use CodeIgniter\HTTP\ResponseInterface;
use Modules\Shared\Controllers\ApiController;
class Advertisement extends ApiController
/**
* index function
* @method : GET
*/
public function index()
$advertisementEntity = new AdvertisementEntity();
$this->urlQueryParam->dataMap($advertisementEntity->getDataMap());
$advertisementService = Services::advertisementService();
$findAllData = $advertisementService->index($this->urlQueryParam);
return $this->respond([
'data' => $findAllData['data'],
'pager' => $findAllData['pager']
], ResponseInterface::HTTP_OK, lang('Shared.api.receive'));
/**
* show function
* @method : GET with params ID
*/
public function show($id = null)
$advertisementService = Services::advertisementService();
$findOneData = $advertisementService->show($id);
return $this->respond([
'data' => $findOneData['data'],
'pager' => $findOneData['pager']
], ResponseInterface::HTTP_OK, lang('Shared.api.receive'));
/**
* create function
* @method : POST
*/
public function create()
$rules = [
'name' => 'required|min_length[3]|max_length[255]',
'link' => 'required',
];
if (!$this->validate($rules))
return $this->respond([
'error' => $this->validator->getErrors(),
], ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Shared.api.validation'));
;
$advertisementEntity = new AdvertisementEntity((array)$this->request->getVar());
$advertisementEntity->enableStatus()->createdAt();
$advertisementService = Services::advertisementService();
$advertisementService->create($advertisementEntity);
return $this->respond([
'insertId' => $advertisementService->getInsertID()
], ResponseInterface::HTTP_CREATED, lang('Shared.api.save'));
/**
* update function
* @method : PUT or PATCH
*/
public function update($id = null)
if ($this->request)
//get request from Vue Js
$json = $this->request->getJSON();
if (!isset($id))
$id = $json->id;
$rules = [
'name' => 'required|min_length[3]|max_length[255]',
'link' => 'required',
];
if (!$this->validate($rules))
return $this->respond([
'error' => $this->validator->getErrors(),
], ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Shared.api.validation'));
$advertisementEntity = new AdvertisementEntity((array)$this->request->getVar());
$advertisementEntity->updatedAt();
$advertisementService = Services::advertisementService();
$advertisementService->update($id, $advertisementEntity);
return $this->respond([
], ResponseInterface::HTTP_OK, lang('Shared.api.update'));
/**
* edit function
* @method : DELETE with params ID
*/
public function delete($id = null)
$advertisementService = Services::advertisementService();
$advertisementService->delete($id);
return $this->respond([
], ResponseInterface::HTTP_OK, lang('Shared.api.remove'));
【讨论】:
以上是关于Codeigniter 4 - 表单验证的主要内容,如果未能解决你的问题,请参考以下文章