Validation errors are appearing in the same page on different divs - php

I have a page that has a form with a select menu and if there are some validation errors in that form the validation errors are shown using "#include('includes.errors')". But in this same page I have a button that when the user clicks in it it shows a modal where the user can introduce a subject, a message to send an email. In this modal I also have "#include('includes.errors')".
Issue: So the issue is that if there are validation errors in the form in the modal because the subject or messare were not filled by the user that errors appear on the modal but also on the same page above the form that has the select menu. Also if there are some validation errors in the form that has the select menu and the user opens the modal that validation erros also appear in the modal.
Do you know how to fix the issue?
Form with select menu:
#include('includes.errors')
<div class="card">
<div class="card_body">
<form method="post"
action="{{route('conferences.storeQuantities', ['id' => $confernece->id, 'slug' => $conference->slug])}}">
<ul class="list-group list-group-flush">
{{ csrf_field() }}
#foreach($registration_types as $rtype)
<li>
<span>{{$rtype->name}}</span>
<select id="rtype_{{ $rtype->id }}" data-price="{{ $rtype->price }}"
name="rtypes[{{ $rtype->name }}]">
<option value="">0</option>
#for ($i = $rtype->min_participants; $i <= $rtype-> max_participants; $i++)
<option value="{{ $i }}">{{ $i }}</option>
#endfor
</select>
<span>X {{$rtype->presentPrice()}}€</span>
</li>
#endforeach
</ul>
</form>
</div>
</div>
Modal:
<div class="modal fade bd-example-modal-lg" id="contactOrganizer" tabindex="-1"
role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Contact Organizer</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<div class="col">
#include('includes.errors')
#if($flash = session('email_sent'))
<div class="alert alert-success" role="alert">
<strong><i class="fa fa-check-circle" aria-hidden="true"></i></strong>
{{ $flash }}
</div>
#endif
<form method="post"
action="{{route('users.contactOrganizer', ['conference_id' => $conference->id])}}"
enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="subject">Subject</label>
<input type="text" class="form-control" value="{{ old('subject') }}"
name="subject"
id="subject">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" value="{{ old('message') }}"
name="message" rows="3"></textarea>
</div>
<input type="submit" class="btn btn-primary btn" value="Send"/>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
jQuery to show the moda if there are validation errors in the contact organizer form:
$(window).on('load', function () {
#if ($errors->any())
$('#contactOrganizer').trigger('click');
#endif
#if(session('email_sent'))
$('#contactOrganizer').trigger('click');
#endif
});
The errors.blade.php file:
#if ($errors->any())
<div class="alert alert-danger mt-3">
<ul>
#foreach ($errors->all() as $error)
<li class="text-danger"><strong><i class="fa fa-times" aria-hidden="true"></i></strong> {{ $error }}</li>
#endforeach
</ul>
</div>
#endif

You can acheave what you want using the named message bag like this :
In the controller you have to name your errors like this :
function storeQuantities(){ //assuming this is your controller function
$validator = Validator::make($request->all(), [
// your validation rules here :)
]);
if ($validator->fails())
{
return redirect()->back()->withErrors($validator, 'conferneceErrors');
}
}
Use the same thing for the other function :
function contactOrganizer(){ //assuming this is your controller function
$validator = Validator::make($request->all(), [
// your validation rules here :)
]);
if ($validator->fails())
{
return redirect()->back()->withErrors($validator, 'contactErrors');
}
}
In the view you can do this :
#include('includes.errors', ['errors' => $errors->conferneceErrors])
For the modal :
#include('includes.errors', ['errors' => $errors->contactErrors])

Related

Laravel 8 CRUD Edit page cannot display correctly

I am currently making laravel project using internet references and now I'm stuck at CRUD operation.
I use Laravel 8 with jetstream for auth, now ui template assets, bootstrap.
I tried several CRUD codes from different sources and everything worked except for edit page.
Index page:
Edit page:
This is my route:
Route::resource('projects', ProjectController::class);
My controller:
public function edit(Project $project)
{
return view('projects.edit', compact('project'));
}
public function update(Request $request, Project $project)
{
$request->validate([
'name' => 'required',
'introduction' => 'required',
'location' => 'required',
'cost' => 'required'
]);
$project->update($request->all());
return redirect()->route('projects.index')
->with('success', 'Project updated successfully');
}
The blade file:
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('projects.index') }}" title="Go back"> <i class="fas fa-backward "></i> </a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('projects.update', $project->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" value="{{ $project->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction"
placeholder="Introduction">{{ $project->introduction }}</textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Location:</strong>
<input type="text" name="location" class="form-control" placeholder="{{ $project->location }}"
value="{{ $project->location }}">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Cost:</strong>
<input type="number" name="cost" class="form-control" placeholder="{{ $project->cost }}"
value="{{ $project->location }}">
</div>
</div>
<div class="text-center col-xs-12 col-sm-12 col-md-12">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
I tried changing the edit blade file codes to different ones but still same. I tried different method from online for the edit function in controller but nothing change. I also tried completely different coding, different model controllers and all, still the same. Only edit page turns out that way. Anyone have any clue?

Trying to get property 'nama_member' of non-object in my Laravel project

I have trouble in my view Blade, what's wrong with my code, am I wrong to write code in the controller or in my code model was I declare?
the error said
Trying to get property 'nama_member' of non-object (View:
C:\xampp\htdocs\rezkastore\resources\views\pages\daftar_pelanggan.blade.php)
Blade/View
#extends('layouts.app')
#section('title', 'Daftar Pelanggan')
#section('content')
<div class="header bg-primary pb-6">
<div class="container-fluid">
<div class="header-body"> </div>
</div>
</div>
<div class="container-fluid mt--6">
<!-- Table -->
<div class="row">
<div class="col">
<div class="card">
<!-- Card header -->
<div class="card-header">
<div class="row align-items-center py-0">
<div class="col-lg-6 col-7">
<h6 class="h2 d-inline-block mb-0">Data Pelanggan</h6>
</div>
<div class="col-lg-6 col-5 text-right">
<button class="btn btn-icon btn-primary" type="button" data-toggle="modal" data-target="#addModal">
<span class="btn-inner--icon"><i class="fa fa-plus-circle" aria-hidden="true"></i></span>
<span class="btn-inner--text">Tambah Data</span>
</button>
</div>
</div>
</div>
<div class="table-responsive py-4">
<table class="table table-flush" id="datatable-basic">
<thead class="thead-light">
<tr>
<th width="30px">No</th>
<th>Nama Produk</th>
<th>Alamat</th>
<th>No.Telp</th>
<th>Member</th>
<th>Menu</th>
</tr>
</thead>
<tfoot>
<tr>
<th width="20px">No</th>
<th>Nama</th>
<th>Alamat</th>
<th>No.Telp</th>
<th>Member</th>
<th>Menu</th>
</tr>
</tfoot>
<tbody>
#php
$no = 1;
#endphp
#foreach($daftar_pelanggan as $pelanggan)
<tr>
<td>{{$no++ }}</td>
<td>{{ $pelanggan->nama_pelanggan }}</td>
<td>{{ $pelanggan->alamat }}</td>
<td>{{ $pelanggan->no_telp }}</td>
<td> {{ $pelanggan->diskon->nama_member }}</td>
<td>
<button data-toggle="modal" data-target="#editModal-{{ $pelanggan->id }}" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i></button>
<button class="btn btn-sm btn-danger" type="button" onclick="deletepelanggan({{ $pelanggan->id }})"> <i class="fa fa-trash"></i>
</button>
<form id="delete-form-{{ $pelanggan->id }}" action="{{ route('daftar_pelanggan.delete',$pelanggan->id) }}" method="POST" style="display: none;">
#csrf
#method('DELETE')
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- Modal Add -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title mb-0" id="addModalLabel">Tambah Data Pelanggan</h5>
</div>
<div class="modal-body">
<!-- Card body -->
<form role="form" action="{{ route('daftar_pelanggan.create') }}" method="POST">
#csrf
#method('POST')
<!-- Input groups with icon -->
<div class="form-group row">
<label for="addNamaPelanggan" class="col-md-4 col-form-label form-control-label">Nama <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="nama" placeholder="Nama Lengkap" id="addNamaPelanggan" name="addNamaPelanggan" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group row">
<label for="addAlamat" class="col-md-4 col-form-label form-control-label">Alamat <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="alamat" placeholder="Jatibarang" id="addAlamat" name="addAlamat" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group row">
<label for="addNoTelp" class="col-md-4 col-form-label form-control-label">No.Telp <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="notelp" placeholder="083XXXXXXXXX" id="addNoTelp" name="addNoTelp" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group row">
<label for="addNoTelp" class="col-md-4 col-form-label form-control-label">diskon Member <span class="text-danger">*</span></label>
<div class="col-md-8">
<select class="form-control" name="AddDiskonid" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')"">
<option disabled selected>-- Pilih Member --</option>
#foreach($diskons as $diskon)
<option value="{{ $diskon->id }}">{{ $diskon->nama_member }}</option>
#endforeach
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Tambah Data</button>
</div>
</form>
</div>
</div>
</div>
<!-- Modal edit -->
#foreach($daftar_pelanggan as $pelanggan)
<div class="modal fade" id="editModal-{{ $pelanggan->id }}" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title mb-0" id="editModalLabel">Update Data Pelanggan</h5>
</div>
<div class="modal-body">
<!-- Card body -->
<form role="form" action="{{ route('daftar_pelanggan.update', $pelanggan->id) }}" method="POST" id="editForm">
#csrf
#method('PUT')
<!-- Input groups with icon -->
<div class="form-group row">
<label for="updateNamaPelanggan" class="col-md-4 col-form-label form-control-label">Nama <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="hidden" name="id" value="{{ $pelanggan->id }}">
<input class="form-control" type="nama" value="{{ $pelanggan->nama_pelanggan }}" name="updateNamaPelanggan" required >
</div>
</div>
<div class="form-group row">
<label for="updateAlamat" class="col-md-4 col-form-label form-control-label">Alamat <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="alamat" value="{{ $pelanggan->alamat }}" name="updateAlamat" required>
</div>
</div>
<div class="form-group row">
<label for="updateNoTelp" class="col-md-4 col-form-label form-control-label">No.Telp <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="notelp" value="{{ $pelanggan->no_telp }}" name="updateNoTelp" required>
</div>
</div>
<div class="form-group row">
<label for="addNoTelp" class="col-md-4 col-form-label form-control-label">Diskon Member <span class="text-danger">*</span></label>
<div class="col-md-8">
<select class="form-control" name="diskon_id" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
<option disabled selected>-- Kategori Member --</option>
#foreach($diskons as $diskon)
<option
#if($produk->diskon_id == $diskon->id)
selected="selected"
#endif
value="{{ $diskon->id }}">{{ $diskon->nama_member }}</option>
#endforeach
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Update Data</button>
</div>
</form>
</div>
</div>
</div>
#endforeach
#include('layouts.footers.auth')
#section('scripts')
<script type="text/javascript">
function deletepelanggan(id) {
swal({
title: 'Yakin Ingin Hapus Data ini?',
text: "Data Tidak Bisa Dikembalikan Setelah Dihapus!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Ya, Hapus!',
cancelButtonText: 'Tidak',
confirmButtonClass: 'btn btn-success',
cancelButtonClass: 'btn btn-danger',
buttonsStyling: false,
reverseButtons: true
}).then((result) => {
if (result.value) {
event.preventDefault();
document.getElementById('delete-form-'+id).submit();
swal(
'Deleted!',
'Your file has been deleted.',
'success')
} else (
result.dismiss === swal.DismissReason.cancel
)
})
}
</script>
#endsection
#endsection
Controller
class DaftarPelangganController extends Controller
{
public function index()
{
$daftar_pelanggan = DaftarPelanggan::all();
$diskons = Diskon::all();
return view('pages.daftar_pelanggan', compact('daftar_pelanggan', 'diskons'));
}
public function update(Request $request, $id)
{
$update_pelanggan = DaftarPelanggan::findOrFail($id);
$update_pelanggan->nama_pelanggan = $request->updateNamaPelanggan;
$update_pelanggan->alamat = $request->updateAlamat;
$update_pelanggan->no_telp = $request->updateNoTelp;
$update_pelanggan->diskon_id = $request->diskon_id;
$update_pelanggan->save();
if ($update_pelanggan) {
Alert::success(' Berhasil Update Data ', ' Silahkan dicek kembali');
} elseif (!$update_pelanggan) {
Alert::error('Data Sudah Ada', ' Silahkan coba lagi');
}
return redirect()->back();
}
public function create(Request $request)
{
$simpan = DB::table('daftar_pelanggans')->insert([
'nama_pelanggan' => $request->post('addNamaPelanggan'),
'alamat' => $request->post('addAlamat'),
'no_telp' => $request->post('addNoTelp'),
'diskon_id' => $request->post('addDiskonid'),
]);
if ($simpan) {
Alert::success(' Berhasil Tambah data ', ' Silahkan dicek kembali');
} else {
Alert::error('data gagal disimpan ', ' Silahkan coba lagi');
}
return redirect()->back();
}
public function delete($id)
{
DB::table('daftar_pelanggans')->where('id', $id)->delete();
return redirect()->back();
}
}
Model
class DaftarPelanggan extends Model
{
use HasFactory;
protected $table = "daftar_pelanggans";
protected $primaryKey = 'id';
protected $fillable = [
'nama_pelanggan',
'alamat',
'no_telp',
'poin',
'diskon_id',
];
public function diskon()
{
return $this->belongsTo(Diskon::class, 'diskon_id');
}
}
Eager load the diskon relation, it will also help prevent the N+1 issue.
public function index()
{
$daftar_pelanggan = DaftarPelanggan::with('diskon')->get();
$diskons = Diskon::all();
//Since $diskons is required for selects where in only id and nama_member is required
//You can select the two columns only to save on memory
$diskons = Diskon::select('id', 'nama_member')->get();
return view('pages.daftar_pelanggan',compact('daftar_pelanggan','diskons'));
}
To avoid getting error when a relation/related model does not exist, when displaying the related model's in a loop in blade, we can define the relation with a default
public function diskon(){
return $this->belongsTo(Diskon::class,'diskon_id')->withDefault([
'nama_member' => 'Guest',
]);
}

