I get no message error after click on submit. Here is my view, which is shown correctly.
{!! Form::open(array('route'=>'store.invitation')) !!}
<div class="form-group">
<input type="hidden" name="iduser" value="{{ $user->id }} "/>
<select class="form-control" name="idgroup">
#foreach ($groups as $group)
<option value="{{ $group->id }}">{{ $group->name }}</option>
#endforeach
</select>
{{ csrf_field() }}
</div>
<div>
{{Form::submit('User einladen',['class' => 'btn btn-primary'])}}
</div>
{!! Form::close() !!}
This is my routes. The last one belongs to this view store.invitation.
Route::get('invitation', 'UsernameController#search')->name('search.username');
Route::post('invitation/result', 'UsernameController#result')->name('result.username');
Route::post('invitation/store', 'InvitationController#store')->name('store.invitation');
And this is my function...
public function store(Request $request)
{
$request->validate([
'idgroup' => [
'required', 'numeric', new AdminGroupRequest, new MaxInvitationAdmin
],
'iduser' => [
'required', 'numeric', new AlreadyInvitation, new AlreadyPartOfGroup, new MaxInvitationUser
]
]);
$iduser = $request->iduser;
$idgroup = $request->idgroup;
$i = new Invitation;
$i->idgroup = $idgroup;
$i->iduser = $iduser;
$i->active = 1;
$i->save();
$username = User::where('id', $iduser)->select('name')->first();
$groupname = Group::where('id', $idgroup)->select('name')->first();
return redirect()->action(
'UsernameController#search')->with('success', 'Sie haben den User ' .$username->name. ' in die Gruppe ' .$groupname->name. ' eingeladen.');
}
EDIT: Problem is the validation. But in my view I have
#if(#count(errors > 0))
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
Instead of:
#if(#count(errors > 0))
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
you should use
#if($errors->any())
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
or because you don't wrap errors in any container you could just use:
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
Related
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.
i dont know this method is working or not (Probably not)
$request->validate([
'name' => 'required',
'icons' => 'required',
]);
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
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
I have a problem with showing error messages or session messages in laravel.
Comment the bootstrap cdn, but makes no differences.
have code like this, works correctly, refreshing the view, but not displaying errors:
Controller.php
return redirect()->route('trainers.show',[$trainer])->with('status','Entrenador actualizado correctamente');
blade.php
#if (session('status'))
<div class="alert alert-success">
{{session('status')}}
</div>
#endif
Controller.php
$validatedData = $request->validate([
'name' => 'required|max: 10',
'avatar' => 'required|image',
'slug' => 'required',
'description' => 'required',
]);
blade.php
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $message)
<li>{{ $message }}</li>
#endforeach
</ul>
</div>
#endif
Regarding the session status, you can use \Session::has('status') and \Session::get('status'), like so:
#if(\Session::has('status'))
<div class="alert alert-success">
{{ \Session::get('status') }}
</div>
#endif
And on your errors, you can also use has(), like so:
#if ($errors->has())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error}}</li>
#endforeach
</ul>
</div>
#endif
I'd like to know why my bootstrap success message that's coming from controller (PostsController.php) won't appear in the view (messages.blade.php)?
I see the Bootsrap success green banner coming about but not the message itself. What could be the reason why?
Here's messages.blade.php:
#if(count($errors) > 0)
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
#if(session('success'))
<div class="alert alert-success">
{{session('session')}}
</div>
#endif
#if(session('error'))
<div class="alert alert-danger">
{{session('error')}}
</div>
#endif
Here's PostController.php:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/posts')->with('success', 'Post Created');
}
#if(session('success'))
...
{{session('session')}}
...
#endif
I think you want session('success') inside the div?
I found the code form this website http://itsolutionstuff.com/post/laravel-56-import-export-to-excel-and-csv-exampleexample.html
This is my route after editing:
Route::post('barang', 'BarangController#importExcel')->name('barang');
This is their Controller, and I just Adding on foreach :
public function importExcel(Request $request)
{
$request->validate([
'import_file' => 'required'
]);
$path = $request->file('import_file')->getRealPath();
$data = Excel::load($path)->get();
if($data->count()){
foreach ($data as $key => $value) {
$arr[] = [
'kode_barang' => $value->kode_barang,
'nama_barang' => $value->nama_barang,
'kategori_id' => $value->kategori_id,
'jumlah_barang' => $value->jumlah_barang,
'harga_satuan' => $value->harga_satuan,
'tanggal_inputan' => $value->tanggal_inputan,
'deskripsi' => $value->deskripsi,
'status' => $value->status,
];
}
if(!empty($arr)){
Item::insert($arr);
}
}
return back()->with('success', 'Insert Record successfully.');
}
And I Just Adding this on my View :
<form action="{{ route('barang') }}" class="form-horizontal" method="post" enctype="multipart/form-data">
#csrf
#if ($errors->any())
<div class="alert alert-danger">
×
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if (Session::has('success'))
<div class="alert alert-success">
×
<p>{{ Session::get('success') }}</p>
</div>
#endif
<input type="file" name="import_file" />
<button class="btn btn-primary">Import File</button>
</form>
The result show data has insert successfully, but nothing data has insert to my table.
Here my excel CSV format :
R4B6,MONITOR LENOVO,ELEKTRONIK,1,-,10/4/2018,-,Aktif
So, how to make the code right for insert into DB ?
I have used this function in controller to store the records of the users with validations. The data is not stored if the validation is not met but it doesnot show any validation error message.
public function store()
{
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if ($validation->passes())
{
User::create($input);
return Redirect::route('users.index');
}
return Redirect::route('users.create')
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
I have the model:
<?php
namespace App;
class User extends BaseModel{
protected $fillable = [
'name', 'email', 'password', 'phone'
];
protected $hidden = [
'password', 'remember_token',
];
public static $rules = array(
'name' => 'required|min:5',
'email' => 'required|email');
}
users.create view file:
#extends('layouts.user')
#section('main')
<h1>Create User</h1>
{{ Form::open(array('route' => 'users.store')) }}
<ul>
<li>
{{ Form::label('name', 'Name:') }}
{{ Form::text('name') }}
</li>
<li>
{{ Form::label('username', 'Username:') }}
{{ Form::text('username') }}
</li>
<li>
{{ Form::label('password', 'Password:') }}
{{ Form::password('password') }}
</li>
<li>
{{ Form::label('password', 'Confirm Password:') }}
{{ Form::password('password_confirmation') }}
</li>
<li>
{{ Form::label('email', 'Email:') }}
{{ Form::text('email') }}
</li>
<li>
{{ Form::label('phone', 'Phone:') }}
{{ Form::text('phone') }}
</li>
<li>
{{ Form::submit('Submit', array('class' => 'btn')) }}
</li>
</ul>
{{ Form::close() }}
#stop
Taken from Laravels Documentation
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif