Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR HTML

Server side validations Laravel

//First, validate the request object and add a message for every field

$validatedData = $request->validate([
                'name' => 'required',
                'password' => 'required|min:5',
                'email' => 'required|email|unique:users'
            ], [
                'name.required' => 'Name is required',
                'password.required' => 'Password is required'
            ]);
            
//Second, add a condition in the view to show the error, if it exist

<div class="form-group">
<label>Name:</label>
<input type="text" name="name" class="form-control" placeholder="Name">
@if ($errors->has('name'))
	<span class="text-danger">{{ $errors->first('name') }}</span>
@endif
</div>


//alert version 

@if ($errors)
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                @foreach ($errors->all() as $error)
                    @if (count($errors->all()) > 1)
                        <strong>{{ $error }}</strong> <br>
                    @else
                        <strong>{{ $error }}</strong>
                    @endif
                @endforeach
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        @endif
 
PREVIOUS NEXT
Tagged: #Server #side #validations #Laravel
ADD COMMENT
Topic
Name
4+7 =