my php variable is reset after open bootstrap modal

on my screen, you can see number : 24,26,33 this is the id_user when i click on "Ajouter Mission", the id are set to 24, instead of 26,33. (24 is the id to auth admin), why the id change to the authentified user when i click on modal button ?
dont comment my indian quality front end, i'll do it later :)
and there is my html :
Sorry, i paste everythig because i have no idea where this problem come from.
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
#csrf
You are in home as Admin !
#if(Session::has('succes'))
<div class="alert alert-success">
{{ Session::get('succes')}}
</div>
#endif
</br>
Liste des Utilisateurs
<table class="table table-sstriped card-body">
<thead>
<tr>
<th>Numero Matricule</th>
<th>Nom</th>
<th>Prenom</th>
<th>Rue</th>
<th>Code Postal</th>
<th>Ville</th>
<th>Adresse mail</th>
</tr>
</thead>
<tbody>
#foreach($users as $resp)
<tr>
<td>{{$resp->MATRICULE}}</td>
<td>{{$resp->NOM}}</td>
<td>{{$resp->PRENOM}}</td>
<td>{{$resp->RUE}}</td>
<td>{{$resp->CP}}</td>
<td>{{$resp->VILLE}}</td>
<td>{{$resp->email}}</td>
<td>{{$resp->ID_PERSONNELS}}</td>
<td>
#if($resp->id_role==1)
<form action="{{action('UserController#destroy', $resp['ID_PERSONNELS'])}}" method="post">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Supprimer</button>
</form>
</td>
#endif
<td>
#if($resp->id_role ==1)
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Ajouter Mission{{$resp->ID_PERSONNELS}}
</button>
#else
No Way, it is Admin
#endif
<!-- Modal -->
<div class="modal fade" data-id="$resp->ID_PERSONNELS" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Ajouter une Mission a {{$resp->ID_PERSONNELS}}{{$resp->NOM}}{{$resp->email}}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="{{route('addmission',26)}}" method="post">
#csrf
<div class="modal-body justify-content-center">
<!----------------------------------Body---------------------------------------------->
<label for="NOM" class="col-md-4 col-form-label text-md-center">{{ __('NOM') }}</label>
<div class="col-md-6">
<input id="NOM" type="text" class="form-control #error('NOM') is-invalid #enderror" name="NOM" required autocomplete="NOM" autofocus> #error('NOM')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span> #enderror
</div>
<label for="DATE_MISSION" class="col-md-4 col-form-label text-md-center">{{ __('DATE_MISSION') }}</label>
<div class="col-md-6">
<input id="DATE_MISSION" type="date" class="form-control #error('DATE_MISSION') is-invalid #enderror" name="DATE_MISSION" required autocomplete="DATE_MISSION" autofocus> #error('DATE_MISSION')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span> #enderror
</div>
<!-- <label class="col-md-4 col-form-label text-md-center" for="ID_PRATICIEN">{{ __('PRATICIEN') }}</label> -->
<!-- <select class="custom-select col-md-4 col-form-label text-md-center" id="inputGroupSelect01">
<option selected>Choose...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select> -->
<!--------------------------------End----Body------------------------------------>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
<button type="submit" class="btn btn-primary">Ajouter Mission</button>
</form>
</div>
</div>
</div>
</td>
</tr>
</tbody> #endforeach
</div>
</table>
</div>
</div>
</div>
</div>
</div>
#endsection #csrf
store controller :
public function store(Request $request, $id){
$miss = User::find($id);
$mission = new Mission;
$mission->NOM =$request->input('NOM');
$mission->ID_PERSONNELS = $miss->ID_PERSONNELS;
$mission->ID_NOTE_DE_FRAIS = 1;
$mission->DATE_MISSION = $request->input('DATE_MISSION');
$mission->save();
return redirect()->action('AdminController#index')->with('succes', 'Mission Ajoutée');
}
and to be sure, the controller for table :
public function index()
{
$id=auth()->id();
$users = User::all();
return view('homeAdmin')->with('users', $users);
}
thx for help :)
You should be using something like AngularJS for this sort of stuff.
However, I believe your issue is that all your generated modals share the same ID, causing the same one to be opened by all the buttons.
Try making the ID of the modal something like MODAL-{{ ID_HERE }} and remember to set the data-target of the button to MODAL-{{ ID_HERE }}
Do this to your button:
data-target=“#MODAL-{{ $resp->id }}”
And to your modal:
id=“MODAL-{{ $resp->id }}”
I don't know what your database structure looks like but use something unique to each row to implement this, e.g. $resp->id
Honestly though, use something like AngularJS, you wouldn’t then have to programmatically generate a modal for all of the entries, you could just fetch the data on page load, ng-repeat them all in however you want and then have a singular modal that refers to the one they clicked, look into it.

