Laravel validation not showing the default error messages - validation

I am working on a laravel project and I am having some issues with the validation.
The default error messages dont appear instead I get to see the validation requirements like this: http://gyazo.com/681e9d8e2e176a29d90db041354f7177
this is my code:
routes.php (I put all the code in here for now)
Route::filter('checkLogin', function()
{
if(Input::GET('email') != ""){ //register
$rules =
array(
'username' => 'required|max:64|min:3|unique:users',
'password' => 'required|max:64|min:6',
'fname' => 'required|max:255|alpha',
'lname' => 'required|max:255|alpha',
'email' => 'required|max:255|email',
'phone' => 'max:24|min:9',
'zip' => 'required',
'street' => 'required|max:255|alpha',
'housenumber' => 'required|max:6|numeric',
'country' => 'required',
'avatar' => 'max:32'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()) {
return Redirect::to('/')->withInput()->withErrors($rules);
}
}
});
this is how the code from the view:
<div class="fields">
<div class="field">
<i class="fa fa-user"></i>
{{ Form::text('username', null, ['placeholder' => 'Username', 'tabindex' => 1]) }}
{{ $errors->first('username') }}
</div>
<div class="field">
<i class="fa fa-lock"></i>
{{ Form::password('password', ['placeholder' => 'Password', 'tabindex' => 2]) }}
{{ $errors->first('password') }}
</div>
</div>

When validation fails, you are returning your $rules as the errors. Change this line:
return Redirect::to('/')->withInput()->withErrors($rules);
to this:
return Redirect::to('/')->withInput()->withErrors($validator);

Related

Laravel 8.0 Error "Action App\Http\Controllers\SkladController not defined."

This is my problem in SkladController. I want to fetch data from the DB but it does show me anything because I have that error in the picture.
SkladController
class SkladController extends Controller
{
public function index()
{
$sklads = Sklad::all();
return view('sklads.index')->with('sklads', $sklads);
}
public function create()
{
return view('sklads.index');
}
public function store(Request $request)
{
$this->validate($request, [
'datle' => 'required',
'mandle' => 'required',
'marcipan' => 'required',
'orechy' => 'required',
]);
//vytvorit v sklade
$sklads = new Sklad;
$sklads->datle = $request->input('datle');
$sklads->mandle = $request->input('mandle');
$sklads->marcipan = $request->input('marcipan');
$sklads->orechy = $request->input('orechy');
$sklads->save();
return redirect('/sklad')->with('success', 'Uložené');
}
}
index.blade.php
<h3> Príjem v sklade</h3>
<br>
{!! Form::open(['action' => 'App\Http\Controllers\SkladController', 'method' => 'POST']) !!}
<div class="row">
<div class="form-group col-md-2">
{{ Form::number('datle', '', ['class' => 'form-control', 'placeholder' => 'Ďatle']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('mandle', '', ['class' => 'form-control', 'placeholder' => 'Mandle']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('marcipan', '', ['class' => 'form-control', 'placeholder' => 'Marcipán']) }}
</div>
<div class="form-group col-md-2">
{{ Form::number('orechy', '', ['class' => 'form-control', 'placeholder' => 'Orechy']) }}
</div>
<div class="form-group col-md-2">
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
</div>
</div>
{!! Form::close() !!}
#if(count($sklads) > 0)
#foreach($sklads as $sklad)
<br>
{{$sklad->datle}} {{$sklad->mandle}} {{$sklad->marcipan}} {{$sklad->orechy}}
#endforeach
#else
<p>nenasli sa zaznamy</p>
#endif
web.php
Route::resource('/sklad', App\Http\Controllers\SkladController::class)
->except(['create', 'store', 'update', 'destroy']);
The issue is the action in
{!! Form::open(['action' => 'App\Http\Controllers\SkladController', 'method' => 'POST']) !!}
It should be
'action' => 'App\Http\Controllers\SkladController#index'
Usually you'd use the store method but you only don't have it according to your route declaration.
or you could use route instead.
{!! Form::open(['route' => 'sklads.index', 'method' => 'POST']) !!}

Laravel - Validation Message not showing on blade

I'm having trouble on showing the validation error messages. I tried the other similar questions but doesn't work for me. Please help me.
This is my Controller.blade.php
public function store(Request $request)
{
$this->validate($request,[
'surname'=>['required', 'string', 'max:255'],
'firstname'=>['required', 'string', 'max:255'],
'birthday'=>'required',
'pofbirth'=>['required', 'string', 'max:255'],
'gender'=>['required', 'numeric'],
'address'=>['required', 'string', 'max:255'],
'city'=>['required', 'string', 'max:255'],
'district'=>['required', 'string', 'max:255'],
'cap'=>['required', 'string', 'max:255'],
'cstatus'=>['required', 'string', 'max:255'],
'conjugated'=>['required', 'string', 'max:255'],
'cellphone'=>['required', 'numeric'],
'email'=>['required', 'string', 'email', 'max:255'],
]);
This is my message.blade.php
#if (count($errors) > 0)
#foreach ($errors->all() as $error)
<p class="alert alert-danger">{{ $error }}</p>
#endforeach
#endif
#if (session()->has('message'))
<p class="alert alert-success">{{ session('message') }}</p>
#endif
This is my blade.php
#include('includes.navbar')
<div class="col" style="background-color: #ffffff;">
#include('includes.message')
<form method="POST" action="{{ route('app.store')}}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="container shadow" style="background-color: rgb(248,249,250);">
<div class="row">
<div class="col">
...
...
...
Thank you so much!
In your method, you have no any session key named message , but there are no any key named message in your validation :
#if (session()->has('message'))
<p class="alert alert-success">{{ session('message') }}</p>
#endif
It would be firstname, surname, gender, age etc (which key you are using in validation). Like this :
#if($errors->has('firstname'))
<p class="alert alert-success">{{ $errors->first('firstname') }}</p>
#endif
Get all error message :
#if ($errors->any())
#foreach ($errors->all() as $error)
<div>{{$error}}</div>
#endforeach
#endif
Change your validation in this way :
$validatedData = $request->validate([
'surname' => 'required|string|max:255',
'firstname' => 'required|string|max:255',
'birthday' => 'required',
'pofbirth' => 'required|string|max:255',
'gender' => 'required|numeric',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'district' => 'required|string|max:255',
'cap' => 'required|string|max:255',
'cstatus' => 'required|string|max:255',
'conjugated' => 'required|string|max:255',
'cellphone' => 'required|numeric',
'email' => 'required|string|email|max:255',
]);

Email Sending and Validation Failing in Laravel 5.4

I am creating a contact form using bootstrap3 build on Laravel 5.4. When I click the submit button, I expect an email to be sent to my inbox or if there are errors they should be validated on the back end and errors displayed at the top of the form. I am using Laravel collective to build the form, when I fill the form and click the submit button, the page only reloads and no validation happens or in case of correct input no email is sent. Please assist?
Form Section
<div class="col-sm-4 wow animated fadeInLeft">
<div id="success" class="col-sm-12">
#if(Session::has('success'))
<span class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</span>
#endif
#if(count($errors) > 0)
<span class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</span>
#endif
</div>
{!! Form::open(array('route' => 'index.post', 'method' => 'POST','class' => 'contact-form')) !!}
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => ''))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => ''))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Routes File
Route::post('/', 'PagesController#postIndex') ->name('index.post');
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
PagesController
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#pwebk.com');
});
Session::flash('success', 'Hello '.$name.', Your Form was successfully sent');
return redirect()->route('pages.index');
}
public function getIndex(){
return view('pages.welcome');
}

