How to validate an input field if value is not null in Laravel
Asked Answered
M

4

26

I am trying to create and update users with laravel 5.4

This is the validation added for create user. It works.

$this->validate($request, [
    'name' => 'required|max:255',
    'email' => 'required|email|max:255|unique:users',
    'password' => 'required|min:6|confirmed',
]);

On update the password field is not required. But validate the min:6 and confirmed rule if the password field is not null. Tried with sometimes.. but not working..

$this->validate($request, [
    'name' => 'required|max:255',
    'email' => 'required|email|max:255|unique:users,email,'.$id,
    'password' => 'sometimes|min:6|confirmed',
]);
Mayfly answered 29/3, 2017 at 5:45 Comment(1)
If you have 2 seperated functions for create and update you could remove the password validation rule from updateSoliloquize
F
58

try using nullable as a rule

'password' => 'nullable|min:6|confirmed',

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

Fayfayal answered 29/3, 2017 at 5:53 Comment(0)
U
10

In case column is nullable

'password' => 'nullable|min:6|confirmed',

@Rejinderi's answer is correct!

In case column is not nullable (This may help other)

'password' => 'sometimes|required|min:6|confirmed',

Result

username = 'admin',
password = null // fail- required

username = 'admin',
password = 123 // fail- min:6

username = 'admin' // pass- validate only exist
Unsparing answered 7/9, 2018 at 2:46 Comment(0)
B
0

This is how I would do it:

//For new or create :
$this->validate($request, [
    'name' => 'required|max:255',
    'email' => 'required|email|max:255|unique:users',
    'password' => 'required|min:6|confirmed',
]);

//For edit or update:
$this->validate($request, [
    'name' => 'required|max:255',
    'email' => 'required|email|max:255|unique:users',
    'password' => 'min:6|confirmed',//just remove required from password rule
]);

Explanation:
This way the value will be validated only when it will be defined (present) in request
If you use nullable, than validator will accept null as value (which i assume is not acceptable)
If you remove password validation from update than this input will not be validated at all, and any value will be accepted (which is again not acceptable);

Byronbyrum answered 29/11, 2017 at 12:43 Comment(1)
Read the question carefully before answering.Mayfly
E
0

we can achieve this also using Validator Class's sometimes method as per Documentation

use Illuminate\Support\Facades\Validator;

//... your code ...

$validator = Validator::make($request->all(), [
      'name' => 'required',
      'contact' => 'required',
]);
$validator->sometimes('password', 'min:6|confirmed', function ($input) {
    return (strlen($input->password) > 0);
});
if ($validator->fails()) {
     return redirect()->back()->withErrors($validator)->withInput();
} 

this will validate password attribute rules when password field's length is greater than 0. otherwise will ignore that password attribute's rule.

Extant answered 16/3, 2022 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.