Cant use action attribute in form - php

So I am making a crud application. But i have a problem in my edit part. I setted up the edit view and the action to the update function, where I setted up the update too. But when I hit submit it wont redirect me to the page and it wont update, it will stay to the edit page, and its not giving me any error!
This is my form(don't mind the style):
<form method="post" action="{{route('profile.update', $user->id)}}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="col-8 offset-2">
<div class="row">
<h1>Edit Profile</h1>
</div>
<div class="form-group row">
<label for="title" class="col-md-4 col-form-label">Title</label>
<input id="title"
type="text"
class="form-control #error('name') is-invalid #enderror"
name="title"
value="{{$user->profile->title}}"
required autocomplete="title" autofocus>
#error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group row">
<label for="caption" class="col-md-4 col-form-label">Description</label>
<input id="description"
type="text"
class="form-control #error('name') is-invalid #enderror"
name="description"
value="{{$user->profile->description}}"
required autocomplete="description" autofocus>
#error('description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group row">
<label for="url" class="col-md-4 col-form-label">Url</label>
<input id="url"
type="text"
class="form-control #error('name') is-invalid #enderror"
name="url"
value="{{$user->profile->url}}"
required autocomplete="url" autofocus>
#error('url')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="row">
<label for="image" class="col-md-4 col-form-label">Profile Image</label>
<input type="file" class="form-control-file" id="image" name="image">
#error('image')
<strong>{{ $message }}</strong>
#enderror
</div>
<div class="row pt-4">
<button type="submit" class="btn btn-primary w-25">Save Profile</button>
</div>
</div>
</form>
Here is my route:
Route::patch('/profile/{user}', [ProfilesController::class, 'update'])->name('profile.update');
Here is my controller:
public function update(User $user)
{
$data = request()->validate([
'title'=>'required',
'description'=>'required',
'url'=>'url',
'image'=>'file|mimes:jpeg,jpg,png,giv,svg'
]);
auth()->user()->profile()->update($data);
return redirect()->route('profile.show', $user->id);
}

So I ran the code to display the validation errors:
And I got this:
So I didn't have a valid URL and than I fixed it to this, now I can update:

Related

Only some values are editing with a form Laravel 8

I am using a form and I don't know why I can edit with it the values except in the fields apellido and cedula.
I am using the same logic in all the form fields so I dunno what can be causing it.
I'm gonna post the code of the view, controller, and model.
View
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Editar Médico</h1>
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('medico.update', $medico) }}">
#csrf
#method('PUT')
<div class="form-group row">
<label for="nombre" class="col-md-4 col-form-label text-md-right">{{ __('Nombre') }}</label>
<div class="col-md-6">
<input id="nombre" type="text" class="form-control #error('nombre') is-invalid #enderror" name="nombre" value="{{ $medico->nombre}}" required autocomplete="nombre" autofocus>
#error('nombre')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="apellido" class="col-md-4 col-form-label text-md-right">{{ __('Apellido') }}</label>
<div class="col-md-6">
<input id="apellido" type="text" class="form-control #error('apellido') is-invalid #enderror" name="apellido" value="{{ $medico->apellido}}" required autocomplete="apellido" autofocus>
#error('apellido')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="cedula" class="col-md-4 col-form-label text-md-right">{{ __('Cédula') }}</label>
<div class="col-md-6">
<input id="cedula" type="text" class="form-control #error('cedula') is-invalid #enderror" name="apellido" value="{{ $medico->cedula}}" required autocomplete="cedula" autofocus>
#error('cedula')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('Email') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ $medico->email}}" required autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="telefono" class="col-md-4 col-form-label text-md-right">{{ __('Teléfono') }}</label>
<div class="col-md-6">
<input id="telefono" type="text" class="form-control #error('telefono') is-invalid #enderror" name="telefono" value="{{ $medico->telefono}}" required autocomplete="telefono" autofocus>
#error('telefono')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="direccion" class="col-md-4 col-form-label text-md-right">{{ __('Dirección') }}</label>
<div class="col-md-6">
<input id="direccion" type="text" class="form-control #error('direccion') is-invalid #enderror" name="apellido" value="{{ $medico->direccion}}" required autocomplete="direccion" autofocus>
#error('direccion')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="ciudadResi" class="col-md-4 col-form-label text-md-right">{{ __('Ciudad de Residencia') }}</label>
<div class="col-md-6">
<input id="ciudadResi" type="text" class="form-control #error('ciudadResi') is-invalid #enderror" name="apellido" value="{{ $medico->ciudadResi}}" required autocomplete="ciudadResi" autofocus>
#error('ciudadResi')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="fechaNacimiento" class="col-md-4 col-form-label text-md-right">{{ __('Fecha de Nacimiento') }}</label>
<div class="col-md-6">
<input id="fechaNacimiento" type="date" class="form-control #error('fechaNacimiento') is-invalid #enderror" name="fechaNacimiento" value="{{ $medico->fechaNacimiento}}" required autocomplete="fechaNacimiento" autofocus>
#error('fechaNacimiento')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="genero" class="col-md-4 col-form-label text-md-right">{{ __('Genero') }}</label>
<div class="col-md-6">
<input id="genero" type="text" class="form-control #error('genero') is-invalid #enderror" name="genero" value="{{ $medico->genero}}" required autocomplete="genero" autofocus>
#error('genero')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Editar') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Controller
public function updateMedico(Request $request, $id) {
$medico = Persona::findOrFail($id);
$medico->fill($request->all());
if($medico ->save()) {
return redirect()->route('personaMostrarMedicos');
} else {
return redirect()->route('medico.edit');
}
}
Model
public $timestamps =false;
protected $fillable = [
'nombre',
'apellido',
'cedula',
'email',
'telefono',
'direccion',
'ciudadResi',
'fechaNacimiento',
'genero',
'estado',
'idTipoPersona'
];
I have no idea what could be wrong because only those 2 fields I mention are the ones I can't update, the other ones are fine.
In case it can helps this is also my database model
After using dd($request->all()); in the controller this is the output
What I have in the form before clicking the update button and the output of dd($request->all())
I am not sure if this is the problem, but you have 2 name="apellido", check cedula, it has that name, so that is wrong.
Remember that when you send a form, the way to get the value is going to use the name property and not the id as that is pure CSS.
Change this:
<input id="cedula" type="text" class="form-control #error('cedula') is-invalid #enderror" name="apellido" value="{{ $medico->cedula}}" required autocomplete="cedula" autofocus>
To this:
<input id="cedula" type="text" class="form-control #error('cedula') is-invalid #enderror" name="cedula" value="{{ $medico->cedula }}" required autocomplete="cedula" autofocus>
You have two inputs with the same name apellido this is why it isn't working properly.
Change this -
<input id="cedula" type="text" class="form-control #error('cedula') is-invalid #enderror" name="apellido" value="{{ $medico->cedula}}" required autocomplete="cedula" autofocus>
To this -
<input id="cedula" type="text" class="form-control #error('cedula') is-invalid #enderror" name="cedula" value="{{ $medico->cedula}}" required autocomplete="cedula" autofocus>
It should be working then.