Laravel - Database store function not working

I am having trouble when inputting data and figuring out why my data is not being stored into the database. I have tried to use the resources route and my custom route code, but it seems still nothing works. Clicking submit just seems to refresh the page with no errors to show
And here is my form :
<div class="container">
<div class="row">
<div class="col-md-12">
<nav class="navbar navbar-expand-sm navbar-dark primary justify-content-between">
<div class="navbar-brand text-dark">Create a Story</div>
</nav>
<hr class="mt-3">
<form action="{{route('story.store')}}" method="post">
#csrf
<div class="row">
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="title">Title</label><br>
<input type="text" name="title" id="title" class="w-100 form-control{{ $errors->has('title') ? ' is-invalid' : '' }}" value="{{ old('title') }}" required>
#if ($errors->has('title'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('title') }}</strong></span>
#endif
</div>
</div>
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="category">Category</label><br>
<select name="category" id="category" class="w-100 form-control{{ $errors->has('category') ? ' is-invalid' : '' }}" value="{{ old('category') }}" required>
<option value="Active" selected>Select Category</option>
#foreach ($category as $item)
<option value="{{$item->id}}">{{$item->nama}}</option>
#endforeach
</select>
#if ($errors->has('category'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('category') }}</strong></span>
#endif
</div>
</div>
<div class="col col-lg-6">
<div class="form-group my-3">
<label for="thumbnail">Story Thumbnail <span class="text-muted">(Recomended size is 650 x 350 pixel)</span></label><br>
<input type="file" name="thumbnail" id="thumbnail">
#if ($errors->has('thumbnail'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('thumbnail') }}</strong></span>
#endif
</div>
</div>
<div class="col col-lg-12">
<div class="form-group mt-4">
<textarea id="text-content" name="content" class="w-100 form-control{{$errors->has('content') ? ' is-invalid' : ''}}" id="content" rows="3" value="{{old('content')}}"></textarea>
#if ($errors->has('content'))
<span class="invalid-feedback" role="alert"><strong>{{ $errors->first('content') }}</strong></span>
#endif
</div>
</div>
</div>
<div class="modal-footer pb-0">
<div class="float-right">
<button type="submit" class="btn btn-outline-primary btn-md">Publish Story</button>
</div>
</div>
</form>
</div>
</div>
The store function in controller :
public function store(Request $request)
{
$user = Auth::user();
$request->validate([
'title' => 'required|string',
'category' => 'required',
'thumbnail' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'content' => 'required',
]);
$thumbnailName = time().'.'.request()->thumbnail->getClientOriginalExtension();
$post = new Post;
$post->title = request('title');
$post->category_id = request('category');
$post->thumbnail = request('thumbnail');
$post->content = request('content');
$post->title = request('title');
$post->date = date('d-m-Y');
$post->author_id = $user->id;
$post->save();
$request->thumbnail->move(asset('storage/uploads'), $thumbnailName);
return redirect('me/stories')->with('Succes', 'Story has been published')->with('thumbnail', $thumbnailName);
}
And this is the routes:
Route::get('me/stories', 'PostController#index')->name('story');
Route::get('me/stories/create', 'PostController#create')->name('story.create');
Route::post('me/stories/store', 'PostController#store')->name('story.store');
Route::post('ck/image_upload', 'PostController#imageUpload')->name('ck.upload');
I'm not getting any error messages at all, it's just that the submission button does nothing. Thanks a lot!
Clicking submit just seems to refresh the page with no errors to show
Your Validator is doing a return to last page with the errors.
Add this snippet of code to your blade
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Also, inspect the page before submitting and go to the network tab (check preserve log at the top) and proceed with the submission, you will get more details of what happened.

