The Validation not working , the error message wont show up Laravel 8

Viewed 6244

Im a laravel beginner, also my first time to use a framework i just learned laravel for 5 days,so i don't understand much about this framework, im using the latest laravel 8 i have a problem with my project, when trying to validate the data from the form.

  1. i dont know this method is working or not (Probably not)

     $request->validate([
         'name' => 'required',
         'icons' => 'required',
     ]);
    
  2. when i trying to get the error message {{$error}} , only showing [] in my view.

<form action="{{ route('poseicon.store') }}" method="post" enctype="multipart/form-data">
@csrf
     <div class="display">
     
        <label>name:</label>
        <input type="text" name="name" id="name">
        <label>icons:</label>
        <input type="file" name="icons" id="icons" class="">
        {{$errors}}
        <input type="submit" value="submit">
    </div>
</form> 

this is my controller

public function store(Request $request)
{
    $request->validate([
        'name' => 'required',
        'icons' => 'required',
    ]);
    //this working correctly when validate method above deleted 
    $file = $request->file('icons');
    $filename = 'pose-photo-' . time() . '.' . $file->getClientOriginalExtension();
    $path = $file->storeAs('img', $filename);

    //this also working correctly when validate method above deleted 
    Poseicon::create([
        'name' => $request->name,
        'icons' =>  $filename
    ]);
    
    return redirect('/poseicon');
    
}

i already try all the documentation said but still not working

3 Answers

As laravel doc says, you should use one of @error directive or loop over $errors

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

OR

@error('title')
    <div class="alert alert-danger">{{ $message }}</div>
@enderror

Simply put this code where you want to show error message. Laravel provide this error code.

In your view page you put this.

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

In your controller You simply declare which field required.

$request->validate([
        'name' => 'required',
        'icons' => 'required',
    ]);

Try this.

controller

use Illuminate\Support\Facades\Validator;


     $validatedData = Validator::make($request->all(), [
        'name' => 'required',
        'icons' => 'required',
    ])->validate();

    $name = $validatedData['name'];
    $icons = $validatedData['icons'];

blade file put this above your form

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