showing user existing data in the database for different input fields in blade laravel with old value

when i try to show existing data in my input field before editing am getting this error "Trying to get property 'posts' of non-object (View: E:\Projects\htdocs\zipad\resources\views\posts\edit.blade.php)" However when i try this code below it works perfectly
working code in blade
#foreach($user->posts as $post)
<div class="col-4"> {{$post->about }}
</div>
#endforeach
edit.blade.php
<form action="/p" enctype="multipart/form-data" method="post">
#csrf
<div class="col-8 offset-2">
<div class="form-group row">
<label for="about" class="col-md-4 col-form-label text-md-right">{{ __(' post about') }}</label>
<div class="col-md-6">
<input id="about" type="text" class="form-control #error('about') is-invalid #enderror" name="about" value="{{ old('about') ?? $user->posts->about }}" required autocomplete="about" autofocus>
#error('about')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<label for="image" class="col-md-4 col-form-label text-md-right">{{ __(' post image') }}</label>
<input type="file", class="form-control-file" id ="image" name="image">
#error('image')
<div class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong> </div>
#enderror
<div class="btn btn-primary">
<button> add img post</button>
</div>
</div>
</div>
</form>
postcontroller
public function edit(User $user)
{
return view('posts.edit', compact('user'));
}
When you are iterating $user->posts it works because you are accessing each post and rendering it.
But in your posts.edit are using $user->posts->about, it doesn't know which post's about.
Solution:
You have to iterate all posts of that user like the working code you have given.
.....
#foreach($user->posts as $post)
<label for="about" class="col-md-4 col-form-label text-md-right">{{ __(' post about') }}</label>
<div class="col-md-6">
<input id="about" type="text" class="form-control #error('about') is-invalid
#enderror" name="about" value="{{ old('about') ?? $post->about }}"
required autocomplete="about" autofocus>
#error('about')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
#endforeach
....

