Form Validation in Laravel

php artisan lang:publish

text-danger

{{ old('form_attribute_name_value') }}   //it's will keep old value

@error('form_name_attribute_value')
 {{ $message }}
 @enderror


validate(
[
                'name' => 'required|max:20|min:5',
                'email' => 'unique:students',
                'phone' => 'url',
            ])

'required|max:20|min:5'
required
max:20
min:10
unique:table_name
integer
size:10
numeric
email
url
lowercase
uppercase
alpha (only alphabet)

alpha_dash   (letters, numbers, dashes, and underscores allowed)
max_digits:20 (max length: 20 digit)
between:5,10 (length between 5 to 10)
digits_between:5,10 (digit length between 5 to 10)
min_digits:10 (min 10 digit)
same:name_field_value (both value should be match)
filled (like required)



Bd phone number validation sample: required|numeric|max_digits:11|min_digits:11

Laravel validation with Custom message

//only
$message=[
            'name.required' => 'Apnar name lagbe',
            'phone.required' => 'Phone number must be lagbe',
            'email.unique' => 'ei email diye r hbena',
        ];

$request->validate([
            'name'=>'required | regex:/^[\pL\s]+$/u|',
            'email'=>'required|email|unique:students',
            'phone'=>'required|numeric|max_digits:11|min_digits:11',
        ],$message)


//full
$message=[
            'name.required' => 'Apnar name lagbe',
            'phone.required' => 'Phone number must be lagbe',
            'email.unique' => 'ei email diye r hbena',
        ];


        $validation = $request->validate([
            'name'=>'required | regex:/^[\pL\s]+$/u|',
            'email'=>'required|email|unique:students',
            'phone'=>'required|numeric|max_digits:11|min_digits:11',
        ],$message);

        if($validation){
        Student::create($request->all());
        return redirect('/student');
        }

Leave a Comment