15
votes

I have a Laravel 5.3 app, and an update form where I send user profile values. One of them is user image as well, I am creating a hidden input field for it, so if the user is not uploading any new image, I can still see that he has the hidden field with the old image value.

<input type="hidden" class="form-control" name="old_image" value="{{ $player->image_filename }}" id="oldImage">

If he removes the image, the hidden input value becomes empty.

document.getElementById('oldImage').value = '';

That is how I thought of updating a user image. But I don't know how to set up validation rules for that form. Something that would be similar to this:

    public function rules()
    {
        return [
          'first_name' => 'required|max:50',
          'last_name' => 'required|max:50',
          'birthday' => 'required',
          'image' => required if old_image empty
        ];
    }

I have tried with 'image' => 'required_without:old_image', and also with 'image' => 'required_if:old_image, null', but none of them showed any error message for missing image. I am showing error messages like this:

6
or you can put if condition in ruleBugfixer
you can use required_if validation rule . refer : laravel.com/docs/5.3/validationmith
can you show more code from where you sending hidden variable..?Ramzan Mahmood
@mith I tried this, but I get no error message for it: 'image' => 'required_if:old_image, ""',Marco

6 Answers

34
votes

You can work with two rules

'image' => 'required_if:old_image'

Or:

'image' => 'required_without:old_image'
8
votes

this is can be done by required_without rule, if it's not working please show an actual sample request data ( dd(request()->all() )) and the validation rules you are using. the scenario you want to catch is simple: if both fields is empty.

in your example, applying this rule should work:

return [
          // ...
          'image' => 'required_without:old_image'
        ];

required_without:foo,bar,...

The field under validation must be present and not empty only when any of the other specified fields are not present. Validation - Laravel 5.3

Example:

$rules = [
        'new_value' => 'required_without:old_value'
        ];

validator(
        ['new_value' =>'',
        'old_value' => 'old value is preserved here']
    , $rules)->errors()->toArray();

// output: [] <-- no errors!

validator(
        ['new_value' =>'New value!',
        'old_value' => '']
    , $rules)->errors()->toArray();

// output: [] <-- no errors!

validator(
        ['new_value' =>'',
        'old_value' => '']
    , $rules)->errors()->toArray();

// output: ["new_value" => ["The new value field is required when old value is not present."]]
2
votes

For new laravel versions, if you need to use required_if, it requires at least two parameters, so you should use the second and third parameter as if conditions.

  'old_image'=>'required_if:image,=,null',
  'image'=>'required_if:old_image,=,null'
1
votes

Try this

if (empty($request->input('old_image'))){
     $this->validate($request, [
       'image' => 'required',
     ]);
}
else {
     // Validation in case of else goes here
     $this->validate($request, [
      // rule
     ]);
}
1
votes

You missing nullable validation

'old_image' => 'required'
'image' => 'required_without:old_image|nullable'
0
votes

Use this

'image'=>'required_if:old_image,=,null'