I am customizing the templates and controllers that Laravel automatically creates for you with the command:
php artisan make:auth
And well, I have it almost finished in a first version of what I want. But I have come across the following problem:
I have added several fields to the table users
that Laravel creates by default, to customize it, including the img_perfil
. The issue of migrations and the addition of new fields to the users table have not caused me any type of problem.
But the issue of the field img_perfil
does hinder me:
When validating it in the frontend part, it is set with a input
type file
:
<div class="form-group row">
<label for="img_perfil" class="col-md-4 col-form-label text-md-right">Imagen de perfil</label>
<div class="col-md-6">
<input id="img_perfil" type="file" class="form-control @error('img_perfil') is-invalid @enderror" name="img_perfil" value="{{ old('img_perfil') }}" autocomplete="img_perfil">
@error('img_perfil')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
In my backend validation ( fileControllers\Auth\RegisterController.php
) I have it as follows:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
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'],
'apel_1'=>['string','max:255'],
'apel_2'=>['string','max:255'],
'user_name'=>['string','required','max:20','unique:users'],
'img_perfil'=>['max:100000','mimes:jpg,png,jpeg,gif'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'apel_1' => $data['apel_1'],
'apel_2' => $data['apel_2'],
'user_name' => $data['user_name'],
'img_perfil' => $data['img_perfil']
]);
}
}
As can be seen, in the validation of the field img_perfil
it is set as follows:
'img_perfil'=>['max:100000','mimes:jpg,png,jpeg,gif'],
It is understood that the validation that I do is: image size | extensions allowed I am not indicating that it is required, because when I do a test to insert user without selecting any photo it tells me:
The img profile must be a file of type: jpg, png, jpeg, gif.
But that should tell me if I have selected a photo. If there isn't, then it doesn't validate anything.
Depending on how you are submitting the form, you can use:
if the field will always be present, even if it has
null
a value.Or you can use:
if the field is only going to be present when it has a value.
nullable
:does not consider the values
null
as invalid.sometimes
:Run validation checks on a field only if that field is present.