The following is my blade :
<form action="{{route('ans1.eval')}}" method="post">
<br>
<input type="radio" name="evaluate" class="evaluate" value=10> 1
<input type="radio" name="evaluate" class="evaluate" value=15> 1.5
<input type="radio" name="evaluate" class="evaluate" value=20> 2
<input type="radio" name="evaluate" class="evaluate" value=25> 2.5
<input type="radio" name="evaluate" class="evaluate" value=30> 3
<button type="submit" class="btn btn-primary" align="right">Evaluate Answer</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
The following is my route :
Route::post('/evaluateans', [
'uses' => 'AnswerController#postEvaluateAns',
'as' => 'ans1.eval',
'middleware' => 'auth'
]);
The following is my validation :
public function postEvaluateAns(Request $request)
{
$this->validate($request, [
'evaluate' => 'required'
]);
}
The following is the error when no evaluation is selected :
MethodNotAllowedHttpException in RouteCollection.php line 218
From the laravel docs on validation
If the validation rules pass, your code will keep executing normally;
however, if validation fails, an exception will be thrown and the
proper error response will automatically be sent back to the user. In
the case of a traditional HTTP request, a redirect response will be
generated.
When your validation fails it redirects back with a GET method (a redirection uses the GET method) but if you show the form from a route that is not a GET one it throws this error.
So you have to show the form with a GET route.
As alternative you can manually create your validator so you can choose the redirect GET route in case of failed validation, e.g.:
public function postEvaluateAns(Request $request)
{
$validator = Validator::make($request->all(), [
'evaluate' => 'required'
]);
if ($validator->fails()) {
return redirect('failed_validation_GET_route')
->withErrors($validator)
->withInput();
}
return redirect()->route('success_GET_route')
->with('status', 'Success!');
}
Related
I used 5.4 and I've an index action in convert controller which shows the form and then have another action calculate in the convert controller. So the form has from-currency, amount, to-currency input and all of them are required.
Here's the validation I've for calculate action:
$this->validate(request(), [
'from_currency' => 'required|min:3|max:3|alpha',
'to_currency' => 'required|min:3|max:3|alpha',
'amount' => 'required|numeric',
]);
If the validation failed I want when showing the errors and the form it will prepopulate the input already.
Is there like a function I can use for Request ? I know how to get the domain/path inside blade like Request::root() and I also tried Request::input('from_currency) in the view but not work.
I even tried to set the view data like 'from_currency' => request('from_currency') and it's blank. Any idea?
When you are validating your form your request if it fail you can redirect to the same page with all the input which was submited
$validator = Validator::make($request->all(), [
'from_currency' => 'required|min:3|max:3|alpha',
'to_currency' => 'required|min:3|max:3|alpha',
'amount' => 'required|numeric',
]);
if ($validator->fails()) {
return redirect('index')
->withErrors($validator)
->withInput();
}
and in your blade view you can show the old value by ussing the old helper like this
<input type="text" name="from_currency" value="{{ old('from_currency') }}">
<input type="text" name="to_currency" value="{{ old('to_currency') }}">
<input type="text" name="amount" value="{{ old('amount') }}">
Try this
In your blade file, make sure your inputs have this:
<input type="text" ... value="{{ old('from_currency') }}" ... >.
Then in your controller...
if($validation->fails()) {
return redirect()->back()->withInput();
}
You can also user Validate instead of Validator::make.
eg
$this->validate($request, [
'question' => "required|min:10|max:100",
'answer' => "required|min:20|max:300",
'rank' => "required|numeric|gt:0|lt:100",
]);
Then in your form use
<input type="text" class="form-control" id="question" name="question" value="{{ old('question') }}">
This will automatically redirect back with input if the validator fails.
This way, you DO NOT have to include
if($validation->fails()) {
return redirect()->back()->withInput();
}
I'm new to laravel and have been going through this code for hours, yet I'm unable to figure out what's really wrong.
I have this form:
<form method="POST" action="{{ action('School1Controller#forgot_password') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="text" class="form-control" name="matricule" placeholder="Matricule No. or name" >
<input type="email" class="form-control" name="inst_email" placeholder="Password">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
which submits to this method..
public function forgot_password(Request $request){
$this->validate($request, [
'matricule' => 'required',
'inst_email' => 'required'
]);
$input = $request->all();
$student = User::where('matricule', $input['matricule'])->where('inst_email', $input['inst_email'])->get();
// dd($student);
if (empty($student)){
Session::flash('message', 'We have no such user in our records.');
return redirect()->back();
}else{
Session::flash('message', 'Student found. We will reset your password soon.');
return redirect()->back();
}
}
..through this route..
Route::post('/forgot_password', ['as' => 'forgot_password', 'uses' => 'School1Controller#forgot_password']);
When it queries the database, it returns the student properly and I can display it using dd($student). In cases when the student is unavailable, it also displays an empty array. Now the problem is, the message that keeps displaying is that which says student is found regardless whether the $student array holds any student or not. Is there a problem with my empty() function or what?
I'm trying to change the status of a database record with just a button click so far I have this:
view
<td>
<a class="btn btn-small btn-warning" href="{{ URL::to('brands/'.$value->BrandID.'/archive') }}">Archive </a>
</td>
controller
public function archive($id)
{
$rules= array ('BrandName' =>'required | max:20',);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails())
{
return Redirect::to('brands.view')
->withErrors($validator);
} else {
DB::table('tbl_brands')->where('BrandID' , $id)
->update(
array
(
'Status' => 'Archived'
));
Session::flash('message','Successfully Archived!');
return Redirect::to('brandsview');
}
}
and the route
Route::put('brands/{id}/archive', array('as' => 'Brandarch', 'uses'=>'BrandsController#archive'));
and my error what method exception. I scrolled down a bit and saw that in the errors, the http request is 'get' which I know should be 'put' any ideas on how to properly execute this?
You will need to change your hyperlink to a submit form in a form with hidden field with name _method, only this way you can control HTTP method used.
For example:
<form action="{{ URL::to('brands/'.$value->BrandID.'/archive') }}" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" value="Archive">
</form>
I am writing an application in Laravel 5.1. According to the documentation I can authenticate and validate my login forms using their AuthController.
The only field I want to validate is a code-input field.
<form method="POST">
<div class="form-group">
<label for="code-input">Code</label>
<input type="text" name="code-input" class="form-control" id="code-input" placeholder="Type your code here">
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<button type="submit" class="btn btn-default">Submit</button>
</form>
However, whenever I set my validation method in the AuthController to check only that field and submit an empty form I get an error that the email field is required and the password field is required. I mean, what?
protected function validator(array $data)
{
return Validator::make($data, [
'code-input' => 'required|max:255'
]);
}
My routes are also working correctly:
Route::get('/', 'Auth\AuthController#getLogin');
Route::post('/', 'Auth\AuthController#postLogin');
Route::get('/logout', 'Auth\AuthController#getLogout');
Does anybody know what is going on?
The validator method is just for creating users (registration). Login is all handled by the AuthenticatesUsers trait postLogin method, which requires email and password.
There are ways to change the "email" requirement easily, but to get rid of both fields and replace with a code, you'll have to write yourself a new postLogin method.
Did you check the postLogin function at AuthenticatesUsers.php? It could have a line of codes something like this which results the validation errors:
$this->validate($request, [
$this->loginUsername() => 'required', 'password' => 'required',
]);
I am getting 'Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException' as response when I am trying to check if the validator works. So when after I click the save button. This is my code :
in my view 'addMedia-partial.blade.php' :
{{ Form::open() }}
<input type="text" name="videoUrl" placeholder="enter youtube video url" />
<input type="text" name="videoTitle" placeholder="enter video title" />
<input type="text" name="videoDescription" placeholder="enter video description" />
<input type="submit" value="Save">
{{ Form::close() }}
in my routes.php :
Route::get('/guide/dashboard/media/add/storeVideo', array('as' => 'guide-video-add','uses' => 'MediaController#getNewVideo'));
Route::post('/guide/dashboard/media/add/storeVideo', array('uses' => 'MediaController#postNewVideo'));
in my controller MediaController.php :
public function postNewVideo()
{
$rules = array('videoUrl' => 'required', 'videoTitle' => 'required|min:5', 'videoDescription' => 'required|min:20');
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
//return Redirect::route('guide-media-add')->withErrors($validator);
//gaat niet omdat dit in een andere controller is ??
return Redirect::route('guide-video-add')->withErrors($validator);
}
}
public function getNewVideo()
{
return View::make('guide.dashboard.dashboard')->nest('child', 'guide.dashboard.addMedia-partial');
}
Please check with Firebug which URL and which type of request are you sending.
Secondly please execute php artisan route:list This will show you all the routes and which method they accept.
There must be mismatch between defined and actual requests. You must be sending POST to the route which only accepts GET.