27

I'm trying to create a user update validation through form, where I pass, for example 'password'=>NULL, or 'password'=>'newone';

I'm trying to make it validate ONLY if it's passed as not null, and nothing, not even 'sometimes' works :/

I'm trying to validate as :

Validator::make(
    ['test' => null], 
    ['test' => 'sometimes|required|min:6']
)->validate();

But it fails to validate.

miken32
  • 42,008
  • 16
  • 111
  • 154
GTMeteor
  • 807
  • 3
  • 9
  • 17
  • You either use `sometimes` or `required`. They're mutually exclusive. If you just use sometimes, it will be validated if it's not empty. – Marco Aurélio Deleu Feb 18 '17 at 18:58
  • Yeah, but `sometimes` also makes the test fail if the value is passed as NULL :/ I guess, the problem then lies in passing NULL values... – GTMeteor Feb 18 '17 at 19:45

5 Answers5

49

Perhaps you were looking for 'nullable'?

'test'=> 'nullable|min:6'
Matt K
  • 507
  • 4
  • 4
13

Though the question is a bit old, this is how you should do it. You dont need to struggle so hard, with so much code, on something this simple.

You need to have both nullable and sometimes on the validation rule, like:

$this->validate($request, [
  'username' => 'required|unique:login',
  'password' => 'sometimes|nullable|between:8,20'
]);

The above will validate only if the field has some value, and ignore if there is none, or if it passes null. This works well.

nixxx
  • 423
  • 1
  • 9
  • 23
3

Do not pass 'required' on validator

Validate like below

$this->validate($request, [
    'username' => 'required|unique:login',
    'password' => 'between:8,20'
]);

The above validator will accept password only if they are present but should be between 8 and 20

This is what I did in my use case

case 'update':
                $rules = [
                            'protocol_id' => 'required',
                            'name' => 'required|max:30|unique:tenant.trackers'.',name,' . $id, 
                            'ip'=>'required',
                            'imei' => 'max:30|unique:tenant.trackers'.',imei,' . $id, 
                            'simcard_no' => 'between:8,15|unique:tenant.trackers'.',simcard_no,' . $id, 

                            'data_retention_period'=>'required|integer'
                         ];  
            break;

Here the tracker may or may not have sim card number , if present it will be 8 to 15 characters wrong

Update

if you still want to pass hardcoded 'NULL' value then add the following in validator

$str='NULL';
$rules = [
    password => 'required|not_in:'.$str,
];
sumit
  • 15,003
  • 12
  • 69
  • 110
  • he is sending null on purpose if the field is empty. so your answer doesn't work! that was my first answer so i updated it! – lewis4u Feb 18 '17 at 20:23
  • True, but he is not using laravel as all the others do...he is sending all the values even though they are not populated....so with your solution null gets to be validated and it doesn't have more than 6 chars and it shows a validation error!!! – lewis4u Feb 18 '17 at 20:27
  • Well its OP problem, and your as well. Laravel style is always like that.. you are free to downvote :) – sumit Feb 18 '17 at 20:30
  • You are not reading me. In laravel if you use it by the documentation the empty input fields are not sent with request! But the OP has custom made it to send null when input field is empty! And if you set the rule to check anything: for example `min:6` it will check the null on that field and null doesn't have enough characters and it will throw a validation error!! – lewis4u Feb 18 '17 at 20:32
  • yeah that will work, just replace `required` with `min:6|not_in:'.$str` – lewis4u Feb 18 '17 at 20:46
  • 1
    He is obviously a beginner, but so was I... it will come to him with time ;) (if he continues to use Laravel) – lewis4u Feb 18 '17 at 20:48
3

I think you are looking for filled. https://laravel.com/docs/5.4/validation#rule-filled

HG Sim
  • 126
  • 4
2

The relevant validation rules are:

  • required
  • sometimes
  • nullable

All have their uses and they can be checked here:

https://laravel.com/docs/5.8/validation#rule-required

if you want validation to always apply

https://laravel.com/docs/5.8/validation#conditionally-adding-rules

if you want to apply validation rules sometimes

https://laravel.com/docs/5.8/validation#a-note-on-optional-fields

if you want your attribute to allow for null as value too

TecBeast
  • 930
  • 8
  • 16