In my application, I get a validation error when selecting a time from 1 to 9, which is one digit.
The source code is:
$validator = Validator::make($request->all(), [
'service_id' => 'required|integer|exists:services,id',
'origin' => 'required|string|latlng',
'destination' => 'required|string|latlng',
'date' => 'required|date_format:Y-m-d|after_or_equal:today',
'time' => 'required_with:date|date_format:H:i'.($request->get('date') == Carbon::today()->format('Y-m-d') ? '|after:'.Carbon::now()->format('H:i') : '')
]);
and i get this error
{time: {The time does not match the format H:i.]}
Try this
$validator = Validator::make($request->all(), [
'service_id' => 'required|integer|exists:services,id',
'origin' => 'required|string|latlng',
'destination' => 'required|string|latlng',
'date' => 'required|date_format:Y-m-d|after_or_equal:today',
'time' => 'required|date_format:H:i'
]);
I hope this works!
Related
in my app the user can update the info of stripe connected account, however I ONLY want to actullay update the value of the fields that appear in the request payload, I could do this with a simple if check but the way I update the stripe array method makes this issue more complicated .
Is there any syntax sugar or trick to make this easier.
How my update method looks;
public function editConnectedAccount(Request $request)
{
$account = Account::retrieve($request->connectedAccountId);
Account::update(
$request->connectedAccountId,
[
'type' => 'custom',
'country' => 'ES',
'email' => $request->userEmail,
'business_type' => 'individual',
'tos_acceptance' => [ 'date' => Carbon::now()->timestamp, 'ip' => '83.46.154.71' ],
'individual' =>
[
'dob' => [ 'day' => $request->userDOBday, 'month' => $request->userDOBmonth, 'year' => $request->userDOByear ],
'first_name' => $request->userName,
'email' => $request->userEmail,
'phone' => $request->userPhone,
'last_name' => $request->userSurname,
//'ssn_last_4' => 7871,
'address' => [ 'city' => $request->userBusinessCity, 'line1' => $request->userBusinessAddress, 'postal_code' => $request->userBusinessZipCode, 'state' => $request->userBusinessCity ]
],
'business_profile' =>
[
'mcc' => 5812, //got it
'description' => '',
//'url' => 'https://www.youtube.com/?hl=es&gl=ES', //got it
],
'capabilities' => [
'card_payments' => ['requested' => true],
'transfers' => ['requested' => true],
],
]
);
return response()->json([
'account' => $account,
], 200);
Consider using a Form Request where you preform validation. This will neaten up your controller for a start and also make validation (never trust user input!) reusable.
Assuming validation is successful, calling $request->validated() from inside your controller method will return only the fields present and validated. You can then use either fill($request->validated()) or update($request->validated()).
I have a set of validation rules in laravel. there are two date fields that one of them have to be greater than other. but when I use gt operator, an error apears:
$validation = Validator::make($request->all(), [
'description' => 'required|string',
'started_at' => 'required|date',
'finished_at' => 'required|date|gt:started_at',
]);
error is: Method Illuminate\Validation\Validator::validateGt does not exist.
gt:started_at is wrong gt rule does not exist in laravel use after
$validation = Validator::make($request->all(), [
'description' => 'required|string',
'started_at' => 'required|date',
'finished_at' => 'required|date|after:started_at',
]);
I need to validate an array but without a request.
In laravel docs validation is described like this:
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
But I can't use $request because the data comes from an external api and the validation is not inside a controller. How can I validate this array? For example:
$validatedData = validate([
'id' => 1,
'body' => 'text'
], [
'id' => 'required',
'body' => 'required'
]);
Should be. Because $request->all() hold all input data as an array .
$input = [
'title' => 'testTitle',
'body' => 'text'
];
$input is your custom array.
$validator = Validator::make($input, [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
Validator::make expects array and not a request object.
You can pass any array and implements the rules on it.
Validator::make(['name' => 'Tom'], ['name' => 'required', 'id' => 'required']);
And it will validate the array. So $request object is not necessary.
You can achieve this by create request object like so:
$request = new Request([
'id' => 1,
'body' => 'text'
]);
$this->validate($request, [
'id' => 'required',
'body' => 'required'
]);
and thus you will get all the functionality of the Request class
$request->all() is array not request object.
This code will work:
$data = [
'id' => 1,
'body' => 'text'
];
$validator = Validator::make($data, [
'id' => 'required',
'body' => 'required',
]);
You can also merge data with the request object:
$data = ['id' => 1];
$request->merge($data);
Then validate as per usual.
I am validating an array in Laravel. I get "0.id has already been taken." in default error message. So I added a 2nd parameter in my validator: 'unique' =>':attribute has already been taken. Please fix your data in spreadsheet.' and shows "0.id has already been taken. Please fix your data in spreadsheet.". I added the 3rd parameter which is the custom attribute. ['*.id' =>'Student ID']. But I want to have a message like this: ID has already been taken. Please fix your data in spreadsheet in line 1.
Here's my full validation code:
$validate = $request - > validate([
'*.id' => 'required|unique:students|numeric',
'*.rfid_number' => 'required|unique:students|numeric',
'*.first_name' => 'required|alpha|max:100',
'*.middle_name' => 'alpha|max:100|nullable',
'*.last_name' => 'required|string|max:100',
'*.name_extension' => 'alpha|max:10|nullable',
'*.email' => 'required|email|unique:students',
'*.photo' => 'string|nullable',
'*.house_number' => 'required|integer',
'*.barangay' => 'required|alpha|max:100',
'*.city' => 'required|alpha|max:100',
'*.province' => 'required|string|max:100',
'*.zip_code' => 'required|integer',
'*.birth_date' => 'required|date|max:100',
'*.birth_place' => 'string|max:200',
'*.gender' => 'required|alpha',
'*.religion' => 'alpha|max:100|nullable',
'*.landline_number' => 'numeric|max:20|nullable',
'*.mobile_number' => 'required',
'*.father_name' => 'string|max:200|required',
'*.father_occupation' => 'string|max:200|nullable',
'*.mother_name' => 'string|max:200|required',
'*.mother_occupation' => 'string|max:200|nullable',
'*.guardian_name' => 'string|max:200|required',
'*.guardian_occupation' => 'string|max:200|nullable',
'*.guardian_address' => 'string|max:200|nullable',
'*.year' => 'integer|max:10|required',
'*.section' => 'alpha|max:200|required'
], [
'unique' => ':attribute has already been taken. Please fix your data in spreadsheet.'
], [ //attributes
'*.id' => 'Student ID'
]);
Something like this would do the trick:
$validate = $request->validate([
//
], [
//
],
collect($request->all())->keys()->flatMap(function ($index) {
return ["$index.id" => 'ID'];
})->toArray());
Iterate over all the indexes so you end up with something like:
[
'0.id' => 'ID',
'1.id' => 'ID',
'2.id' => 'ID',
'3.id' => 'ID',
'4.id' => 'ID',
'5.id' => 'ID',
'6.id' => 'ID',
'7.id' => 'ID',
]
As your final array to the validator
How i set custom the attributte names in laravel 5.2 I already try this code, but doesn't work:
$attNames = array(
'code' => 'Número',
'contributor' => 'Nº Contribuinte',
'create_date' => 'Data criação',
'address' => 'Morada',
'zip_code' => 'Cod. Postal',
'city' => 'Localidade',
'email' => 'E-mail',
'phone_number' => 'Telefone',
'note' => 'Observações',
);
$validator = Validator::make($client, $this->rules,[],$attNames);
$validator->setAttributeNames($attNames);
if ($validator->fails()) {
// send back to the page with the input data and errors
$errors = $validator->messages();
return Redirect::to('/client/create')->withInput()->withErrors($errors);
}
You have passed wrong arguments to Validator::make.
You can pass only three arguments.
As per Documentation,
If needed, you may use custom error messages for validation instead of
the defaults. There are several ways to specify custom messages.
First, you may pass the custom messages as the third argument to the
Validator::make method.
$messages = [
'required' => 'The :attribute field is required.',
];
$validator = Validator::make($input, $rules, $messages);
I figure out.
controller:
use Validator;
(...)
$attName=array(
'code' => trans('validation.code'),
'contributor' => trans('validation.contributor'),
'create_date' => trans('validation.create_date'),
'address' => trans('validation.address'),
'zip_code' => trans('validation.zip_code'),
'city' => trans('validation.city'),
'email' => trans('validation.email'),
'phone_number' => trans('validation.phone_number'),
'note' => trans('validation.note'),
);
$validator = Validator::make($client, $this->rules, [], $attNames);
validation.php:
'attributes' => [
'code' => 'número',
'contributor' => 'nº contribuinte',
'create_date' => 'data criação',
'address' => 'morada',
'zip_code' => 'cod. postal',
'city' => 'localidade',
'email' => 'e-mail',
'phone_number' => 'telefone',
'note' => 'observações',
],