Validation rules required_if with other condition (Laravel 5.4)

后端 未结 4 1823
逝去的感伤
逝去的感伤 2021-02-20 09:38

I got a problem with validation rules with nested conditions.

class StoreRequest extends Request
{
        public function authorize(){
        return true;
             


        
相关标签:
4条回答
  • 2021-02-20 10:06

    try this pass the value directly refer: https://www.npmjs.com/package/validatorjs

    "required_if:anotherfield,value"

    The field under validation must be present and not empty if the anotherfield field is equal to any value.

    'type_id' => 'required|integer', 'external_id' => 'required_if:type_i,3|integer',

    0 讨论(0)
  • 2021-02-20 10:10

    Your rule performs two checks that are independent of one another; just because the external_id field is not required when the type_id != 3, does not mean the integer check is ignored.

    What you are looking for is a conditional rule, which gives you finer control of when to perform a check, e.g. :

    $validator = Validator::make($data, [
        'type_id'   => 'required|integer'
    ]);
    
    $validator->sometimes('external_id', 'required|integer', function($input) {
        return $input->type_id == 3;
    });
    

    When using form validation, you can access the underlying validator instance by overriding the getValidatorInstance() method:

    class StoreRequest extends Request
    {
            public function authorize(){
            return true;
            }
    
            public function rules(){
                    return [
                        'type_id'     => 'required|integer'
                    ];
            }
    
            protected function getValidatorInstance() {
                $validator = parent::getValidatorInstance();
                $validator->sometimes('external_id', 'required|integer', function($input) {
                    return $input->type_id == 3;
                });
                return $validator;
            }
    }
    
    0 讨论(0)
  • 2021-02-20 10:16

    Just came across the same problem and found the following answer that seems to work for me:

    issue-using-required-if-validation-rule-in-form-builder

         return [
                    'type_id'     => 'required|integer',
                    'external_id' => 'required_if:type_id,==,3|nullable|integer',
                ];
    

    Result for me:

    field not populated, type id not 3 - pass

    field not populated, type id 3 - fail - required field

    field populated, type id 3, non-integer - fail in integer rule

    field populated, type id 3, integer - pass - all good!

    note - think nullable rule came in Laravel 5.3

    0 讨论(0)
  • 2021-02-20 10:20

    try this,

      class StoreRequest extends Request
        {
                public function authorize(){
                return true;
                }
    
                public function rules(){
                        return [
                            'type_id'     => 'required|integer',
                            'external_id' => 'required_if:type_id|in:3|integer',
                        ];
                }
        }
    
    0 讨论(0)
提交回复
热议问题