Laravel required_unless validation

时间:2017-07-20 08:10:14

标签: laravel validation laravel-5

使用required_unless验证时遇到问题。

我想要做的是当building name 为空或其他方式时需要building number,以便在building number时需要building name } 为空。目前它需要两者。

public function rules(Request $request)
{
    $id = $request->input('entity_id');
    $entity = Entity::with('address')
        ->find($id);
    $validation = $entity->id;
    return [
        'name' => 'required|alpha_special|unique:entities,name,'.$validation.'|min:3|max:30',
        'type' => 'required',
        'email' => 'required|email|unique:entities,email,'.$validation,
        'building_name' => 'alpha|required_unless:building_number,value',
        'building_number' => 'numeric|required_unless:building_name,value',
        'street' => 'required|alpha_spaces',
        'town' => 'nullable|alpha',
        'city' => 'nullable|alpha',
        'postcode' => 'required|max:8',
        'telephone' => 'required|telephone',
        'ical' => 'nullable|active_url',
    ];
}

视图:

<div class="hidden multi form-group{{ $errors->has('building_name') ? ' has-error' : '' }}">
                          <div class="building_name_message form-control alert-warning" style="display: none;"></div>
                            <label  id="building_name_label" for="building_name" >Building Name</label>
                            <input  id="building_name" type="text" class="form-field form-control" placeholder="Building name (If applicable)" name="building_name" value="{{ old('building_name') }}">
                              @if ($errors->has('building_name'))
                                <span class="help-block">
                                  <strong>{{ $errors->first('building_name') }}</strong>
                                </span>
                              @endif
                          <div class="building_number_message form-control alert-warning" style="display: none;"></div>
                            <label  id="building_number_label" for="building_number" >Building Number</label>
                            <input  id="building_number" type="text" class="form-field form-control" placeholder="Building Number (If applicable)" name="building_number" value="{{ old('building_number') }}">
                              @if ($errors->has('building_number'))
                                <span class="help-block">
                                  <strong>{{ $errors->first('building_number') }}</strong>
                                </span>
                              @endif
                              <div class="form-navigation">
                                <button type="button" class="previous btn btn-info pull-left">&lt; Previous</button>
                                <button type="button" id="next" class="next btn btn-info pull-right">Next &gt;</button>
                              </div>
                        </div>

3 个答案:

答案 0 :(得分:4)

您必须在验证空值检查时放置 null 而不是

'building_name' => 'alpha|required_unless:building_number,null',
'building_number' => 'numeric|required_unless:building_name,null',

另一种方法是使用required_without

'building_name' => 'alpha|required_without:building_number',
'building_number' => 'numeric|required_without:building_name',

请参阅文档here

<强>更新

由于上述验证分别针对alphanumeric检查building_namebuilding_number类型,您可以更改验证规则的顺序,以便检查{{1首先,如果它不存在,则不检查值。

required_without

这可能适用于您的情况。

希望,你理解。

答案 1 :(得分:1)

您应该根据您的要求使用required_without

'building_name' => 'alpha|required_without:building_number',
'building_number' => 'numeric|required_without:building_name',

答案 2 :(得分:1)

来自文档:

  

<强> required_unless:anotherfield,值,...

     

验证字段必须存在且不为空,除非anotherfield字段等于任何值。

这听起来并不像你需要的那样(从技术上来说,如果“value”设置为“null”但是重点是什么呢?),但下一个会这样做:

  

<强> required_without:FOO,酒吧,...

     

验证字段必须存在且仅当任何其他指定字段不存在时才为空。

'building_name' => 'alpha|required_without:building_number',
'building_number' => 'numeric|required_without:building_name',