try to create validator manually in Lumen. The official documentation is written:
<?php
namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
/**
* Store a new blog post.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Store the blog post...
}
}
I wrote
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController,
Validator;
class Welcome extends BaseController
{
public function index()
{
$validator = Validator::make(
['test' =>'TestValidation'],
['test' => 'required|unique:posts|max:255']
);
}
}
but Lumen returns fatal error:
Fatal error: Class 'Validator' not found in ...
I have tried to do like in Laravel 5:
use Illuminate\Support\Facades\Validator;
but then Lumen returns
Fatal error: Call to a member function make() on a non-object in
Somebody knows how to use the Validator class in Lumen? Thank you.
Validator is a facade. Facades aren't enabled by default in lumen.
If you would like to use the a facade, you should uncomment the
$app->withFacades();
call in your bootstrap/app.php file.
This is for Lumen version 5.3 (as shown in the docs):
use Illuminate\Http\Request;
$app->post('/user', function (Request $request) {
$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:users'
]);
// Store User...
});
https://lumen.laravel.com/docs/5.3/validation
Related
i got some issue on laravel with this code > for logincontroller
in database i was using char for level as called for admin and user. and the code i build like this
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
class LoginController extends Controller
{
public function index(){
return view('auth.login');
}
public function store(Request $request){
if(Auth::user()->level==admin){
return view('admin.admin-home',[
]);
}else{
(Auth::user()->level==user);
return view('user.user-home'
);
}
return back()->with('alert',[
'type' => 'danger',
'msg' => 'There is wrong in your credentials!'
]);
}
}
when to run this got error > ErrorException Attempt to read property "level" on null
hope can help for this thanks.
import class
<?php
namespace App\Imports;
use App\Excel;
use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Support\Facades\Hash;
class ExcelImport implements ToModel
{
/**
* #param array $row
*
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Excel([
'name' => $row[0],
'email' => $row[1],
'password' => Hash::make($row[2]),
]);
}
}
Controller
<?php
namespace Modules\Admin\Http\Controllers;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use App\imports\ExcelImport;
use Maatwebsite\Excel\Facades\Excel;
use DB;
use Validator;
use session;
class ImportExcelController extends Controller
{
public function store(Request $request)
{
$validator = Validator::make($request->all(),[
'file' => 'required|mimes:xls,xlsx,csv'
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator);
}else{
$file = $request->file;
$data = Excel::import(new ExcelImport, $file);
return redirect()->back()->with('message','File uploaded successfully');
}
}
}
showing
Class 'App\imports\ExcelImport' not found , line "$data = Excel::import(new ExcelImport, $file);"
Why am I getting this error still? I have imported the excel class already in the controller.
Help me to find out this issue.
Run the command: composer dump-autoload and try.
Make the this
use App\imports\ExcelImport;
To
use App\Imports\ExcelImport;
Seems like a speeling mistake.
I created a Laravel login passport API but when email is provided "null" or password is null, it's not showing validation required error. It's returning html of something?
Here is my code of controller:
<?php
namespace App\Http\Controllers\Api;
use\App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class AuthController extends Controller
{
public function login(Request $request)
{
$loginData= $request->validate([
'email'=>'required',
'password'=>'required',
]);
if(!auth()->attempt($loginData))
{
return response()->json(['message'=>'Invalid credientails']);
}
$accessToken =auth()->user()->createToken('authToken')->accessToken;
return response()->json(['user'=>auth()->user(),'access_Token'=>$accessToken]);
}
}
Try this
Also please make sure you include the class validator in controller-> use Validator;
$validator = Validator::make($request->all(), [
'email'=>'required',
'password'=>'required',
]);
if ($validator->fails())
{
$message = $validator->errors()->first();
return response()->json(['statusCode'=>200,'success'=>false,'message'=>$message], 200);
}
I need to use my custom validation rule for validating API requests.
Request Class:
This is my request validation rule.
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class LoginRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'username' => 'required',
'password' => 'required'
];
}
public function messages()
{
return [
'username.required' => 'The Username field is required',
'password.required' => 'The Password field is required'
];
}
}
API Controller:
This is my API controller and method.
use Illuminate\Http\Request;
use App\Http\Controllers\API\BaseController as BaseController;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\LoginRequest;
class LoginController extends BaseController
{
public function login(LoginRequest $request)
{
print_r($validatorMsg);
die();
}
}
Unable to get error message.
guys, I resolved the problem this is the solution:
public function login(Request $request)
{
$LoginRequest = New LoginRequest;
$validator = Validator::make($request->all(), $LoginRequest->rules(),$LoginRequest->messages());
if($validator->fails()){
return response()->json($validator->errors(), 422);
}
}
In order to display error messages in your API's, use response() method in your LoginRequest class, so you always return JSON. Something like this:
public function response(array $errors)
{
// Always return JSON.
return response()->json($errors, 422);
}
Now try to submit empty form, and you should be able to view the error message.
I have Laravel version 5.3 and i created a file createArticleRequest.php under the request folder , which looks like below:
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateArticleRequest extends Request {
public function authorize() {
return true;
}
public function rules() {
return [
'title' => 'required|min:3',
'body' => 'required',
'published_at' => 'required|date',
]
}
}
?>
In My articles controller i have the following method:
public function store(CreateArticleRequest $request) {
// $input = Request::all();
Article::create($request->all());
return redirect('articles');
}
But when i fill the form in my view and click on submit i get an error like so:
ReflectionException in Route.php line 286:
Class App\Http\Controllers\CreateArticleRequest does not exist
Why am i getting this error ??
I believe my articles Controller and my createArticlesRequest are in the same namespace so why am i getthing this error?
You should use PHP' use keyword at the top of the PHP file, so that php can find the CreateArticleRequest package Class in the right namespace like this:
namespace App\Http\Controllers;
use App\Http\Requests\CreateArticleRequest;
class Controller {
public function store(CreateArticleRequest $request) {
// $input = Request::all();
Article::create($request->all());
return redirect('articles');
}
}
Hope this helps!