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

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

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。

我用于验证这两个字段(以及其他与此问题不相关的字段)的代码如下:

  1. <!-- language: php -->
  2. protected function validator(array $data)
  3. {
  4. $customMessages = [
  5. 'required' => ':attribute 是必填项',
  6. 'min' => ':attribute 至少需要 :min 个字符',
  7. 'max' => ':attribute 最多可以有 :max 个字符',
  8. 'string' => ':attribute 必须是一个字符串',
  9. 'int' => ':attribute 必须是一个整数',
  10. 'email' => ':attribute 是一个电子邮件地址',
  11. 'confirmed' => '你未确认电子邮件'
  12. ];
  13. return Validator::make($data, [
  14. 'name' => ['required', 'string', 'max:255'],
  15. 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
  16. 'password' => ['required', 'string', 'min:8', 'confirmed'],
  17. 'dni' => ['required', 'string', 'min:9', 'max:9'],
  18. 'surname1' => ['required', 'string', 'max:50'],
  19. 'surname2' => ['required', 'string', 'max:50'],
  20. 'v1' => ['required', 'string', 'max: 100'],
  21. 'v2' => ['required', 'string', 'max: 100'],
  22. 'district' => ['required', 'int'],
  23. 'section' => ['required', 'int'],
  24. 'chair' => ['required', 'string'],
  25. 'municipality' => ['required'],
  26. 'province' => ['required'],
  27. 'n_census' => ['required', 'int'],
  28. 'n_participants' => ['required', 'int', 'max: 50']
  29. ], $customMessages);
  30. }

是否可以使用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 -->

  1. protected function validator(array $data)
  2. {
  3. $customMessages = [
  4. &#39;required&#39; =&gt; &#39;:attribute es obligatori&#39;,
  5. &#39;min&#39; =&gt; &#39;:attribute cal que siga, com a m&#237;nim, :min caracters&#39;,
  6. &#39;max&#39; =&gt; &#39;:attribute cal que siga, com a m&#224;xim, :max caracters&#39;,
  7. &#39;string&#39; =&gt; &#39;:attribute cal que siga una cadena de caracters&#39;,
  8. &#39;int&#39; =&gt; &#39;:attribute cal que siga un enter&#39;,
  9. &#39;email&#39; =&gt; &#39;:attribute es un email&#39;,
  10. &#39;confirmed&#39; =&gt; &#39;No has confirmat el correu electr&#242;nic&#39;
  11. ];
  12. return Validator::make($data, [
  13. &#39;name&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:255&#39;],
  14. &#39;email&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;email&#39;, &#39;max:255&#39;, &#39;unique:users&#39;],
  15. &#39;password&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;min:8&#39;, &#39;confirmed&#39;],
  16. &#39;dni&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;min:9&#39;, &#39;max:9&#39;],
  17. &#39;surname1&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:50&#39;],
  18. &#39;surname2&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:50&#39;],
  19. &#39;v1&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max: 100&#39;],
  20. &#39;v2&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max: 100&#39;],
  21. &#39;district&#39; =&gt; [&#39;required&#39;, &#39;int&#39;],
  22. &#39;section&#39; =&gt; [&#39;required&#39;, &#39;int&#39;],
  23. &#39;chair&#39; =&gt; [&#39;required&#39;, &#39;string&#39;],
  24. &#39;municipality&#39; =&gt; [&#39;required&#39;],
  25. &#39;province&#39; =&gt; [&#39;required&#39;],
  26. &#39;n_census&#39; =&gt; [&#39;required&#39;, &#39;int&#39;],
  27. &#39;n_participants&#39; =&gt; [&#39;required&#39;, &#39;int&#39;, &#39;max: 50&#39;]
  28. ], $customMessages);
  29. }

Is it possible to use Validator::make() to do that?

答案1

得分: 2

  1. return Validator::make($data, [
  2. 'name' => ['required', 'string', 'max:255'],
  3. 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
  4. 'password' => ['required', 'string', 'min:8', 'confirmed'],
  5. 'dni' => ['required', 'string', 'min:9', 'max:9'],
  6. 'surname1' => ['required', 'string', 'max:50'],
  7. 'surname2' => ['required', 'string', 'max:50'],
  8. 'v1' => ['required', 'string', 'max:100'],
  9. 'v2' => ['required', 'string', 'max:100'],
  10. 'district' => ['required', 'int'],
  11. 'section' => ['required', 'int'],
  12. 'chair' => ['required', 'string'],
  13. 'municipality' => ['required'],
  14. 'province' => ['required'],
  15. 'n_participants' => ['required', 'int', 'max:50'],
  16. 'n_census' => ['required', 'int', 'gte:n_participants'],
  17. ], $customMessages);
英文:

Use this

  1. return Validator::make($data, [
  2. &#39;name&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:255&#39;],
  3. &#39;email&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;email&#39;, &#39;max:255&#39;, &#39;unique:users&#39;],
  4. &#39;password&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;min:8&#39;, &#39;confirmed&#39;],
  5. &#39;dni&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;min:9&#39;, &#39;max:9&#39;],
  6. &#39;surname1&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:50&#39;],
  7. &#39;surname2&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max:50&#39;],
  8. &#39;v1&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max: 100&#39;],
  9. &#39;v2&#39; =&gt; [&#39;required&#39;, &#39;string&#39;, &#39;max: 100&#39;],
  10. &#39;district&#39; =&gt; [&#39;required&#39;, &#39;int&#39;],
  11. &#39;section&#39; =&gt; [&#39;required&#39;, &#39;int&#39;],
  12. &#39;chair&#39; =&gt; [&#39;required&#39;, &#39;string&#39;],
  13. &#39;municipality&#39; =&gt; [&#39;required&#39;],
  14. &#39;province&#39; =&gt; [&#39;required&#39;],
  15. &#39;n_participants&#39; =&gt; [&#39;required&#39;, &#39;int&#39;, &#39;max: 50&#39;],
  16. &#39;n_census&#39; =&gt; [&#39;required&#39;, &#39;int&#39;,&#39;gte:n_participants&#39;],
  17. ], $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:

确定