Laravel Part-10
Laravel Part-10
--------------------------------------------------------------------------
Validating Form
a. Validate() Method
b. Form Request Validation
Validate ( ) Method
a. validate ( ) Method is used to validate the application's incoming
data.
b. Validate method is available on all incoming HTTP Requests.
Example:-
App\Http\Controllers\RegistrationController.php
public function signup(Request $request)
{
// single Rule
$validate=$request->validate([
'name'=>'required',
'email'=>'required',
'pass'=>'required'
]) ;
// The form is valide
}
Validation Rules
a. Rules specified as Array
public function signup(Request $request)
{
// Rules specified as array
$validate=$request->validate([
'name'=>['required'],
'email'=>['required','min:20'],
'pass'=>['required'],
]) ;
}
@if ($errors->any())
<ul>
@foreach ($errors->all() as $message)
<li> {{$message}}</li>
@endforeach
</ul>
@endif
2. Retrieving All Error Messages For A Field
If you need to retrieve an array of all the messages for a given
field, use the get method:
@if ($errors->any())
<ul>
@foreach ($errors->get('email') as $message)
<li>{{$message}}</li>
@endforeach
</ul>
@endif
If you are validating an array form field, you may retrieve all of the
messages for each of the array elements using the * character:
@foreach ($errors->get(‘attachments.*’) as $message)
{{$message}}
@endforeach
Laravel will place the new rule in the app/Rules directory. If this
directory does not exist, Laravel will create it when you execute the
Artisan command to create your rule.
php artisan make:rule RuleName
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
b. The message method should return the validation error message that
should be used when validation fails.
c. import in controller : -use App\Rules\Uppercase;
$request->validate([
'name'=>['required',new Uppercase],
]);
2. Using Closures
a. If you only need the functionality of a custom rule once throughout
your application, you may use a closure instead of a rule object. The
closure receives the attribute's name, the attribute's value, and a
$fail callback that should be called if validation fails.
b. use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
‘name' => [
'required',
‘max:20',
function ($attribute, $value, $fail) {
if ($value === ‘sonam') {
$fail('The '.$attribute.' is invalid.');
}
}, ],]);
$validate=$request->validate([
'name'=>['required',
function ($attribute,$value,$fail){
if($value==='admin')
{
$fail('The' .$attribute. 'is invalid');
}
}
],
dd($validate);//Here all details inside the validation method.
6. The rules() method returns the validation rules that should apply to the
request's data.
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class RegisterationRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name'=>'required',
'email'=>'required',
'pass'=>'required'
];
}
}
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;