Validator::validateNumber does not exist - laravel

Image 1
Image 2
Image 3
Why is post request not allowed?

Use numeric instead number
commisionRate => "required|numeric|between:0,0.99",

According to Laravel default validation rules which are here:
https://laravel.com/docs/5.6/validation#available-validation-rules
there are no rule for "number"
according to your third screenshot you are using number validation rules just remove it.
commisionRate => "required|number|min:0.01|max:1
to this
commisionRate => "required|min:0.01|max:1
check your all fiels and do this change. To check number laravel provide you "numeric" validation rules

Related

Laravel Validation 'starts_with' alpha

I am looking to use Laravel validation for user input fields, and all works well aside from one rule I'm looking to enforce: how to ensure a field starts with alpha (where it allows alpha_dash elsewhere). I tried the PHP 'regexp' version [A-Za-z] (shown in code snippet below) but to no avail. I also tried 'starts_with:alpha' also to no avail. I'm hoping to avoid regexp and the like and would rather wait for Laravel solution if there is no simple solution.
Thanks!
'username' => 'required|starts_with:[A-Za-z]|alpha_dash|max:20|unique:users,username',
'firstname' => 'required|starts_with:[A-Za-z]|alpha_dash|max:20',
'lastname' => 'required|starts_with:[A-Za-z]|alpha_dash|max:30',
Try this rule instead of alpha_dash & starts_with:
'required|min:2|max:30|regex:/^[A-Z][a-zA-Z0-9_-]+$/'

laravel5 Route::resource Generate wrong route

I am using Laravel 5.7 Route::resource.
I has saw
https://laravel.com/docs/5.7/controllers
when I use
Route::resource('koumokus', 'KoumokuController');
should be Generate as bleow
GET admin/koumokus/{koumoku} show
GET admin/koumokus/{koumoku}/edit edit
However, It is Generate
GET admin/koumokus/{koumokus} show
GET admin/koumokus/{koumokus}/edit edit
on the other hand,
Route::resource('funruis', 'FunruiController');
is normally to gent
GET admin/funruis/{funrui} show
GET admin/funruis/{funrui}/edit edit
How can I fixed it?
Sometimes laravel fails to find the conventional name for the parameter on the resource routes. It has happened to me sometimes when I used words that are not in english. But it allows you to customize that name. You can take a look at the docs here ->
Naming Resource Route Parameters
Since you are working with the 'koumokus' resource here, you can specify that the parameter for the 'koumokus' resource must be 'koumoku'.
Route::resource('koumokus', 'KoumokuController', [
'parameters' => [
'koumokus' => 'koumoku'
]
]);

How to add / remove elements from array that is in Request

My request looks like this
Array
(
[name] => Eugene A
[address] => Array
(
[billing] => Array
(
[address] => aaa
)
[shipping] => Array
(
[address] => bbb
)
)
)
I need to delete the shipping address. But how?
I can only delete both addresses,
$request->request->remove('address');
but I don't want it.
I want to delete only shipping address, like so
$request->request->remove('address.shipping');
But it is not working for me
Laravel 5.6
Update
Why do I need it?
Easy. I have abstracted out my Form Request validation into a class that is a child to Illuminate\Foundation\Http\FormRequest.
I actually have few classes for validation. I call them one by one in a controller like so:
app()->make(CustomerPostRequest::class); // validate Customer information
app()->make(AddressSaveRequest::class); // validate Addresses
Why?
Now I can Mock this requests in unit-tests, and I can have my validation abstracted out. And I can use Address validation in many places.
But Now I need more flexibility. Why?
Because AddressSaveRequest rule looks like this
public function rules(): array
{
return [
'address.*.address' => [
'bail',
'required',
'string',
],
...
It validates all addresses.
But sometimes I don't want to validate shipping address, if the the chech_box - ship_to_the_same_address is ticked.
But I have my Address validator abstracted in separate file and it is used in many places. There are places where ship_to_the_same_address tick box is not presented.
Thus I cannot use 'required_unless:ship_to_same_address,yes',
And I cannot use
app()->makeWith(AddressSaveRequest::class, ['ship_to_the_same_address ' => 'yes']);
Because Taylor said ...when calling makeWith. In my opinion it should make a new instance each time this method is called because the given parameter array is dynamic.. And it does, and it does not work correctly with app()->instance(AddressSaveRequest::class, $addressSaveRequest); and cannot be mocked in unit tests.
Why Taylor decided it - I seriously don't know.
PS
And yes, I know that mocking requests is not recommended.
If you were trying to add or remove inputs from the Request itself:
You can add data to the request pretty easily by merging it in and letting Laravel handle which data source is being used:
$request->merge(['input' => 'value']);
That will merge in the input named input into the input source for the Request.
For removing inputs you could try to replace all the inputs without that particular input in the replacement:
$request->replace($request->except('address.shipping'));
Just one idea to try.
Try this:
$request->except(['address.shipping']);
Details: Laravel Request
Laravel has a helper method called array_forget, which does exactly what it sounds like:
$requestArray = $request->all();
$newArray = array_forget($requestArray, 'address.shipping')
Documentation
After the edit to the main question with why some inputs of the request are to be deleted, my main answer isn't correct anymore. User Lagbox has the correct answer for the question that was asked.
However, I would like to note that another solution would be to have seperate Request classes with validation. One for placing an order (assuming it is a system where someone can order stuff) where ship_to_same_address is present and another one for things like updating your account, like PlaceOrderRequest and UpdateAccountRequest classes.

Laravel 5 : Optional parameter in routes

I am looking for a solution for this code to work with the optional parameter.Indeed placed at the beginning, this parameter is no longer considered optional but I absolutely need to have a solution.
The goal is to archive the old versions of the website by year (it's for a music festival) without having to duplicate all laravel files ...
Route::get('{year?}/lineup', 'HomeController#agenda')->name('agenda');
In this precise case :
/2018/lineup => works
/lineup => not working
Note : I'm using Laravel 5.4
An idea, a suggestion to archive by date the old versions of the site ?
Archives must be accessible online.
Thank you
If you add them in this order it should first check for a match with the year and then for only the lineup. Then it is up to your controller (same for both routes, no extra files needed) to handle the input parameter, if it is set or not.
Route::get('{year}/lineup', 'HomeController#agenda')->name('agenda.history');
Route::get('lineup', 'HomeController#agenda')->name('agenda.current');

Laravel validation required rule not working

I need to add required rule if one field is available. Also need to check if it is an integer and 10 digit. So I added the rule like below.
'id_number' => 'sometimes|required|digits:10|integer'
Validations works only when the field is available. But here required rule is not working. It directly shows integer error even if the field is empty.
I use Laravel 5.1
Finally I figured it!
You need to change the order of required rule to last. It works when I add rule like this,
'id_number' => 'sometimes|digits:10|integer|required'

Resources