英文:
How to validate a form field based on the data from another one? - Laravel 6
问题
我有一个包含以下字段的表单:
- n_census(提供有关城镇或城市的总人口信息)
- n_participants(根据n_census提供选举过程中参与者的数量信息)
我想要根据n_census验证n_participants,因为不可能有比n_census更多的n_participants。
我用于验证这两个字段(以及其他与此问题不相关的字段)的代码如下:
<!-- language: php -->
protected function validator(array $data)
{
$customMessages = [
'required' => ':attribute 是必填项',
'min' => ':attribute 至少需要 :min 个字符',
'max' => ':attribute 最多可以有 :max 个字符',
'string' => ':attribute 必须是一个字符串',
'int' => ':attribute 必须是一个整数',
'email' => ':attribute 是一个电子邮件地址',
'confirmed' => '你未确认电子邮件'
];
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'dni' => ['required', 'string', 'min:9', 'max:9'],
'surname1' => ['required', 'string', 'max:50'],
'surname2' => ['required', 'string', 'max:50'],
'v1' => ['required', 'string', 'max: 100'],
'v2' => ['required', 'string', 'max: 100'],
'district' => ['required', 'int'],
'section' => ['required', 'int'],
'chair' => ['required', 'string'],
'municipality' => ['required'],
'province' => ['required'],
'n_census' => ['required', 'int'],
'n_participants' => ['required', 'int', 'max: 50']
], $customMessages);
}
是否可以使用Validator::make()
来实现这个目标?
英文:
I have a form which contains the following fields:
- n_census (provides information about the total population from a town or city)
- n_participants (provides information about the number of participants in an election process according to n_census)
I would like to validate n_participants according to n_census because it's not possible to have more n_participants than n_census.
The code I use to validate both fields (and others I have but not relevant for this issue) is:
<!-- language: php -->
protected function validator(array $data)
{
$customMessages = [
'required' => ':attribute es obligatori',
'min' => ':attribute cal que siga, com a mínim, :min caracters',
'max' => ':attribute cal que siga, com a màxim, :max caracters',
'string' => ':attribute cal que siga una cadena de caracters',
'int' => ':attribute cal que siga un enter',
'email' => ':attribute es un email',
'confirmed' => 'No has confirmat el correu electrònic'
];
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'dni' => ['required', 'string', 'min:9', 'max:9'],
'surname1' => ['required', 'string', 'max:50'],
'surname2' => ['required', 'string', 'max:50'],
'v1' => ['required', 'string', 'max: 100'],
'v2' => ['required', 'string', 'max: 100'],
'district' => ['required', 'int'],
'section' => ['required', 'int'],
'chair' => ['required', 'string'],
'municipality' => ['required'],
'province' => ['required'],
'n_census' => ['required', 'int'],
'n_participants' => ['required', 'int', 'max: 50']
], $customMessages);
}
Is it possible to use Validator::make()
to do that?
答案1
得分: 2
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'dni' => ['required', 'string', 'min:9', 'max:9'],
'surname1' => ['required', 'string', 'max:50'],
'surname2' => ['required', 'string', 'max:50'],
'v1' => ['required', 'string', 'max:100'],
'v2' => ['required', 'string', 'max:100'],
'district' => ['required', 'int'],
'section' => ['required', 'int'],
'chair' => ['required', 'string'],
'municipality' => ['required'],
'province' => ['required'],
'n_participants' => ['required', 'int', 'max:50'],
'n_census' => ['required', 'int', 'gte:n_participants'],
], $customMessages);
英文:
Use this
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'dni' => ['required', 'string', 'min:9', 'max:9'],
'surname1' => ['required', 'string', 'max:50'],
'surname2' => ['required', 'string', 'max:50'],
'v1' => ['required', 'string', 'max: 100'],
'v2' => ['required', 'string', 'max: 100'],
'district' => ['required', 'int'],
'section' => ['required', 'int'],
'chair' => ['required', 'string'],
'municipality' => ['required'],
'province' => ['required'],
'n_participants' => ['required', 'int', 'max: 50'],
'n_census' => ['required', 'int','gte:n_participants'],
], $customMessages);
added gte:n_participants in condtion
https://laravel.com/docs/6.x/validation#rule-gt
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论