gpt4 book ai didi

php - 验证总是返回消息 'required is not a valid rule' |代码点火器 4

转载 作者:行者123 更新时间:2023-12-04 07:43:39 26 4
gpt4 key购买 nike

我在 config/validation.php 中使用官方文档中的引用进行了验证 https://codeigniter4.github.io/userguide/libraries/validation.html像这样:

class Validation
{
....

public $user = [
'name' => [
'rules' => 'required'
],
'email' => [
'rules' => 'valid_email|required',
'errors' => [
'valid_email' => 'E-mail is not valid',
'required' => 'E-mail is required'
]
]
];
}
然后我像这样在我的 Controller 上调用它:
....

class User extends ResourceController
{
public function create()
{

$name = $this->request->getPost('name');
$email = $this->request->getPost('email');
$country = $this->request->getPost('country');
$province = $this->request->getPost('province');
$city = $this->request->getPost('city');
$day_of_birth = $this->request->getPost('day_of_birth');
$password = $this->request->getPost('password');
$phone_number = $this->request->getPost('phone_number');
$photo = $this->request->getPost('photo');

$data = [
'name' => $name,
'email' => $email,
'country' => $country,
'province' => $province,
'city' => $city,
'day_of_birth' => $day_of_birth,
'password' => $password,
'phone_number' => $phone_number,
'photo' => $photo
];

$validate = $this->validation->run($data,'user');
$errors = $this->validation->getErrors();

if($errors){
return $this->fail($errors);
}

return $this->respond($data);
}
}
当我使用 postman 测试它时,我得到这样的返回:
enter image description here
如果我在 Controller 中进行验证,则验证工作正常,但我想在validation.php 中声明验证,请有人帮助我,无论我在validation.php 中写什么,然后我都使用 $this->validation->run($data,'name') 调用总是返回相同的

最佳答案

您尚未在名称所需验证中添加错误消息,因此可能是问题所在,因此您必须向名称添加错误消息。
因为消息中的验证很重要。没有任何错误信息,用户怎么知道表单出了什么问题。
这是自定义函数或验证示例。改变你的功能就好了。

class Validation
{
public $user = [
'name' => [
'rules' => 'required',
'errors' => [
'required' => 'Name is required.'
]
],
'email' => [
'rules' => 'required|valid_email',
'errors' => [
'valid_email' => 'E-mail is not valid',
'required' => 'E-mail is required'
]
],
];
}

关于php - 验证总是返回消息 'required is not a valid rule' |代码点火器 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67313974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com