Category : laravel-validation

I use custom request classes which extend Laravavel’s FormRequest (User Guide, API Documentaion) in order to validate the input parameters, and to authorize the request. The skeleton looks like this: class MyRequest extends FormRequest { /** * Determines if the user is authorized to make this request. */ public function authorize(): bool { // TODO: ..

Read more

I’m facing a problem while validating the country in laravel 1.View <div class="form-group"> <label >{{__("Country")}} <span class="required">*</span> </label> <select name="country" class="form-control"> <option value="">{{__(‘– Select –‘)}}</option> @foreach(get_country_lists() as $id=>$name) <option @if((old(‘country’,$dataUser->country ?? ”)) == $id)selected @endif value="{{$id}}">{{$name}}</option> @endforeach </select> <span class="invalid-feedback error error-country"></span> </div> 2.in Controller ‘country’ => ‘required|max:200’, ‘country.required’ => __(‘Select your country from the ..

Read more

I need to validate a request account_type if it is either ‘Personal’ or ‘Organizational’ So that i can validate other field by required if account_type,personal.Heres the code that i have tried. ‘account_type’ => ‘required|options:PERSONAL,ORGANIZATIONAL’, ‘per_client_name’ => ‘required_if:account_type,PERSONAL|string’, ‘per_street’ => ‘required_if:account_type,PERSONAL’, ‘per_address’ => ‘required_if:account_type,PERSONAL’, ‘org_name’ => ‘required_if:account_type,ORGANIZATIONAL’, ‘org_type’ => ‘required_if:account_type,ORGANIZATIONAL’, ‘org_cont_name’ => ‘required_if:account_type,ORGANIZATIONAL’ Method IlluminateValidationValidator::validateOptions does ..

Read more

I got an input that contains an array of objects. Now I’d like to validate it, but one part of the validation depends on another item of the object, so I need it’s index: [ ‘items.myarray.*.owner_id’ => ‘numeric|exists:AppModelsOwner’, ‘items.myarray.*.key’ => [‘nullable’, ‘string’, ‘min:20’, ‘max:20’, ‘exists:AppModelsInvitation,key’, new KeyIsValid(Owner::findOrFail(request()->input(‘items.*.owner_id’)))], ]; Of course request()->input(‘items.myarray.*.owner_id’) is not working – ..

Read more

I have a form that submits a large number of images, let’s say there are 5 images. I named them img1,img2..,img5. for now I am validating those fields one by one. my validation code $rules = [ ‘image1’ => ‘required|image|mimes:jpeg,png,jpg,gif,svg|max:3072’, ‘image2’ => ‘image|mimes:jpeg,png,jpg,gif,svg|max:3072’, ‘image3’ => ‘image|mimes:jpeg,png,jpg,gif,svg|max:3072’, ‘image4’ => ‘image|mimes:jpeg,png,jpg,gif,svg|max:3072’, ‘image5’ => ‘image|mimes:jpeg,png,jpg,gif,svg|max:3072’, ]; $msg = ..

Read more

I have a form that submits a large number of images, let’s say there are 5 images. I named them img1,img2..,img5. for now I am validating those fields one by one. my validation code $rules = [ ‘image1’ => ‘required|image|mimes:jpeg,png,jpg,gif,svg|max:3072’, ‘image2’ => ‘image|mimes:jpeg,png,jpg,gif,svg|max:3072’, ‘image3’ => ‘image|mimes:jpeg,png,jpg,gif,svg|max:3072’, ‘image4’ => ‘image|mimes:jpeg,png,jpg,gif,svg|max:3072’, ‘image5’ => ‘image|mimes:jpeg,png,jpg,gif,svg|max:3072’, ]; $msg = ..

Read more

Say a model has a status property, that holds an array of strings. If this array contains a string name "Other", the status_other field should be required. I can achieve this with the following rules: ‘status’ => ‘nullable|array’, ‘status_other’ => Rule::requiredIf(in_array(‘Other’, $this->model->status)) Is there a way to write the status_other rule as a string? I ..

Read more

I have an import of employees with a unique employee number, the problem I’m facing is when 2 rows have the same number, the import still continues. This is my validation for my employee number. ‘*.employee_number’ => [‘required’, ‘max:50’, ‘unique:employee_information,employee_number,NULL,id,company_id,’.Auth::user()->company_id], Vision of the import file Employee Number| ———————| EMP1 //Accepts ———————| EMP1 //Accepts too The ..

Read more

I need to apply a required rule to a array field in my form request when the another array field, in the same index, has a certain value. Example: public function rules(): array { return [ ‘data’ => ‘array’, ‘data.*.is_admin’ => ‘required|boolean’, ‘data.*.area’ => [ ‘nullable’, Rule::requiredIf($condition), // other rules… ] ] } When evaluating ..

Read more

In Laravel 8 app I defined boolean field $table->boolean(‘featured’)->default(false); and validation rules : ‘feature’ => ‘required|boolean’, But defining selection input on a form I have 1st “Select” item, like: <select class="form-control select2" id="featured" name="featured"> <option value="">Select…</option> @if($gigFeaturedList) @foreach($gigFeaturedList as $key=>$value) <option and I suppose that make my validation to work in invalid way, so when ..

Read more

I have a question about Laravel’s validation logic. My HTML code: <div class="row"> <div class="col-md-12"> <form id="carFilesForm" autocomplete="off" enctype="multipart/form-data"> <input name="car_files[]" type="file" multiple> <button type="submit">Upload</button> </form> </div> </div> I am submitting the form with ajax. My CSRF token is already specified in the global js file. (Additionally, I don’t have a problem with uploading files ..

Read more

I have an EmailAddress model that has an ’email_address’ property. This property must be unique, so I added a rule to my StoreEmailAddressRequest. The problem comes up when I want to update a model. In my UpdateEmailAdressRequest I added an entry: Rule::unique(’email_addresses’)->ignore($id); Now, when I try to update the model and don’t touch email_address, everything ..

Read more

I used Request to validate data public function rules () : array { return [‘order_type’ => ‘numeric|gt:0′]; } For example, my order_type=’abc’, I’ll be received 2 message in result order_type: [ ‘validate.numeric’, ‘validate.numeric.gt’ ] But I want only 1 message for both numeric validate and greater than 0 validate. And my result I want is: ..

Read more

I am trying to validate a form using form request like so. /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ ‘foo_required’ => [‘boolean’, ‘nullable’], ‘foo_id’ => [‘bail’, ‘required_if:foo_required,true’, new UniqueVendorId(request()->input(‘dest’), request()->input(‘cus_id’))], ]; } The above is my form request rules, where ..

Read more

I am trying to make a validation that will check whether at least one item is provided in an array following the steps in Custom Validation Rules Routes.php Route::middleware([‘auth:api’, ‘bindings’])->group(function () { Route::prefix(‘api’)->group(function () { Route::apiResources([ ‘exam-papers/{examPaper}/questions’ => ExamPaperQuestionsController::class ]); }); }); ValidateArrayElementRule.php namespace AppRules; use IlluminateContractsValidationRule; class ValidateArrayElementRule implements Rule { public function __construct() ..

Read more

{"message":"The given data was invalid.","errors":{"category_image":["The category image must be an image.","The category image must be a file of type: jpeg, png, jpg, gif, svg.","The category image may not be greater than 2048 characters."]}} this message shows every time even when I upload .jpg/.png/.jpeg file my validation code: $request->validate([ ‘category_name’ =>’required|min:3|max:20|unique:categories’, ‘publication_status’=>’required’, ‘category_image’ =>’required|image|mimes:jpeg,png,jpg,gif,svg|max:2048′, ]); my ..

Read more

I have a page with multiple forms on it but I need to validate a specific submitted form. In my view I have this for each input of the forms <div class="form-group"> <label for="name">{{ __(‘Name’)}}</label> <input class="form-control {{ $errors->has(‘name’) ? ‘is-invalid’ : ” }}" type="text" name="name" id="name" value="{{ old(‘name’, ”) }}"> @if($errors->storecomment->any()) <div class="invalid-feedback"> @foreach($errors->storecomment->all() ..

Read more

there I’m facing an issue with the validation, Input arrays should include at least one value for example when I submit a form I get the following inputs array:26 [ "_token" => "GMPKHos9nnC6zw6UGN4nFKVEGd5PoEpDe9lEPETw" "Customer" => "1" "Text_From_Date" => "07 Jun 2021" "From_Date" => "2021-06-07" "Text_Due_Date" => "16 Jun 2021" "Due_Date" => "2021-06-16" "Invoice_Id" => "INV-1" ..

Read more