Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

olabd's avatar
Level 1

Validation based on multiple fields

Hello, so I've encountered a problem to properly validate my form input. You may find some explanation in the question I posted yesterday: https://laracasts.com/discuss/channels/requests/problem-with-dependent-validation (+ one more condition if checkbox on/off)

I managed to implement the solution that is working but I think it's not quite nicely written.

Here's the code:

        Validator::extend('myvalidation', function($attribute, $value, $parameters, $validator){
            $r = true;
            if ($value == '0'){
                if (!Input::hasFile('cover')) $r = false;
            }elseif ($value == '1'){
                if (Input::get('cover_delete') == "on") $r = false;
            }
            return $r;
        });

The fragment of form I modified:

                    <fieldset class="form-group row {{ $errors->has('cover') ? ' has-danger' : '' }}">
                        <div class="col-sm-6">
                            {!! Form::label('cover') !!}
                            {!! Form::file('cover', null, ['class' => 'form-control', 'id' => 'cover']) !!}
                        </div>
                        @if(File::exists($modelCycleEdit->cover))
                        <div class="col-sm-6">
                            <img src="{{ URL::asset($modelCycleEdit->cover) }}" alt="img" style = "max-width:200px">
                            {!! Form::checkbox('cover_delete', null, false, ['id' => 'cover_delete']) !!}<span>usuń</span>
                            {!! Form::hidden('is_cover', '1') !!}
                        </div>
                        @else
                            {!! Form::hidden('is_cover', '0') !!}
                        @endif
                    </fieldset>

In Request Object return rules array:

            //'cover' => 'required_if:digital_forms,1',
            'is_cover' => 'myvalidation',

The thing I dislike mostly about this is that the rule is not set to the main field I need to validate ('cover') but to the flag field('is_cover'). I couldn't set it to the 'cover' field because there may be cases it is not sent (when I don't need to upload cover because it is in the database). Do you have idea how to tackle this? I also think my solution is not really elegant so I would much appreciate every criticism.

0 likes
1 reply
D9705996's avatar

It looks like you are trying to do too much in you view, controller and validation. I would look at your domain to see if you can extract the multiple things you are trying to achieve in one place into multiple actions.

E.g. have separate forms and routes for each specific thing you want to do.

Your validation and views then don't need any conditional elements.

It might be hard to make the change but you should make it now otherwise you will be constantly fighting when you want to add, remove or change features in your application

Please or to participate in this conversation.