laravel 7 eye hidden

i try to add hide eye in password in laravel project in register. i find code in codepen but when i add
<span toggle="#password-field" class="fa fa-fw fa-eye field-icon toggle-password"></span>
Register
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-12">
<h1 style="border-bottom: solid yellow;"><strong>Formulaire d'inscription</strong></h1>
<div class="card-body">
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
#csrf
<h3><strong>Vos identifiants</strong></h3>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">Adresse mail</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #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>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">Mot de passe</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #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 class="input-group-addon">
<i class="fa fa-eye-slash" aria-hidden="true"></i>
</div>
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">Confirmation mot de passe</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<h3><strong>Informations personnelles</strong></h3>
<div class="form-group row">
<label for="sex" class="col-md-4 col-form-label text-md-right">Civilité</label>
<div class="col-md-1">
<div class="form-check">
<input class="form-check-input" type="radio" name="sexe" id="exampleRadios1" value="M" checked>
<label class="form-check-label" for="exampleRadios1">
Male
</label>
</div>
</div>
<div class="col-md-1">
<div class="form-check">
<input class="form-check-input" type="radio" name="sexe" id="exampleRadios2" value="F">
<label class="form-check-label" for="exampleRadios2">
Female
</label>
</div>
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">Prénom</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #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>
<div class="form-group row">
<label for="firstname" class="col-md-4 col-form-label text-md-right">Nom</label>
<div class="col-md-6">
<input id="firstname" type="text" class="form-control #error('firstname') is-invalid #enderror" name="firstname" value="{{ old('firstname') }}" required autocomplete="firstname" autofocus>
#error('firstname')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="address" class="col-md-4 col-form-label text-md-right">Adresse</label>
<div class="col-md-6">
<input id="address" type="text" class="form-control #error('address') is-invalid #enderror" name="address" value="{{ old('address') }}" required autocomplete="address" autofocus>
#error('address')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="city" class="col-md-4 col-form-label text-md-right">Ville</label>
<div class="col-md-6">
<input id="city" type="text" class="form-control #error('city') is-invalid #enderror" name="city" value="{{ old('city') }}" required autocomplete="city" autofocus>
#error('city')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="zipcode" class="col-md-4 col-form-label text-md-right">Code postale</label>
<div class="col-md-6">
<input id="zipcode" type="text" class="form-control #error('zipcode') is-invalid #enderror" name="zipcode" value="{{ old('zipcode') }}" required autocomplete="zipcode" autofocus>
#error('zipcode')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-md-4 col-form-label text-md-right">Numéro de téléphone</label>
<div class="col-md-6">
<input id="phone" type="text" class="form-control #error('phone') is-invalid #enderror" name="phone" value="{{ old('phone') }}" required autocomplete="phone" autofocus>
#error('phone')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="birthday" class="col-md-4 col-form-label text-md-right">Date de naissance</label>
<div class="col-md-6">
<input id="birthday" type="date" class="form-control #error('birthday') is-invalid #enderror" name="birthday" value="{{ old('birthday') }}" required autocomplete="birthday" autofocus>
#error('birthday')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="pseudo" class="col-md-4 col-form-label text-md-right">Nom d'utilisateur (pseudo)</label>
<div class="col-md-6">
<input id="pseudo" type="text" class="form-control #error('pseudo') is-invalid #enderror" name="pseudo" value="{{ old('pseudo') }}" required autocomplete="pseudo" autofocus>
#error('pseudo')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="avatar" class="col-md-4 col-form-label text-md-right"></label>
<div class="col-md-6">
<input id="avatar" type="file" class="form-control #error('avatar') is-invalid #enderror" name="avatar" autocomplete="avatar" autofocus>
#error('avatar')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<div class="col-md-6 offset-4">
<div class="g-recaptcha"
data-sitekey="{{env('GOOGLE_RECAPTCHA_KEY')}}">
</div>
<span role="alert" class="invalid-feedback d-block">
#error('g-recaptcha-response')
<strong>{{ $errors->first('g-recaptcha-response') }}</strong>
#enderror
</span>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Crée mon compte
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#show_hide_password a").on('click', function(event) {
event.preventDefault();
if($('#show_hide_password input').attr("type") == "text"){
$('#show_hide_password input').attr('type', 'password');
$('#show_hide_password i').addClass( "fa-eye-slash" );
$('#show_hide_password i').removeClass( "fa-eye" );
}else if($('#show_hide_password input').attr("type") == "password"){
$('#show_hide_password input').attr('type', 'text');
$('#show_hide_password i').removeClass( "fa-eye-slash" );
$('#show_hide_password i').addClass( "fa-eye" );
}
});
});
</script>
#endsection
#section('scripts')
<script src='https://www.google.com/recaptcha/api.js'></script>
#endsection
not have something, i try to add code from there :
https://codepen.io/Qanser/pen/dVRGJvm but its not work for me i think i do something not good someone can help me add eye? Nothing appears, it's been 2 hours already I'm on it I can't take it anymore XD Someone
you can see my screen shot in this link : ibb.co/W2kQTj8
Have mercy on a noob help me XD
make sure that the page layouts.app that you extend have the link to fa eye class like one bellow
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
</head>
<body>
<i class="fa fa-eye"></i>
</body>
</html>