Alert Message not showing in Laravel 5.4

I have got a contact form that is build using Laravel 5.4, parsley.js and Bootstrap 3, it works fine but it does not display a success message at the top of the form on successful delivery of a message or display any errors in case there is an error. Please assist?
Contoller
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#kapsol.com');
});
Session::flash('success', 'Hello $name, Your Form was successfully sent');
return redirect()->route('pages.index');
}
Index.blade.php
<div class="col-sm-4">
{!! Form::open(array('route' => 'index.post', 'class' => 'contact-form', 'data-parsley-validate' => '')) !!}
<div id="success">
<div class="col-sm-12">
#if(Session::has('success'))
<div class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</div>
#endif
#if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</div>
#endif
</div>
</div>
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => '', 'minlength' => '10'))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => '', 'type' => 'email'))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input', 'minlength' => '100')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Route
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
Route::post('/', 'PagesController#postIndex') ->name('index.post');
You are redirecting to the same route your form is posting. I assume that the route that render the view is a GET route and have another name, you should redirect to that route or the route that renders index.blade.php
This is why you have the with() method to chain the route() method on. So rather than use Session:flash(), you can simply add a with() method that flashes that message to the session for the next request:
return redirect()->route('pages.index')->with('success', 'Hello'. $name.', Your Form was successfully sent');
Or rather if your form was originally from the index page, then you simply don't have to remember the name of the route, simply use back() helper method, i.e:
return back()->with('success', 'Hello'. $name.', Your Form was successfully sent');

Method name must be a string error in laravel 5.2

Im getting this error when try to insert data into databse in laravel 5.2.
Check out through this code and find what i did as error
Controller.php
public function store(Request $request){
$this->$request([
'title'=>'required',
'description' => 'required'
]);
Item::create($request->all());
return redirect()->route('itemCRUD.index')
->with('success', 'Item created Successfully');
}
create.php (short code)
{!! Form::open(array('route' => 'itemCRUD.store','method'=>'POST')) !!}
{!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}
{!! Form::textarea('description', null, array('placeholder' => 'Description','class' => 'form-control','style'=>'height:100px')) !!}
<button type="submit" class="btn btn-primary">Submit</button>
{!! Form::close() !!}
please help me to find out the error
Change:
$this->$request([
'title'=>'required',
'description' => 'required'
]);
to
$this->validate($request, [
'title'=>'required',
'description' => 'required'
]);
Hope this helps!

Resources