28

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
  • stackoverflow.com/questions/41697979/… Commented Jan 23, 2017 at 11:44
  • or you can put if condition in rule Commented Jan 23, 2017 at 11:45
  • you can use required_if validation rule . refer : laravel.com/docs/5.3/validation Commented Jan 23, 2017 at 11:52
  • can you show more code from where you sending hidden variable..? Commented Jan 23, 2017 at 11:53
  • @mith I tried this, but I get no error message for it: 'image' => 'required_if:old_image, ""', Commented Jan 23, 2017 at 11:56

9 Answers 9

51

You can work with two rules

'image' => 'required_if:old_image'

Or:

'image' => 'required_without:old_image'
Sign up to request clarification or add additional context in comments.

5 Comments

Please, remove this extra comma.
Done! Thanks @MouhammedElshaaer
doesnt work anymore : Validation rule required_if requires at least 2 parameters
required_if:old_image,null
Atm (Laravel 8) required_if:x,null isn't the same as required_without:x
13

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'

2 Comments

'required_if:old_image,!=,null' don't work..
Use required_unless:old_image,null
11

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."]]

1 Comment

This should be the accepted answer. Some integrations with Laravel rules require array configuration (such as Folklore GraphQL), and any non-pure validation will not suffice.
5

You missing nullable validation

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

Comments

5

Use this

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

Comments

5

I know this is an old question but I came across it during my search. So I'll post my solution here as well for future visitors.

I think this solution is what the OP wants. This solution is not using other packages or custom validation methods so that's a plus as well imo.

What I did was this:

public function rules()
{
    return [
      'first_name' => ['required', 'max:50'],
      'last_name' => ['required', 'max:50'],
      'birthday' => ['required'],
      'old_image' => ['nullable'],
      'image' => [
          Rule::requiredIf(function() {
              return empty($this->request->get('old_image'));
          })
      ]
    ];
}

Comments

1

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
     ]);
}

2 Comments

this is what i said in comment above,
There's a Laravel wrapper for this; $request->has('old_image')
0
use Illuminate\Validation\Rule;
'image' => Rule::when($request->image != null, 'required|string')

Comments

0

You will need to use required_with:

'image' => 'required_with:the_other_field_name_here'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.