In my method update() has some block of error conditions Call to a member function failed on array precisely that checks if $validations fails. I noticed that all the store() and updated() methods work correctly without the need for the conditional block. I just thought it would be nice to insert these conditions.
namespace App\Http\Controllers;
use App\Http\Requests\HunterRequest;
use Illuminate\Http\Request;
use App\Models\HunterModel;
class HunterController extends Controller {
public function store(Request $request)
{
$validations = $request->validate();
if ($validations->fails()){
return redirect()->back();
}
HunterModel::create($validations);
return redirect()->to('/');
}
public function update(Request $request, $id)
{
$validations = $request->validate();
if ($validations->fails()){ // Call to a member function fails() on array
return redirect()->back();
}
HunterModel::where('id',$id)->update($validations);
return redirect()->to('/');
}
}
$request->validate() expects an array of validation rules to validate the incoming request data against. You're not currently supplying anything so aren't actually performing any validation.
If validation is successful, the return value of that call will be an array of validated data, it is not an object and so doesn't have a fails() method (or any methods for that matter).
// $validations is an array
$validations = $request->validate([
'field' => ['required'],
]);
If validation is not successful, Laravel will automatically redirect back to the caller sending all failuer errors messages.
Therefore, the following lines are redundant and should be removed as Laravel will handle failure scenarios for you:
if ($validations->fails()){
return redirect()->back();
}
you are using validate method for the validation logic, this method return an array ...
you should use make method to check validation's fails
$validator = Validator::make($request->all(), [
'smoeFiled' => 'required|.....',
]);
you should also provide the validator with validation rules.
see Laravel doc for more details.
Related
I want to devide my controller to several service layer, validation layer, and logical layer.
But I got stuck when I want to send new variable to validation request, my schenario is, if there is a new sent it indicates for new data, and if new variable is not exists it indicates it's updating data.
Here is my Controller:
public function store(AlbumRequest $request, AlbumService $service)
{
$request->add(['new' => true])
try{
$service->store($request);
return redirect(route('admin.web.album.index'));
}catch(\Exception $err){
return back()->withInput()->with('error',$err->getMessage());
}
}
Here is my Request
class AlbumRequest extends FormRequest
{
public function rules()
{
dd($this->request->get('new')
}
}
I want to catch variable I have sent from Controller to Request. How to do that?
Thank you.
You can add new parameter in request from controller like that
$request->merge(array('new' => true));
before your request reaches your controller , it has to go through AlbumRequest class . so you have to merge that field in AlbumRequest class by using method prepareForValidation :
protected function prepareForValidation()
{
$this->merge([
'new' => true,
]);
}
add this method in your AlbumRequest class and see if it works
I am afraid you cannot do that because the incoming form request is validated before the controller method is called. Now if you want to know whether the request is for creating something new or updating something, you can do it by accessing the route parameters and method type
public function rules()
{
$rules = [
'something' => 'required',
];
if (in_array($this->method(), ['PUT', 'PATCH'])) {
//it is an edit request
//you can also access router parameter here, $this->route()->parameter('other thing');
$rules['somethingelse'] = [
'required',
];
}
return $rules;
}
You can enter the requested class as URL params.
class AlbumRequest extends FormRequest
{
public function rules()
{
dd(request()->get('new')
}
}
I am new in Laravel, what I try to achieve is very simple thing, I would like to use FormRequest provided by Laravel to do validation of the request, but I encounter some puzzles (which I am sure is easy things to solve if you are experienced in Laravel).
Here is what I tried:
I have route maps to controller:
Route::put('user/{name}', 'UserController#show');
I can get the name parameter in show function:
class UserController {
public function show($name)
{
// validtion rules to apply
...
}
}
I have validation rules to apply to the request, so I decided to create form request by php artisan make:request ShowRequest, which creates the form request class:
class ShowRequest extends FormRequest {
public function authorize()
{
return true;
}
public function rules()
{
return [
// my validation rules here
];
}
}
Since I have above request class, so I refactored the show function in controller to receive the ShowRequest .
class UserController {
public function show(ShowRequest $request)
{
// now I don't need validtion rules in this function
// but how can I access the 'name' parameter now
...
}
}
I have two questions to ask:
Inside the refactored show function, how can I now access the route parameter name ?
If we forget about the parameter is a name (please don't focus on what to validate for name, imaging it is an object or value to validate in general). How to add custom logic for handling validation error instead of using Laravel default behaviour. I want to inject code like dummy code below:
if (!$validator->pass())
{
//my custom code for handling validation failure
}
Where to put my custom code for handling validation error now? I mean I don't know where to have this logic, in controller? in the request class? how?
You still can add the parameter $name in the show() method of your controller as it's part of the routed url more than the validated form/data. (recommanded)
class UserController {
public function show(ShowRequest $request, $name)
{
//...
}
}
You can also access it from the request object
class UserController {
public function show(ShowRequest $request)
{
$request->input('name');
}
}
As for the error messages (not the exception) you can add the messages() method to your ShowRequest::class
class ShowRequest extends FormRequest
{
/**
* #return array
*/
public function messages()
{
return [
'name.required' => 'The name is required',
'name.numeric' => 'The name must be a number',
//...
];
}
}
If you instead need to validate that the name catched by the route is only composed of letter OR really exists as a field in your DB (like a slug of a post) you need to add some validation in your route declaration.
Setup a route that catches request only if it is composed of letters.
Route::get('/user/{name}', 'Controller#show')->where(['name' => '[a-z]+']);
Setup a route that catches request only if the "name" exists in DB:
User.php
Class User //..
{
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'name';
}
}
web.php
//
Route::get('/user/{user:name}', 'Controller#show');
And adapt your controller to take a user directly
class UserController {
public function show(ShowRequest $request, User $user)
{
//...
}
}
You can access the values of the Form Request using this
$validated = $request->validated();
The $validated will have all the values which had been validated by the FormRequest.
To answer your second question, if you want to throw custom validation, you can always use the following
throw ValidationException::withMessages(['name' => 'Something is wrong']);
I saw there are a few other questions in which they asked what the difference was between $this->validate and Validator::make(). They didnt really answer the conceptual question I was wondering though.
Is there a proper use for each of these? Like when to use one versus the other?
How I am currently using it is in my API classes I use a if else with $validator::make() (like below) while in my Web portion of the program I use $this->validate() (also below)
Is this a proper way to use this?
$validator::make:
public function store(Request $request)
{
$validator = Validator::make($request->all(),[
'name' => 'required',
'url' => 'required',
'isPublic' => 'required'
]);
if($validator->fails()){
return response($validator->messages(), 200);
} else {
Helpers::storeServer($request);
return response()->json([
'message'=> ['Server Stored']
]);
}
}
$this->validate:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'url' => 'required',
'isPublic' => 'required'
]);
Helpers::storeServer($request);
return redirect('dashboard')->with('success', 'Server stored');
}
Nope, they do the same exact thing two different ways. I mean that literally, $this->validate() calls the make() method on the validation class. If you look at ValidatesRequests.php, implemented by Controller.php which your controller extends.
The validate() method calls:
$validator = $this->getValidationFactory()
->make($request->all(), $rules, $messages, $customAttributes);
So it ends up using the make() method eventually. There is a difference in how it is handled, since $this->validate() calls:
if ($validator->fails()) {
$this->throwValidationException($request, $validator);
}
So using Validator::make() will allow you to handle the exception yourself, instead of $this->validate() auto throwing the validation exception for you. This is useful for doing something before your redirect. You show this in your first example, since you check if the validation fails before deciding how to handle it.
In the second example you know that if validation fails it will automatically refuse the request...
if fail any rules on $this->validate, will auto throw an error
$validator::make: you can easy handle the errors and may be you want wrap any data to array() and pass to $validator::make: for Validation
and also if you want to use FormRequest + Route Model Binding
your store() look like this
public function store(YourFormRequestRules $request)
{
Helpers::storeServer($request);
return redirect('dashboard')->with('success', 'Server stored');
}
public function update(YourFormRequestRules $request, Post $post)
{
$post->title = $request->input('title');
$post->save();
return redirect()->route('example.index');
}
thats all
Using $this->validate(): YourController extends a Controller class which uses ValidatesRequests trait and it looks like this:
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
The ValidatesRequests trait itself is made up of several methods that the base Controller gets and then your own controller and which includes:
validateWith($validator, Request $request = null)
...
validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
...
validateWithBag($errorBag, Request $request, array $rules, array $messages = [], array $customAttributes = [])
... and so on...
These methods helps makes some validation and request errors handling much convenient and in fact considering the mentioned validate() method.
When there's validation error in the request passed, it helps you handle the response without litering your controller with unneeded logic; i.e when it's an ajax call it returns a 422 response with a json body while it returns populates the error bag and fill the $errors variable for use in the blade template for non-ajax.
In summary, this only helps you incase you don't want to do manual validation by creating an instance of Validator, so to keep the developer focus on doing the real work ;)
UPDATE:
$validator = Validator::make() //creates an instance of the validator for further operations
// You can even do after the creating the instance of validator:
$this->validateWith($validator, $request)
//Lets do both steps above i.e the validation and trigger the Validation Exception if there's failure.
$this->validate($request, [...rules...], [...messages..])
Check: https://laravel.com/docs/5.6/validation#quick-writing-the-validation-logic
Validator helper makes me more happy
$validator = validator()->make(request()->all(), [
'type' => 'required|integer'
]);
if ($validator->fails())
{
redirect()->back()->with('error', ['your message here']);
}
Laravel give helper that makes developing more convince.
Let's say I'm building a small application, where a small part of it is responsible for sending an email when a contact form is submitted. I only want to do that if the contact form passes some simple validation.
In the following controller the ContactRequest $request parameter is unused inside the method, although Laravel used the type-hinting to automatically apply the ContactRequest logic to the request.
Is it possible to achieve the same thing without leaving an unused variable in the controller method?
// Route
Route::post( 'contact', 'PageController#submitContactForm' );
// PageController
public function submitContactForm( ContactRequest $request ) {
sendContactFormEmail();
return redirect()->back();
}
// ContactRequest
public function authorize() {
return hasNotSubmittedContactFormRecently();
}
public function rules() {
return [ 'message' => 'required' ];
}
Yes, you can write your controller method like so:
// PageController
public function submitContactForm()
{
app()->make(ContactRequest::class);
sendContactFormEmail();
return redirect()->back();
}
and it will have same effect. However for me it's better to use it as you used it before.
Also probably you somehow use data you receive, so it might be more reasonable to use it like this:
sendContactFormEmail($request);
instead of probably injecting request into sendContactFormEmail method.
I wish to create a Request in a Laravel 5.1 application which has a specific set of rules based on the function that calls it.
For example, say, there are two functions in UsersController namely, login() and register(). The login() function requires only two inputs- username and password, whereas the register() function requires three inputs- username, password and email among other constraints. How can I create a single Request, call it UserRequest, that can handle the rules corresponding to both the above functions based on whichever makes the Request?
I am not sure if what I'm attempting could be done, or whether it is a good practice. Please suggest regarding the same as well.
You have a lot of flexibility since you can do whatever in rules() method, you just need a way to distinguish who is using the Request.
For this example, i would simply use the route() method(it tells you what route was called), you can do something like this:
class MyRequest extends Request {
/*
* Request rules
*/
protected $rules = [
// login rules
'login_route' => [
'login' => 'required',
],
// register rules
'register_route' => [
'login' => 'sometimes',
],
// depends if you need it
'default' => [
'login' => 'sometimes',
]
];
public function authorize()
{
return true; // or whatever
}
public function rules()
{
// where did this request come from?
$route = $this->route();
if(array_key_exists($route, $this->rules))
return $this->rules[$route];
return $this->rules['default'];
}
}
There are other ways, it depends on your problem, you could check the request method (GET, POST..) using getMethod() or check a segment, or instanciate an object on construct (dependency injection) to check if a user is logged in or not for example, really, it depends.
However, if the use case is complex, it is better to seperate in two requests.
Hope this helps.
You mean form requests. Before your method will be excecuted, the validator checks the rules. If not valid, the not-valid messages array will be returned with http status 422. See example below from https://mattstauffer.co/blog/laravel-5.0-form-requests
<?php namespace App\Http\Controllers;
use App\Http\Requests\FriendFormRequest;
use Illuminate\Routing\Controller;
use Response;
use View;
class FriendsController extends Controller
{
public function getAddFriend()
{
return view('friends.add');
}
public function postAddFriend(FriendFormRequest $request)
{
return Response::make('Friend added!');
}
}
And then your form request class:
<?php namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Response;
class FriendFormRequest extends FormRequest
{
public function rules()
{
return [
'first_name' => 'required',
'email_address' => 'required|email'
];
}
public function authorize()
{
// Only allow logged in users
// return \Auth::check();
// Allows all users in
return true;
}
// OPTIONAL OVERRIDE
public function forbiddenResponse()
{
// Optionally, send a custom response on authorize failure
// (default is to just redirect to initial page with errors)
//
// Can return a response, a view, a redirect, or whatever else
return Response::make('Permission denied foo!', 403);
}
// OPTIONAL OVERRIDE
public function response()
{
// If you want to customize what happens on a failed validation,
// override this method.
// See what it does natively here:
}
}
You can make different 'standalone' form requests and use them for your controller methods.
Official documentation here: http://laravel.com/docs/master/validation#form-request-validation