Problems getting email field to ignore no changes - php

I'm having a problem with the email_costumer field.
In an insertion field of type POST, the excerpt below blocks the function of inserting an e-mail belonging to a customer in the customers table:
'POST' => [
'email_customer' => ['required','max:50','email', Rule::unique('costumers','email_costumer')->ignore($this->id)],
],
Now in a PATCH form for validations. I want it to be possible to keep the value of email_customer without the Unique validation not triggering, because otherwise it blocks me from updating the email with one that already belongs to a customer in the customers table.
'PATCH' => [
'email_customer' => ['required','max:50','email', Rule::unique('customers','email_costumer')->ignore($this->id)],
],
CustomerRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class CustomerRequest 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<string, mixed>
*/
public function rules()
{
return match ($this->method()) {
'POST' => [
'email_customer' => ['required','max:50','email', Rule::unique('costumers','email_costumer')->ignore($this->id)],
],
'PATCH' => [
'email_customer' => ['required','max:50','email', Rule::unique('customers','email_costumer')->ignore($this->id)],
],
};
}
}
How i fix this problem?

Related

Laravel validation rule for one item which can be email or phone number

I want to validate one request item which can be phone number or email address. Is there any way in Laravel validation rules or other options to handle this?
<?php
namespace App\Http\Requests\UserAuthRequests;
use Illuminate\Foundation\Http\FormRequest;
class AuthenticationUser 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 [
'credentials' => ['required'],
'g-recaptcha-response' => ['required'], ];
}
}
My proposition is to write custom validation rule, where you will be conditionally check if it is a valid email or phone number.
Validator::extend('email_or_phone', function ($attribute, $value, $parameters, $validator) {
return
$validator->validateDigitsBetween($attribute, $value, [8, 10]) ||
$validator->validateEmail($attribute, $value, []);
});
Write this code in boot method in for example AppServiceProvider. I also strongly recommend you to use dedicated classes for custom rules as described here: https://laravel.com/docs/9.x/validation#custom-validation-rules
In both ways you just end up with code like this:
public function rules()
{
return [
'credentials' => ['email_or_phone'], // HERE WE USE OUR CUSTOM RULE
'g-recaptcha-response' => ['required'],
];
}

How validate request with custom FormRequest without dependency injection

I validate requests using FormRequest. This is a great tool, but I ran into a problem. Making standard identical methods for controllers, I decided to put these methods in a basic abstract class. But in this case, I can't validate requests using FormRequest, since they are transmitted using DI. I found the following solution that partially solved this problem:
/** #var FormRequest $customForRequest */
$customForRequest = $requestModel::createFrom($this->request);
return Validator::make($this->request->all(), $customForRequest->rules())->validate();
But the withValidator method, which in the standard scenario works as an after hook, is not called in this situating. How can this method be forced to be executed, leaving the validation logic in a separate class?
My FormRequest:
class MassUpdateDocumentAttribute 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 [
'filter' => 'required|array',
'filter.id.*' => [
'sometimes',
'integer',
Rule::exists(DocumentAttribute::class, 'id')
],
'fields' => 'required|array',
'fields.document_id' => [
'sometimes',
Rule::exists(Document::class, 'id'),
],
'fields.document_attr_type_id' => [
'required_with:fields.value',
Rule::exists(DocumentAttrType::class, 'id'),
],
'fields.value' => [
'sometimes',
'bail',
]
];
}
public function withValidator(Validator $validator)
{
if ($validator->fails()) return;
$validator->after(function ($validator) use ($documentAttributeRepository, $documentAttrTypeRepository) {
$data = $validator->getData();
// some large logic
});
}
}
You should be able to instantiate your FormRequest class by simply calling app(YourRequest::class) inside the method. It will automatically validate. This way you can simply pass the YourRequest::class part as a variable to desired FormRequest and it all should be working.
$request = app(YourRequest::class);
$data = $request->validated();

Renaming laravel validation array notation in error messages

I am getting a very unfriendly field name in my validation errors similar to the array notation used as name for the field's rules
EG.
$rules = [
'resource.*.name' => 'required|string|max:16'
];
// error message.
// the resource.0.name is required.
How do I rename the resource.0.name in the message to something else like name or resource name.
For more convenience you may use laravels form request validation,
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class Resource 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 [
'resource.*.name' => 'required|string|max:16'
];
}
public function messages()
{
return [
'resource.*.name' => 'The Resouce Name must match the criteria'
];
}
}
In your controller:
use App\Http\Requests\Resource;
public function store(Resource $request)
{
}

Filter data in form request in Laravel 5.5

I have generated new form Request for the controller, but I do not know how to filter data before there will handle in the validator and so on.
Are there some native solutions in Laravel for this case?
class TestRequest 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 [
"title" => "required|string",
"order" => "required|integer"
];
}
}
class TestController extends Controller
{
public function store(TestRequest $request, $chapterId)
{
// some business logic
}
}
There is some solution in Laravel 5.5 but in this example author uses
validate for filtering data from request, but I need to use filter inside TestRequest
$data = $this->validate(request(), [
//...
]); // I can't use this inside TestRequest
You can use my package: https://github.com/mikicaivosevic/laravel-filters
It's allows you to filter request values before validation...
<?php
class LoginRequest extends FormRequest {
//Filter
public function filters()
{
return [
'name' => 'lower',
'id' => 'int',
];
}
//...
}
Convert $request->name value into lowercase.
Conert $request->id value into integer.

Laravel 5.4 Validation Using Form Requests

I want to use Form Requests to validate Model so i started by create php artisan make:request TaskRequest and after i add in TaskRequest class `
public function rules()
{
return [
'name' => 'required|min:5',
];
}
public function messages()
{
return [
'name.required' => 'A title is required',
];
}
`
and in My logic
Route::post('/tasks',function (\App\Http\Requests\TaskRequest $request){
$task = new \App\Task();
$task->name = $request->input("name");
$task->save();
return response()->json(['task was created',$task], http_response_code());
});
So when i try to add a task i get error HttpException, This action is unauthorized.,AuthorizationException ...
It was work for me without Validation. So how can i fix this issue ?
Every (self-created) request has an authorize function that determines if the user is allowed to send the request. This can be useful to check for admin privileges or similar.
In you case, you can simply return true. More information can be found in the corresponding docs
Your authorize function in your TaskRequest would look like this:
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
In your custom request class for "form request" which contains the validation logic pass return:true; instead of return:false; and then it will work like a charm.
The code will look like something as following,
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class portfolioValidate 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 [
'title'=> 'required',
'content'=> 'required'
];
}
}
as you can use middleware to make authentication for the page which contains this form... so we don't need that authorization in the FormRequest class. So returning true will make it(validation) authorized for all cases.
I think now it is clear to everyone now.

Categories