Answers for "validation laravel 8"

PHP
3

email validation in laravel

'email' => 'required|email|unique:users,email',
//@sujay
Posted by: Guest on October-09-2020
8

if any error in blade laravel

@if ($errors->any())
     @foreach ($errors->all() as $error)
         <div>{{$error}}</div>
     @endforeach
 @endif
Posted by: Guest on November-01-2020
7

laravel validation example

//import		
		use Illuminate\Support\Facades\Validator;
	
		// single var check
        $validator = Validator::make(['data' => $value],
            ['data' => 'string|min:1|max:10']
        );
        if ($validator->fails()) {
            // your code
        }

        // array check
        $validator = Validator::make(['data' => $array],
            ['email' => 'string|min:1|max:10'],
            ['username' => 'string|min:1|max:10'],
            ['password' => 'string|min:1|max:10'],
            ['...' => '...']
        );

        if ($validator->fails()) {
            // your code
        }
Posted by: Guest on April-17-2020
1

validation in laravel

enum
  'priority'      => ['required', 'in:normal,meduim,high'],	
integer
  'department_id' => 'required|integer',
string
  'email' => 'required|email|unique:users',
  'title'         => ['required', 'min:3'],
  'name' => 'required|max:120',
integer
	'phone' => 'required|regex:/(01)[0-9]{9}/'
  	'phone' => 'required|numeric|phone_number|size:11'
  	'PHONE' => 'required|regex:/(0)[0-9]/|not_regex:/[a-z]/|min:9',


From Laravel 5.5 on you can use an artisan command to create a new Rule which you can code regarding your requirements to decide whether it passes or fail.
Ej: php artisan make:rule PhoneNumber
Then edit app/Rules/PhoneNumber.php, on method passes
  /**
 * Determine if the validation rule passes.
 *
 * @param  string  $attribute
 * @param  mixed  $value
 * @return bool
 */
public function passes($attribute, $value)
{

    return preg_match('%^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$%i', $value) && strlen($value) >= 10;
}


use App\Rules\PhoneNumber;

$request->validate([
    'name' => ['required', new PhoneNumber],
]);
Posted by: Guest on October-12-2021
2

laravel validation

<!-- /resources/views/post/create.blade.php -->

<h1>Create Post</h1>

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<!-- Create Post Form -->
Posted by: Guest on August-22-2020
4

laravel validation

/**
 * Store a new blog post.
 *
 * @param  Request  $request
 * @return Response
 */
public function store(Request $request)
{
    $validatedData = $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    // The blog post is valid...
}
Posted by: Guest on October-19-2020

Browse Popular Code Answers by Language