I'm trying to validate some input array fields in Laravel. My application reports back the field is required, even though it has been filled out.
The validation code I have is:
$this->validate($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'email' => 'unique:contacts,email,' . $request->id,
'address' => 'required',
'address.0.address_line_1' => 'required',
]);
The full posted array is:
Array
(
[id] => 1
[_token] => xxx
[first_name] => Joe
[last_name] => Bloggs
[contact_name] => Joe Bloggs
[telephone] => 077
[email] => joe#test.com
[address] => Array
(
[0] => Array
(
['address_line_1'] => sss
['address_line_2'] =>
['city'] =>
['county'] =>
['postcode'] =>
['property_type'] => site
)
)
)
My input fields are constructed like so:
address[0]['address_line_1']
I'm getting the validation message error:
The address.0.address line 1 field is required.
Anyone know what's wrong here?
That's because you don't have any address_line_1 or address_line_2 in your array validation but you have posted those two elements.
You only have address.0.address_line_1 and there's no address.0.address_line_1 inside your posted data.
The elements of your array validation should be the same with what you posted.
In your case, you can remove address.0.address_line_1 and add 2 more elements of your array validation.
'address_line_1' => 'required',
'address_line_2' => 'required',
The updated answer :
'address' => 'required|array',
'address.*' => 'required|array',
'address.*.address_line_1' => 'required|string',
Just replace your validator with this
$this->validate($request, [
'first_name' => 'required',
'last_name' => 'required',
'telephone' => 'required',
'email' => 'unique:contacts,email,' . $request->id,
'address' => 'required',
'address.*' => 'required',
'address.*.address_line_1' => 'required',
]);
I managed to figure out what was causing the issue...it was actually to do with the form input field names.
I removed the single quotes from the input names and it worked!
I changed:
<input type="text" class="form-control" name="address[0]['address_line_1']">
To, this:
<input type="text" class="form-control" name="address[0][address_line_1]">
And it worked! Hope it helps someone else.
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()).
Array one:
[
'jabatan' => 'required',
'tipe_kegiatan' => 'required',
'waktu' => 'required',
'nrt' => 'required',
0 => [
'materi' => 'required',
'metode' => 'required',
'hasil' => 'required',
'img' => 'required|image|max:1024',
]
];
Array two:
[
'jabatan' => 'required',
'tipe_kegiatan' => 'required',
'waktu' => 'required',
'nrt' => 'required',
'materi' => 'required',
'metode' => 'required',
'hasil' => 'required',
'img' => 'required|image|max:1024',
];
how to change the data array as shown below
Change Data array one to array two (I try using array_push but I don't get the wanted result)
My case:
I have 2 conditions where if the user inputs by selecting form type 1 then the validation will adjust to form 1 and if the user selects input with form 2 then the validation also adjusts to form 2. all processes are in one post request action.
you should use array_merge like this:
<?php
$a1=["red","green"];
$a2=["blue","yellow"];
print_r(array_merge($a1,$a2));
?>
if i have more than 20 fields,can we validate this with less code???
i do not want to write required for all input .
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'gender' => 'required',
'date_of_birth' => 'required',
'place_of_birth' => 'required',
'nationality' => 'required',
'mobile_number' => 'required',
'email' => 'required|email|unique:informations',
'home_region' => 'required',
'digital_address' => 'required',
'school_name' => 'required',
'school_region' => 'required',
'school_digital_address' => 'required',
'school_level' => 'required',
'school_program_of_study' => 'required',
'patron_first_name' => 'required',
'patron_last_name' => 'required',
'patron_gender' => 'required',
'patron_mobile_number' => 'required'
]);
Sure! here's how, iterate through all request data (including ones you didn't intend to have sent which makes this a bad idea) and validate them to be required except for ones that requires more rules like 'email' (those can be validated by themselves)
$data = array_except($request->all(), ['_token', 'email']);
foreach ($data as $key => $value) {
$request->validate([$key => 'required']);
}
$request->validate(['email' => 'required|email|unique:informations']);
That's the minimal code laravel needs to works the validation.
If you dont enter each field's name, how is it supposed to know wich missing entry from the request is required ? laravel needs each and every input name marked down.
Actually yes there is a way.
This is your code
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'gender' => 'required',
'date_of_birth' => 'required',
'place_of_birth' => 'required',
'nationality' => 'required',
'mobile_number' => 'required',
'email' => 'required|email|unique:informations',
'home_region' => 'required',
'digital_address' => 'required',
'school_name' => 'required',
'school_region' => 'required',
'school_digital_address' => 'required',
'school_level' => 'required',
'school_program_of_study' => 'required',
'patron_first_name' => 'required',
'patron_last_name' => 'required',
'patron_gender' => 'required',
'patron_mobile_number' => 'required'
]);
Now in your form html for all these fields you need to make an array for it like this
<form>
<input type="text" name="person[first_name]">
<input type="text" name="person[last_name]">
<input type="email" name="email">
</form>
and in your Controller or Request you can make this array required like this
$request->validate([
'person' => 'required|array',
'person.*' => 'required'
'email' => 'required|email|unique:informations',
]);
This way you can have less index in your array ;), Hope it will help
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
I am newbie with Laravel and picked up a system to give maintenance.
I have this rule:
protected $rules = array(
'per_vlr_principal_rte' => 'required_if:per_fase_processual,2',
'per_vlr_juros_rte' => 'required_if:per_fase_processual,2',
'per_vlr_principal_rdo' => 'required_if:per_fase_processual,1,2',
'per_vlr_juros_rdo' => 'required_if:per_fase_processual,1,2',
'per_vlr_principal_perito' => 'required_if:per_fase_processual,2',
'per_vlr_juros_perito' => 'required_if:per_fase_processual,2',
'per_vlr_homologado' => 'required_if:per_fase_processual,2',
'per_qte_rte' => 'required',
'per_competencias' => 'required',
'per_qte_laudo' => 'required',
'per_dt_calculo' => 'required'
);
I'd like to know if the code below is correct:
'per_vlr_principal_rdo' => 'required_if:per_fase_processual,1,2',
'per_vlr_juros_rdo' => 'required_if:per_fase_processual,1,2',
The validation occurs only when the value is 1.
How to fix it?