Sending email activation(confirmation) link when user UPDATING email address in Laravel

How can I resend my activation account email to my user once the user update the email address.
Email address should not be changed in the db until user click on the confirmation link
following is my update function for an user in my usercontroller(only the function body included)
if($request->hasFile('propic'))
{
$this->validate($request, [
'name' => ['required', 'alpha','min:2', 'max:255'],
'last_name' => ['required', 'alpha','min:5', 'max:255'],
'mobile' => ['required', 'numeric','min:9','regex:/\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|3[70]|7|1)\d{1,14}$/'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$setting->id.''],
'propic' => ['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
],$request->all());
$imageName = time().'.'.$request->propic->extension();
$request->propic->move(public_path('propics'), $imageName);
$setting->propic=$imageName;
$setting->name=$request->input('name');
$setting->last_name=$request->input('last_name');
$setting->mobile=$request->input('mobile');
$setting->email=$request->input('email');
$setting->update();
return Redirect::back()->with('success',__('sentence.User updated successfully'));
}
and following is my update user form
<form action="{{ route('settings.update',$user->id) }}" method="POST" enctype="multipart/form-data">
<div class="row mt-5">
<div class="col-sm-3">
#if($user->propic != 'user-pic.png')
<button type="submit" name="resetphoto" class="btn btn-warning pull-right">{{ __('sentence.Remove Profile Pic') }}</button>
#endif
<img src="/propics/{{$user->propic}}" alt="Profile Pic" id="profile_pic_display" class="mb-3">
<input type="file" name="propic" class="form-control">
#error('propic')
<span class="help-block" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="col-sm-9">
<!-- <form action="{{ route('settings.update',$user->id) }}" method="POST"> -->
#csrf
#method('PUT')
<div class="row">
<div class="col-md-6">
<div class="form-group field-user-firstname required">
<label class="control-label"
for="user-firstname">{{ __('sentence.First Name') }}</label>
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror"
name="name" value="{{$user->name}}" autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6">
<div class="form-group field-user-lastname required">
<label class="control-label" for="user-lastname">{{ __('sentence.Last Name') }}</label>
<input id="last_name" type="text"
class="form-control #error('name') is-invalid #enderror" name="last_name"
value="{{$user->last_name}}" autocomplete="last_name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group field-user-mobile required">
<label class="control-label" for="user-mobile">{{ __('sentence.Mobile') }}</label><br/>
<input id="mobile_1" type="tel"
class="form-control #error('mobile') is-invalid #enderror" name="mobile"
value="{{$user->mobile}}" style="min-width:398px;" autocomplete="mobile"
autofocus>
#error('mobile')
<span class="help-block" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6">
<div class="form-group field-user-email required">
<label class="control-label" for="user-email">{{ __('sentence.Email') }}</label>
<input id="email_" type="email"
class="form-control #error('email') is-invalid #enderror" name="email"
value="{{$user->email}}" autocomplete="email" >
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
</div>
<div class="row">
<div class="col-sm">
<div class="form-group pull-right">
<button type="submit"
class="btn btn-default">{{ __('sentence.Cancel') }}</button>
<button type="submit"
class="btn btn-default subscribe px-5">{{ __('sentence.Update') }}</button>
</div>
</div>
</div>
</div>
</form>
Currently i'm sending activation link to user email when the user is registering, but I'm struggling to do it when an existing user is trying to update email address..
Your question isn't entirely clear because you're working in a box here and haven't given us the full picture. If the intention is to ensure that the email cannot unless they've activated, then re-send them the activation email and confirm the new address upon activation, then you're missing some logic here.
Any way that you look at it, you need to store a reference to what the email should be somewhere. Either a column, such as pending_email on the table, or it's own table, such as pending_changes for instance.
In your boot method of your User model you can watch for the updating event and dispatch a job from there to re-send the activation email and halt the change of the email address:
public static function boot()
{
parent::boot();
static::updating(function ($model) {
if ($model->isDirty('email') && $model->activated_at === null) {
$model->email = $model->getOriginal('email');
dispatch (new SendActivationEmail($model));
}
});
}
Now you can append to that updating event to see if they're activating and have a pending_email:
static::updating(function ($model) {
if ($model->isDirty('activated_at') && $model->pending_email) {
$model->email = $model->pending_email;
$model->pending_email = null;
}
});

Removing the laravel error messages once the text field is clicked

Currently I'm showing my form validation error messages in the front end blade under the relevant field.
But the messages are there until the user resubmit the form, they wont removed.
I want to hide each error message once the user clicks on the field (or in the most user friendly way).
This is form code for my register blade,
<form method="POST" action="{{ route('register') }}">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('sentence.First Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="last_name" class="col-md-4 col-form-label text-md-right">{{ __('sentence.Last Name') }}</label>
<div class="col-md-6">
<input id="last_name" type="text" class="form-control #error('name') is-invalid #enderror" name="last_name" value="{{ old('name') }}" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('sentence.E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="text" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="moblie" class="col-md-4 col-form-label text-md-right">{{ __('Mobile') }}</label>
<div class="col-md-6">
<input id="mobile_1" type="tel" class="form-control #error('mobile') is-invalid #enderror" name="mobile" value="{{ old('mobile') }}" style="min-width: 330px;" autofocus>
#error('mobile')
<br/>
<span class="help-block" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('sentence.Username') }}</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control #error('username') is-invalid #enderror" name="username" value="{{ old('username') }}" autofocus>
#error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('sentence.Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" >
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('sentence.Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" >
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('sentence.Register') }}
</button>
</div>
</div>
</form>
I think the most user-friendly way is to have client-side validation,
Security: Server-side validation
User friendly: Client-side validation
For client-side, you can use jQuery, In your case:
<script>
jQuery( document ).ready(function() {
// event for click on input (also you can use click)
//better to change form to .yourFormClass
$('form input[type=text]').focus(function(){
// get selected input error container
$(this).siblings(".invalid-feedback").hide();
});
});
</script>
Add this code before closing body tag,(Don't forget to use the jQuery library ),
Hope it helps

Categories