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

0 votes

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',
]);
Sep 25, 2020 in Laravel by kartik
• 37,510 points
15,221 views

1 answer to this question.

0 votes

Hello @kartik,

try using nullable as a rule

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

Hope it helps!!

Thank you!!

answered Sep 25, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

How do you check if a field is not null with Eloquent?

Hello @kartik, Simply,we can use Model::whereNotNull('sent_at'); Or Model::whereRaw('sent_at is not null'); Thank ...READ MORE

answered Nov 11, 2020 in Laravel by Niroj
• 82,880 points
3,383 views
0 votes
1 answer

How to redirect to Login if user not logged in Laravel?

Hello @kartik, You can use it in middleware. ...READ MORE

answered Dec 4, 2020 in Laravel by Niroj
• 82,880 points
25,451 views
0 votes
1 answer

How to check if a cookie is set in laravel?

Hello @kartik, You can change: @if (Cookie::get('cookiename') !== false) to @if ...READ MORE

answered Dec 7, 2020 in Laravel by Niroj
• 82,880 points
3,594 views
0 votes
1 answer

How to identify wheather the request is HTTP GET or HTTP POST in Laravel?

Hey, In order to identify the type of ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,880 points
2,719 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,756 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,647 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,506 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,380 views
0 votes
1 answer

How to redirect to login if user is not authenticated in laravel?

Hello @kartik, Use middleware for this purpose and ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,880 points
8,073 views
0 votes
1 answer

How to check request is ajax or not in Laravel?

Hello, Laravel allow use of their library method that ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
11,382 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP