Laravel 5 如何对2个字段进行一次验证错误

Laravel 5 如何对2个字段进行一次验证错误,laravel-5,Laravel 5,我有两个邮政编码字段main和sub <input type="text" name="postal01"> <input type="text" name="postal02"> 然而,它并没有像我预期的那样工作 如何处理此情况?您可以在验证后使用挂钩。将以下内容添加到您的表单请求中: /** * Configure the validator instance. * * @param \Illuminate\Validation\Validator $valid

我有两个邮政编码字段main和sub

<input type="text" name="postal01">
<input type="text" name="postal02">
然而,它并没有像我预期的那样工作


如何处理此情况?

您可以在验证后使用挂钩。将以下内容添加到您的表单请求中:

/**
* Configure the validator instance.
*
* @param  \Illuminate\Validation\Validator  $validator
* @return void
*/
public function withValidator($validator)
{
    $validator->after(function ($validator) {
        if($validator->errors()->has('postal01') || $validator->errors()->has('postal02')) {
            $validator->errors()->add('postal_cd', 'Please enter a postal code');
        }
    });
}
。。。然后将其显示在刀片上:

{{ $errors->first('postal_cd') }}
那是我的打字错误

class MemberRequest extends FormRequest
{
  public function all($keys = null)
  {
    $result = parent::all($keys);

    if($this->filled('postal01') && $this->filled('postal02')) {
      $result['postal_code'] = $this->input('postal01') . $this->input('postal02');
   // $results['postal_code'] = $this->input('postal01') . $this->input('postal02');
    }
    return $result;
}
返回的值应为$result。但这是打字错误$results['邮政编码']。
它可以工作。

应该在哪里调用此函数?这似乎对我不起作用,你不叫它。您只需要将其添加到表单请求中。我是否需要为postal01和postal02设置规则以发生错误?是的,您可以按原样设置规则。钩子将检查验证器实例中是否存在错误,并在键
postal_cd
下添加一个新的错误。在刀片文件中,您只需输出
邮政光盘中的内容即可。谢谢。然而,如果我需要处理的每个字段都没有错误,但两个字段的总大小都是错误,我该如何处理它呢?例如,每个字段允许5位数字,但两个字段不允许总共10位?我能让你明白我的意思吗?
class MemberRequest extends FormRequest
{
  public function all($keys = null)
  {
    $result = parent::all($keys);

    if($this->filled('postal01') && $this->filled('postal02')) {
      $result['postal_code'] = $this->input('postal01') . $this->input('postal02');
   // $results['postal_code'] = $this->input('postal01') . $this->input('postal02');
    }
    return $result;
}