How to open the modal if there is a validation error?

I have this code below, if a user is not authenticated it appears this "Login" link:
#if(!\Auth::check())
<span><a id="show_login_modal" href="javascript:void(0)">Login</a>
</span>
#endif
If the user logins with success the modal close and the user is loged in. Its working fine.
But if there is a validation error, for example if password is inscorrect, the modal is closed the user is not loged in and no error message appears. For the validaiton message appear is necessary to click in "login" to open again the modal and it apperas "These credentials do not match our records."
Do you know how to if there is a validation error open the modal without be necessary to click in "login"?
jQuery:
$('#show_login_modal').click(function(){
$('#login_modal').modal('show');
})
$('#close_login_modal').click(function(){
$('#login_modal').modal('hide');
})
Modal:
<div class="modal fade" id="login_modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Login</h5>
<button type="button" class="close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
#include('includes.errors')
<form class="clearfix" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group col-12 px-0">
<label for="inputEmail4">Email</label>
<input type="email" class="form-control" value="{{ old('email') }}" name="email" required autofocus placeholder="Email">
</div>
<div class="form-group col-12 px-0">
<label for="inputEmail4">Password
<a href="{{ route('password.request') }}" class="text-gray ml-1" style="text-decoration: underline">
<small>Recover password</small></a> </label>
<input type="password" class="form-control" name="password" required placeholder="Palavra-passe">
</div>
<button type="submit" class="btn btn-primary btn d-block w-100">Login</button>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="close_login_modal" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
errors.blade.php
#if ($errors->any())
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif

Categories