It seems that the validation fails because the selected file for the avatar field is not a valid image. That's the only thing I can spot
Avatar Validation error when registering new user. "The avatar must be an image"
Hi Everyone,
I uploaded a question earlier however due to my late reply back haven't had an answer so made a new post for the updated reply.
I am trying to register a new user however I keep getting the error "The Avatar must be an image".
I have looked over my code countless times and cannot see any issues with it. Maybe I just need a fresh pair of eyes to have a look and it maybe obvious.
I have tried looking online however the solutions for other people that have worked in the past don't work for me as I have already set what they were talking about adding up.
My code is bellow. I have included the file system as I was working on another image upload for products. But thought I would include it just in case anyone mentions it.
Controller
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'userlevel' => ['required','string'],
'avatar' => ['required', 'image', 'mimes:jpg,jpeg,bmp,svg,png', 'max:5000'],
]);
}
protected function create(array $data)
{
if(request()->has('avatar')){
$profileimage = request()->file('avatar');
$proimgname = time() .'.'. $profileimage -> getClientOriginalExtension();
$proimgsave = public_path('/uploads/avatars/');
$profileimage -> move($proimgsave, $proimgname);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'userlevel' => $data['userlevel'],
'avatar' => '/uploads/avatars/'. $proimgname,
]);
}
}
}
View
@extends('layouts.app')
@section('content')
<div class="content">
<div><h2 class="header">{{ __('Register') }}</h2></div>
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data ">
@csrf
<div class="form">
<div class="name">
<label for="name" class="left-form">{{ __('Name') }}</label>
<input id="name" type="text" class="right-form @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="holder">
<label for="email" class="left-form">{{ __('E-Mail Address') }}</label>
<input id="email" type="email" class="right-form @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="holder">
<label for="avatar" class="left-form">{{ __('Avatar') }}</label>
<input id="avatar" class="right-form @error('avatar') is-invalid @enderror" type="file" name="avatar">
@error('avatar')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="holder">
<label for="password" class="left-form">{{ __('Password') }}</label>
<input id="password" type="password" class="right-form @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="confirm-pass">
<label for="password-confirm" class="left-form">{{ __('Confirm Password') }}</label>
<input id="password-confirm" type="password" class="right-form" name="password_confirmation" required autocomplete="new-password">
</div>
{{-- <div class="holder">--}}
{{-- <label for="bio" class="left-form">{{ __('Bio') }}</label>--}}
{{-- <textarea id="bio" type="text" class="right-form @error('bio') is-invalid @enderror" name="bio" value="{{ old('bio') }}" placeholder="Please enter your bio." autofocus></textarea>--}}
{{-- @error('bio')--}}
{{-- <span class="invalid-feedback" role="alert">--}}
{{-- <strong>{{ $message }}</strong>--}}
{{-- </span>--}}
{{-- @enderror--}}
{{-- </div>--}}
{{-- <div class="holder">--}}
{{-- <label for="experience" class="left-form">{{ __('Experience Level') }}</label>--}}
{{-- <input id="experience" type="text" class="right-form @error('experience') is-invalid @enderror" name="experience" value="{{ old('experience') }}" placeholder="Whats your experiene level?" autofocus>--}}
{{-- @error('experience')--}}
{{-- <span class="invalid-feedback" role="alert">--}}
{{-- <strong>{{ $message }}</strong>--}}
{{-- </span>--}}
{{-- @enderror--}}
{{-- </div>--}}
{{-- <div class="holder">--}}
{{-- <label for="years_played" class="left-form">{{ __('Years Played?') }}</label>--}}
{{-- <input id="years_played" type="text" class="right-form @error('years_played') is-invalid @enderror" name="years_played" value="{{ old('years_played') }}" placeholder="How many years have you been playing?" autofocus>--}}
{{-- @error('years_played')--}}
{{-- <span class="invalid-feedback" role="alert">--}}
{{-- <strong>{{ $message }}</strong>--}}
{{-- </span>--}}
{{-- @enderror--}}
{{-- </div>--}}
{{-- <div class="holder">--}}
{{-- <label for="gear" class="left-form">{{ __('Your Gear') }}</label>--}}
{{-- <input id="gear" type="text" class="right-form @error('gear') is-invalid @enderror" name="gear" value="{{ old('gear') }}" placeholder="What Gear do you use?" autofocus>--}}
{{-- @error('gear')--}}
{{-- <span class="invalid-feedback" role="alert">--}}
{{-- <strong>{{ $message }}</strong>--}}
{{-- </span>--}}
{{-- @enderror--}}
{{-- </div>--}}
<input id="userlevel" name="userlevel" class="input" type="text" placeholder="" value="user" hidden>
<div class="submit">
<div class="reset-reg">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</div>
</form>
</div>
@endsection
Model
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password','updated_at', 'userlevel','avatar',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
private $email;
public function isAdmin()
{
return $this->userlevel === 'admin';
}
}
Filesystem
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DRIVER', 'local'),
/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => public_path('uploads/products/'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
],
];
Hopefully you'll be able to see something I can't, Ben :)
Your problem is in below code
$user = new User;
if (request()->hasFile('avatar')) {
// Create avatar
$user->avatar = '/uploads/avatars/'. $avatarname;
}
$user->fill([
// Other fields
'avatar' => $user, <------ HERE IS THE PROBLEM ----->
]);
You assign the user model to the avatar key, but that variable is then pointing to itself. You simply have to remove avatar => $user, here and it should work as expected!
Please or to participate in this conversation.