如何根据另一个表单字段的数据验证表单字段? – Laravel 6

huangapple go评论66阅读模式
英文:

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 = [
        &#39;required&#39; =&gt; &#39;:attribute es obligatori&#39;,
        &#39;min&#39; =&gt; &#39;:attribute cal que siga, com a m&#237;nim, :min caracters&#39;,
        &#39;max&#39; =&gt; &#39;:attribute cal que siga, com a m&#224;xim, :max caracters&#39;,
        &#39;string&#39; =&gt; &#39;:attribute cal que siga una cadena de caracters&#39;,
        &#39;int&#39; =&gt; &#39;:attribute cal que siga un enter&#39;,
        &#39;email&#39; =&gt; &#39;:attribute es un email&#39;,
        &#39;confirmed&#39; =&gt; &#39;No has confirmat el correu electr&#242;nic&#39;
    ];

    return Validator::make($data, [
        &#39;name&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:255&#39;],
        &#39;email&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;email&#39;, &#39;max:255&#39;, &#39;unique:users&#39;],
        &#39;password&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;min:8&#39;, &#39;confirmed&#39;],
        &#39;dni&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;min:9&#39;, &#39;max:9&#39;],
        &#39;surname1&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:50&#39;],
        &#39;surname2&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:50&#39;],
        &#39;v1&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max: 100&#39;],
        &#39;v2&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max: 100&#39;],
        &#39;district&#39; =&gt; [&#39;required&#39;, &#39;int&#39;],
        &#39;section&#39; =&gt; [&#39;required&#39;, &#39;int&#39;],
        &#39;chair&#39; =&gt; [&#39;required&#39;, &#39;string&#39;],
        &#39;municipality&#39; =&gt; [&#39;required&#39;],
        &#39;province&#39; =&gt; [&#39;required&#39;],
        &#39;n_census&#39; =&gt; [&#39;required&#39;, &#39;int&#39;],
        &#39;n_participants&#39; =&gt; [&#39;required&#39;, &#39;int&#39;, &#39;max: 50&#39;]
    ], $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, [
            &#39;name&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:255&#39;],
            &#39;email&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;email&#39;, &#39;max:255&#39;, &#39;unique:users&#39;],
            &#39;password&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;min:8&#39;, &#39;confirmed&#39;],
            &#39;dni&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;min:9&#39;, &#39;max:9&#39;],
            &#39;surname1&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:50&#39;],
            &#39;surname2&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:50&#39;],
            &#39;v1&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max: 100&#39;],
            &#39;v2&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max: 100&#39;],
            &#39;district&#39; =&gt; [&#39;required&#39;, &#39;int&#39;],
            &#39;section&#39; =&gt; [&#39;required&#39;, &#39;int&#39;],
            &#39;chair&#39; =&gt; [&#39;required&#39;, &#39;string&#39;],
            &#39;municipality&#39; =&gt;  [&#39;required&#39;],
            &#39;province&#39; =&gt;  [&#39;required&#39;],
            &#39;n_participants&#39; =&gt; [&#39;required&#39;, &#39;int&#39;, &#39;max: 50&#39;],
            &#39;n_census&#39; =&gt; [&#39;required&#39;, &#39;int&#39;,&#39;gte:n_participants&#39;],
        ], $customMessages);

added gte:n_participants in condtion
https://laravel.com/docs/6.x/validation#rule-gt

huangapple
  • 本文由 发表于 2020年1月3日 19:20:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定