I have created a simple function in App\Traits\Validate.
This function simply validates image as seen.
public function validate_image($request)
{
$check = true;
$validate = $this->validate($request, [
'image' => 'image|mimes:jpeg,png,jpg|max:2048',
]);
dd($validate);
if($validate->fails())
{
$check = false;
}
return $check;
}
Now when I access this function in Controller through
$check_image = $this->validate_image($request);
I am passing the whole request variable. But still in validate_image function I am getting null. What is that I am missing? I have other functions in Validate trait and those are working fine, but this function returning null value. Please help
And yess image is the name of the file field in form
I don't know why but try this...
first add
use Validator;
Now that you are sending $request I believe you it in array before passing to Validator
This
$data[] = $request->files;
$validate = Validator::make($data, [
'image' => 'image|mimes:jpeg,png,jpg|max:2048',
]);
Now dd() this. Hope it works.
first of all make sure that your form can send files.
<form method="POST" action="" enctype="multipart/form-data">
{!! Form::open([
'route' => '',
'method' => 'POST',
'files' => true
]) !!}
also create custom request. Do you really need validation in controller?
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CustomRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'image' => 'image|mimes:jpeg,png,jpg|max:2048',
];
}
}
Ensure that from your form you are able to handle images.
Ensure that in your opening clause you have added:
enctype="multipart/form-data"
Related
I'm have created a custom form request in my laravel 5.6 something like this:
<?php
namespace Noetic\Plugins\blog\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StorePostRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
];
}
}
When I do not place anything in rules, I get the controllers working, and when I put any rule inside suppose I put
return [
'title' => 'required',
'body' => 'required',
];
It works until it gets validated true, I mean if title and body is passed it gets validated, but when I don't send any data for title or body I'm not getting errors as response, I see the home page belonging to web middleware, I want to return the error data as response.
My controller is something like this:
public function store( StorePostRequest $request )
{
if ($request->fails()) {
return $this->errorResponse($request->errors()->all());
}
$data = $request->only('title', 'body');
$post = Post::create($data);
return response()->json(['post'=> $post ],200);
}
Help me out with these. Thanks
In your controller function you don't need to catch the validation just try with success path.
Handler will handle your validation
public function store( StorePostRequest $request )
{
$data = $request->only('title', 'body');
$post = Post::create($data);
return response()->json(['post'=> $post ],200);
}
In your Handler
use Illuminate\Validation\ValidationException;
if ($exception instanceof ValidationException)
{
return response($exception->errors())->header('Content-Type', 'application/json');
}
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
after that
protected function failedValidation(Validator $validator) {
throw new HttpResponseException(response()->json($validator->errors(), 422));
}
Is it possible to create a dynamic FormRequest validation in my function? See sample code below.
public function store(Request $request)
{
Model::create($request->all());
return redirect(url('/'));
}
What I mean is that I will change the "Request" parameter to the variable $formRequest.
My goal is that I would like to create different validation rules for a dynamic set of data of a single model.
If I could achieve this with other ways, please let me know. Thank you!
Edit:
Sample scenario:
I have a form that has fields of First Name, Middle Name and Last Name.
First Rule:
public function rules()
{
return [
'firstname' => 'required',
'middlename' => 'required',
'lastname' => 'required'
];
}
Second Rule:
public function rules()
{
return [
'firstname' => 'required',
'lastname' => 'required'
];
}
Where in the second rule only requires first and last name.
I just want to know if there are other ways of doing this rather than creating multiple store methods and adding more routes.
Skipping FormRequest and using the validate method on the $request instance can achieve this. Laracasts even has a lesson on it.
public function store(Request $request) {
$rules = [/*...*/];
$attributes = $request->validate($rules);
Model::create($attributes);
return redirect(url('/'));
}
You can create a custom request:
php artisan make:request CustomRequest
This will generate this class:
class CustomRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
];
}
}
The authorize() method will determine if the request can be validated in the first place.
The rules() method will return the validation rules for the current request.
And then in your controller function:
public function yourfunction(CustomRequest $request)
In the validation rules you can simply add the "sometimes" rule. You can find it here https://laravel.com/docs/5.7/validation#conditionally-adding-rules
public function rules()
{
return [
'firstname' => 'required',
'middlename' => 'sometimes|required',
'lastname' => 'required'
];
}
Request class
class LoginRequest extends Request
{
public function authorize() {
return true;
}
public function rules() {
return [
'EmailAddress' => 'required',
'Password' => 'required',
];
}
public function messages() {
return [
"EmailAddress.required" => trans("login.RequiredEmailAddress"),
"Password.required" => trans("login.RequiredPassword")
];
}
}
Route
Route::post('/AuthenticateUser',
array(
'uses' => 'API\Login\apiLoginController#AuthenticateUser',
'as' => 'AuthenticateUser'
)
);
Controller Action Method
I have a controller, I did so far for request class only to validate the input parameters. below is the action method
public function AuthenticateUser(LoginRequest $request) {
dd("Hello");
}
Url
localhost:85/Laravel/public/api/v1/AuthenticateUser
I am using Postman Chrome extension to test the Url. So, as we can see that in the Request class both Email Address and the password are required parameters.
When I pass both parameters value. there is not issue and everything works. When I keep the Email Address value empty...I got 404 error and here is the screenshot.
Am I missing something to get rid of 404 error when Email address is not given? I am expecting an error message to enter Email Address
Below is the working state when I pass both email and password
Solution 1:
I managed to get rid of the 404 and return a 422 by adding the following header in the request:
accept:application/json
This is not really a bug in Laravel as Taylor pointed out but a way to differentiate if it is an AJAX/API request or not.
Solution 2:
Alternatively, if you don't want the client to specify that header, you can create a middleware that will add the header accept:application/json on every API requests. Here's how:
Create a new middleware: app/Http/Middleware/ForceJsonResponse.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class ForceJsonResponse
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle(Request $request, Closure $next)
{
$request->headers->set('Accept', 'application/json');
return $next($request);
}
}
In /app/Http/Kernel.php, inside $middlewareGroups.api, specify the namespace to your newly created middleware:
protected $middlewareGroups = [
'web' => [...],
'api' => [
[...]
\App\Http\Middleware\ForceJsonResponse::class,
],
];
Finally got it working by changing the request class like below.
class LoginRequest extends Request
{
public function wantsJson() {
return true;
}
public function authorize() {
return true;
}
public function rules() {
return [
'EmailAddress' => 'required',
'Password' => 'required',
];
}
public function messages() {
return [
"EmailAddress.required" => trans("login.RequiredEmailAddress"),
"Password.required" => trans("login.RequiredPassword")
];
}
}
just added below code.
public function wantsJson() {
return true;
}
It is because you are validating directly on route handling and not matching throughs NotFoundException. You need to pass the Request to your Controller as is and do:
$this->validate($request, [
'EmailAddress' => 'required|email',
'Password' => 'required',
]);
I have a very simple Rule method in request class like below.
public function rules()
{
return [
'Subject' => 'required|max:50',
'Description' => 'required|max:500',
'DepartmentID' => 'required|integer|min:1',
'PriorityID' => 'required|integer|min:1'
];
}
Inside Controller Action method, below is the code.
private function SaveChanges(\App\Http\Requests\TicketRequest $request) {
$v = \Validator::make($request->all(), [
]);
$DepartmentAdmins = $this->getDepartmentAdmins();
//Check if department admin missing then no need to add the record
if($DepartmentAdmins == null || count($DepartmentAdmins) == 0) {
$v->errors()->add('MissingAdmins', 'Department admin missing.');
return redirect()->back()->withErrors($v->errors());
}
}
Question:
As we can see in the rule method there are 4 form fields. Is there any way to shift the check for Department Admin existence from Controller Action method to request class?
Laravel's Request has after hook that can be run after normal validation completes. This is how you can use it in your case:
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Property;
use Illuminate\Validation\Validator;
class SomeRequest extends Request
{
/**
* Get the validator instance for the request.
*
* #return Validator
*/
protected function getValidatorInstance()
{
$instance = parent::getValidatorInstance();
$instance->after(function ($validator) {
$this->validateDepartmentAdmins($validator);
});
return $instance;
}
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'Subject' => 'required|max:50',
'Description' => 'required|max:500',
'DepartmentID' => 'required|integer|min:1',
'PriorityID' => 'required|integer|min:1'
];
}
/**
* #param Validator $validator
*/
public function validateDepartmentAdmins(Validator $validator)
{
$DepartmentAdmins = $this->getDepartmentAdmins();
//Check if department admin missing then no need to add the record
if($DepartmentAdmins == null || count($DepartmentAdmins) == 0) {
$validator->errors()->add('MissingAdmins', 'Department admin missing.');
}
}
That way you won't have to do any validation in your SaveChanges controller method.
This code is used in Laravel 5.1, but I believe it will work the same in 5.2.
The Form Request Class basically has two methods. "authorize" and "rules". the best way to shift the check for Department Admin existense is to add your own custom validator(for example named "adminCountValidator") and implement your logic for checking the number of administrators there. Then use yoir newly defined validator in "rules" method like this:
public function rules()
{
return [
'Subject' => 'required|max:50',
'Description' => 'required|max:500',
'DepartmentID' => 'required|integer|min:1|adminCountValidator',
'PriorityID' => 'required|integer|min:1'
];
}
if you define a custome validation rule, you can also define the associated error message and your controller action will be much more cleaner. here is the link for defining your own custom validator
custom-validation-rules
here is a sample code for adding a custom validator within a service provider
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Validator::extend('adminCountValidator', function($attribute, $value, $parameters, $validator) {
/*
implement your getDepartmentAdmins()
function here and return true or false
*/
});
}
I am creating Rest Full Api for mobile application, I am validating request it redirects me to the login page with errors.
Here is my ApiController (I have created for all api):
use App\User as UserModel;
use App\Fb_friend as FbFriendsModel;
use App\Http\Requests\UserRequest;
class ApiController extends Controller
{
/**
* Create a new movie model instance.
*
* #return void
*/
public function __construct(UserModel $user, FbFriendsModel $fb_friends){
$this->user = $user;
$this->fb_friends = $fb_friends;
}
public function createUser (UserRequest $request) {
// some code here
}
Route:
Route::post('createUser', ['as' => 'createUser', 'uses' => 'ApiController#createUser']);
UserRequest.php:
public function rules()
{
return [
'fb_id' => 'required|unique:users',
'username' => 'required|unique:users',
'email' => 'required|unique:users',
'image' => 'required',
'device_id' => 'required',
'status' => 'required',
];
}
I have override a function Request.php for error formatting:
abstract class Request extends FormRequest
{
protected function formatErrors(Validator $validator)
{
return [$validator->messages()->toJson()];
}
}
When I try to call service via postman, it returns me error in json format but it also print the login page, I m not getting why?
If you are using Postman for testing API's, it is not necessary to override the response() in Request class, One can follow the following steps,
make return type in authorize() in your custom Request as true,
public function authorize()
{
//make it true
return true;
}
Go to headers section in your Postman and define Accept type,
Accept:application/json
Now hit the endpoint of your API and bam..working fine for me.
It has been done by override the response method in app/Http/Requests/Request.php
public function response(array $errors) {
if ($this->ajax() || $this->wantsJson() || Request::isJson()) {
$newError = [];
$newError['result'] = false;
$newError['errors'] = $errors;
// in the above three lines I have customize my errors array.
return new JsonResponse($newError, 422);
}
return $this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors);
}
We also need to use JsonResponse class at the top
use Illuminate\Http\JsonResponse;
Source: https://laracasts.com/discuss/channels/general-discussion/laravel-5-validation-formrequest