HDTuto.com

Laravel Bail Rule Validation Example



In this article we will cover on how to implement laravel validation bail example. This tutorial will give you simple example of laravel validation bail not working . This article goes in detailed on laravel bail rule example. We will look at example of laravel stop validation on first failure. you will do the following things for laravel bail validation rule.

In this example i will explain you why we have to use bail validation rule in our laravel application. you can easily use bail validation in laravel 6 and laravel 7. If you added more then one validation on your field like required, integer, min and max then if first is fail then other should stop to display error message. right now by default it print other too. so you can prevent that by using bail validation rule in laravel.

Here, i will give you bellow both example and you can also see screen shot of layout so let's see both example so you can understand well.

Without Bail Validation:

/**

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

$request->validate([

'number' => 'required|integer|min:3|max:10',

'detail' => 'required',

]);

Product::create($request->all());

return redirect()->route('products.index')

->with('success','Product created successfully.');

}

Output:

Using Bail Validation:

/**

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

$request->validate([

'number' => 'bail|required|integer|min:3|max:10',

'detail' => 'required',

]);

Product::create($request->all());

return redirect()->route('products.index')

->with('success','Product created successfully.');

}

Output:

I hope you got it the point.

I hope it can help you...




April 8, 2020 | Category : Laravel



Blog Search


Copyright © 2020 HDTuto.com. All rights reserved.