始终验证 return 消息 'required is not a valid rule' |代码点火器 4
Validation always return mesasge 'required is not a valid rule' | Codeigniter 4
我在配置/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'
]
]
];
}
然后我在我的控制器上这样调用它:
....
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);
}
}
当我使用邮递员对其进行测试时,我得到了这样的 return:
如果我在控制器中执行验证,验证工作正常,但我想在 validation.php 中声明验证,有人请帮助我,无论我在 validation.php 中写什么然后我调用使用 $this->validation->run($data,'name')
总是 return 相同
您没有在需要验证的名称中添加错误消息,所以可能是问题所在,因此您必须在名称中添加错误消息。
因为消息中的验证很重要。没有任何错误信息,用户怎么知道表单出了什么问题。
这里是自定义的函数或验证示例。改变你的功能就像。
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'
]
],
];
}
我在配置/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'
]
]
];
}
然后我在我的控制器上这样调用它:
....
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);
}
}
当我使用邮递员对其进行测试时,我得到了这样的 return:
如果我在控制器中执行验证,验证工作正常,但我想在 validation.php 中声明验证,有人请帮助我,无论我在 validation.php 中写什么然后我调用使用 $this->validation->run($data,'name')
总是 return 相同
您没有在需要验证的名称中添加错误消息,所以可能是问题所在,因此您必须在名称中添加错误消息。
因为消息中的验证很重要。没有任何错误信息,用户怎么知道表单出了什么问题。
这里是自定义的函数或验证示例。改变你的功能就像。
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'
]
],
];
}