dongwen7371 2018-07-18 13:19
浏览 412
已采纳

1条消息,用于多个required_without规则

I have 2 (contact telephone and contact mobile) fields in a form which either A OR B need to be filled.

I'm validating the form using laravel request validation and the required_without rule, which looks like this.

'contact_telephone' => 'required_without:contact_mobile',
'contact_mobile' => 'required_without:contact_telephone',

So with this code it specifies that one of the 2 fields must be filled.

The error is that if I try to submit the form without filling either one in, the error message shows twice.

How would I show the message just once? Or is there a better rule to achieve this instead of required_without that i'm missing?

  • 写回答

2条回答 默认 最新

  • dragonsun00000 2018-07-18 13:51
    关注

    After playing around with it, I realised that only 1 of the required_without are actually required.

    The logic is quite simple, the required validation (whatever one used) only fires for empty fields. If I specify that the contact telephone is required when the mobile is empty like so

    'contact_telephone' => 'required_without:contact_mobile',
    

    Then it will work for all 3 possible options.

    1. If I fill out the mobile and not the telephone then the mobile is no longer empty and the telephone field isn't required anymore (Passes validation).

    2. If I fill neither out, then the mobile is empty, and the telephone is then required, thus displaying 1 error message (Fails Validation).

    3. If I fill out the telephone then it does not worry about the mobile validation as the telephone is not empty anyway (Passes validation).

    This means that either one of the 2 fields are required, but it will only display 1 error message and requires 1 rule.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效