Laravel unique validation with Route parameter - validation

I'm a newbie in PHP, so now i need to validate model with static variable.
This is what i have
class Setting extends Model {
protected $table = 'settings';
public static $rules = [
'skey' => 'required|unique:table,id,' . Route::input('settings')
];
}
It throws following error : syntax error, unexpected '.', expecting ']'
Ok, I understand that can not used in declaring variable.
Now, this is my question:
How can I done this with Illuminate\Http\Request, I dont want to create a new SettingRequest that can use easier.
I also dont want use in store or update method in controller. I want to use this way in both 2 method create/update.
In PHP, anyway to create setter or getter as C#.

You can't do it like this as Luis said.
I assumed that you're using L5. Better practise is using a Request class.
<?php
namespace App\Http\Requests;
class SettingRequest extends Request
{
/**
* 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 [
'skey' => 'required|unique:table,id,' .$this->input('settings')
];
}
}
after that you can use the SettingRequest class as a method parameter in your controller like this:
public function update(SettingRequest $request) {}
public function create(SettingRequest $request) {}

You can't do it in that context. Try to do it inside a method:
public static function getRules()
{
return [
'skey' => 'required|unique:table,id,' . Route::input('settings')
];
}

Related

How to validate images array type using rule object with custom message in Laravel

Actually, I tried to create rule object which is able to validate every image type in array of images and not only enough but also, I must to show custom message in override message function in rule object.
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class ImagesArray implements Rule
{
/**
* Create a new rule instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* #param string $attribute
* #param mixed $value
* #return bool
*/
public function passes($attribute, $value)
{
return [$attribute => 'mimes:jpeg,jpg,png' ];
here i need to validate these file types.
}
/**
* Get the validation error message.
*
* #return string
*/
public function message()
{
return 'The validation error message.';
here, I need to show my custom messgae.
}
}
You should use Request.
For example, create q request class: php artisan make:request MyRequest.
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class MyRequest 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' => 'mimes:jpeg,jpg,png',
];
}
public function messages()
{
return [
'image.mimes' => 'This image is not supported.',
];
}
}
In your controller import class MyRequest and in the method use MyRequest
e.g:
public function store(MyRequest $request)
{ // your code
}
Let me know if that was helpful. Thanks!
When validating arrays or nested parameters, you should use . in your rules access a specific array index. but if you want to apply a rule to every index on that array, you can use .*.
$validator = Validator::make($request->all(), [
'image.*' => 'mimes:jpeg,jpg,png',
], [
'image.*' => 'Invalid file type.',
]);
Or if you're using Request Forms
public function rules(){
return [
'image.*' => 'mimes:jpeg,jpg,png',
];
}
public function mesages(){
return [
'image.*' => 'Invalid file type.',
];
}
For more info, see Laravel's Documentation on Validation Arrays

My controller is ignoring my custom validation rule

I need a custom validation rule for my application. As a proof of concept I created a simple validation rule. For testing purposes, the passes method returns false. I added the validation to my controller, but even though the validator returns false, the controller proceeds as though the validation succeeded. I used the debugger to confirm that the passes method is indeed being hit and is returning false.
Why am I doing wrong?
Here's my rule class:
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class UniqueAgent implements Rule
{
protected $id;
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* UniqueAgent constructor.
*/
public function __construct($id)
{
$this->setId($id);
}
public function passes($attribute, $value)
{
// TODO: Implement passes() method.
return false;
}
public function message()
{
// TODO: Implement message() method.
}
}
Here's the controller code:
public function update(Request $request, $id)
{
// UniqueAgent rule returns 'false'
$validateData = $request->validate([
'agency' => [
'required',
new UniqueAgent($id)
]
]);
$agent = Agent::findOrFail($id)
$agent->agency = $request->input('agency');
$agent->save();
}
Thanks
Problem solved. For anyone interested, I hadn't added a return value to the message method of the UniqueAgent class. I added a return "some string" and now the controller redirects the client to view and displays the error message.
In my opinion, this is a poor design. If not returning a message string causes the class not to perform as intended, it shouldn't just fail quietly. In my opinion, it would be better for if the controller simply did what it does when the validation fails (i.e., redirects to the view), with or without an error message.

Laravel unique field validation

