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.