I have a Product model with a text input field for the product number. In my Laravel application I validate this field to be unique to that specific user. So two users can have a same product number, but one user cannot have duplicate. So far the validation rules work when adding new products:
'product_no' => 'nullable|unique:products,product_no,NULL,id,user_id,' . auth()->user()->id
However, when editing the same product, the validation fails. Probably because it already exists. I am not sure how to exclude the existing ID in the validation. Any ideas?
Example as requested
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class Request1 extends FormRequest
{
private $rules;
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
public function __construct()
{
parent::__construct();
$this->rules = [
'password' => [
'nullable',
]
];
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return $this->rules;
}
}
And the one with unique looks like this then
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class Request2 extends Request1
{
public function __construct()
{
parent::__construct();
$this->rules[] = 'unique:products,product_no,NULL,id,user_id,' . auth()->user()->id';
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return $this->rules;
}
}

Declaration of App\Http\Requests\Comment\CreateRequest::authorize() should be compatible with App\Http\Requests\Request::authorize()

Hello im creating i comment system for my laravel app but i get this error when i try to comment.. basically when a user is not logged in and he tries to comment and submit it, it should redirect him to the login page, so my CreateRequest file is this
<?php
namespace App\Http\Requests\Comment;
use App\Http\Requests\Request;
use App\Repositories\Image\ImageRepository;
class CreateRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* #param ImageRepository $image
* #return bool
*/
public function authorize(ImageRepository $image) {
$image = $image->getById($this->route('id'))->first();
if (!$image) {
return false;
}
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules() {
return [
'comment' => ['required', 'min:2'],
];
}
/**
* #return \Illuminate\Http\RedirectResponse
*/
public function forbiddenResponse() {
return redirect()->route('login');
}
}
and my App\Http\Requests\Request file is this
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
public function authorize() {
return true;
}
}
if i remove
public function authorize() {
return true;
}
then comments works and if user is not logged in the user gets redirected to the login page but then my login is not working and i get Forbidden when i try to login
im developing my app on top of this project https://github.com/laravelish/EasyAdmin
Hope someone can help me
You've defined an authorize method in your abstract Request class:
public function authorize() {
return true;
}
Note it doesn't take any parameters. Now if you extend this abstract class (like you are doing with CreateRequest) and you write an authorize method, you must use the same signature. Specifically: authorize can't take any parameters, because it doesn't in the abstract class.
If you were to remove the authorize method from your abstract class, this specific error would go away. However I'm not sure that would solve your problem. I don't think Laravel provides dependency injection for the authorize method, you can't just inject your repository like that.
This is one way to fix both issues:
public function authorize() {
$image = app(ImageRepository::class)->getById($this->route('id'))->first();
...

Laravel 5 how to validate route parameters?

I want to validate the route parameters in the "form request" but don't know how to do it.
Below is the code sample, I am trying with:
Route
// controller Server
Route::group(['prefix' => 'server'], function(){
Route::get('checkToken/{token}',['as'=>'checkKey','uses'=> 'ServerController#checkToken']);
});
Controller
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
class ServerController extends Controller {
public function checkToken( \App\Http\Requests\CheckTokenServerRequest $request) // OT: - why I have to set full path to work??
{
$token = Token::where('token', '=', $request->token)->first();
$dt = new DateTime;
$token->executed_at = $dt->format('m-d-y H:i:s');
$token->save();
return response()->json(json_decode($token->json),200);
}
}
CheckTokenServerRequest
namespace App\Http\Requests;
use App\Http\Requests\Request;
class CheckTokenServerRequest extends Request {
//autorization
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'token' => ['required','exists:Tokens,token,executed_at,null']
];
}
}
But when I try to validate a simple url http://myurl/server/checkToken/222, I am getting the response: no " token " parameter set.
Is it possible to validate the parameters in a separate "Form request", Or I have to do all in a controller?
ps. Sorry for my bad English.
For Laravel < 5.5:
The way for this is overriding all() method for CheckTokenServerRequest like so:
public function all()
{
$data = parent::all();
$data['token'] = $this->route('token');
return $data;
}
EDIT
For Laravel >= 5.5:
Above solution works in Laravel < 5.5. If you want to use it in Laravel 5.5 or above, you should use:
public function all($keys = null)
{
$data = parent::all($keys);
$data['token'] = $this->route('token');
return $data;
}
instead.
Override the all() function on the Request object to automatically apply validation rules to the URL parameters
class SetEmailRequest
{
public function rules()
{
return [
'email' => 'required|email|max:40',
'id' => 'required|integer', // << url parameter
];
}
public function all()
{
$data = parent::all();
$data['id'] = $this->route('id');
return $data;
}
public function authorize()
{
return true;
}
}
Access the data normally from the controller like this, after injecting the request:
$setEmailRequest->email // request data
$setEmailRequest->id, // url data
If you dont want to specify each route param and just put all route params you can override like this:
Laravel < 5.5:
public function all()
{
return array_merge(parent::all(), $this->route()->parameters());
}
Laravel 5.5 or above:
public function all($keys = null)
{
// Add route parameters to validation data
return array_merge(parent::all(), $this->route()->parameters());
}
The form request validators are used for validating HTML form data that are sent to server via POST method. It is better that you do not use them for validating route parameters. route parameters are mostly used for retrieving data from data base so in order to ensure that your token route parameter is correct change this line of your code, from
$token = Token::where('token', '=', $request->token)->first();
to
$token = Token::where('token', '=', $request->input(token))->firstOrFail();
firstOrFail() is a very good function, it sends 404 to your user, if the user insert any invalid token.
you get no " token " parameter set because Laravel assumes that your "token" parameter is a POST data which in your case it is not.
if you insist on validating your "token" parameter, by form request validators you gonna slow down your application, because you perform two queries to your db,
one in here
$token = Token::where('token', '=', $request->token)->first();
and one in here
return [
'token' => ['required','exists:Tokens,token,executed_at,null']
];
I suggest to use firsOrFail to do both validating and retrieving at once.
A trait can cause this validation to be relatively automagic.
Trait
<?php
namespace App\Http\Requests;
/**
* Class RouteParameterValidation
* #package App\Http\Requests
*/
trait RouteParameterValidation{
/**
* #var bool
*/
private $captured_route_vars = false;
/**
* #return mixed
*/
public function all(){
return $this->capture_route_vars(parent::all());
}
/**
* #param $inputs
*
* #return mixed
*/
private function capture_route_vars($inputs){
if($this->captured_route_vars){
return $inputs;
}
$inputs += $this->route()->parameters();
$inputs = self::numbers($inputs);
$this->replace($inputs);
$this->captured_route_vars = true;
return $inputs;
}
/**
* #param $inputs
*
* #return mixed
*/
private static function numbers($inputs){
foreach($inputs as $k => $input){
if(is_numeric($input) and !is_infinite($inputs[$k] * 1)){
$inputs[$k] *= 1;
}
}
return $inputs;
}
}
Usage
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class MyCustomRequest extends FormRequest{
use RouteParameterValidation;
/**
* 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 [
//
'any_route_param' => 'required'//any rule(s) or custom rule(s)
];
}
}
For \App\Http\Requests\CheckTokenServerRequest you can add use App\Http\Requests\CheckTokenServerRequest; at the top.
If you pass the token by url you can use it likes a variable in controller.
public function checkToken($token) //same with the name in url
{
$_token = Token::where('token', '=', $token)->first();
$dt = new DateTime;
$_token->executed_at = $dt->format('m-d-y H:i:s');
$_token->save();
return response()->json(json_decode($token->json),200);
}
$request->merge(['id' => $id]);
...
$this->validate($request, $rules);
or
$request->merge(['param' => $this->route('param')]);
...
$this->validate($request, $rules);
You just missing the underscore before token. Replace with
_token
wherever you check it against the form generated by laravel.
public function rules()
{
return [
'_token' => ['required','exists:Tokens,token,executed_at,null']
];
FormRequest has a method validationData() that defines what data to use for validation. So just override that one with route parameters in your form request class:
/**
* Use route parameters for validation
* #return array
*/
protected function validationData()
{
return $this->route()->parameters();
}
or leave most of the all logic in place and override input method from trait \Illuminate\Http\Concerns\InteractsWithInput
/**
* Retrieve an input item from the request.
*
* #param string|null $key
* #param string|array|null $default
* #return string|array|null
*/
public function input($key = null, $default = null)
{
return data_get(
$this->getInputSource()->all() + $this->query->all() + $this->route()->parameters(), $key, $default
);
}

Resources