Add new Object data Using PHP Laravel (Array_push) - php

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));
?>

Related

How to make two columns of a table requiring to be unique at user request

I have tried validating the user request like this:
$data = $request->validate([
'fname' => 'nullable',
'lname' => 'nullable',
'gender' => 'required',
'mobile' => 'required|unique:users,usr_name',
'ncode' => 'nullable',
'password' => 'required',
'password_confirmation' => 'required',
]);
That the mobile filed value must be unique:users,usr_name but I do need to check that it is unique at members table as well (the mbr_mobile column):
unique:members,mbr_mobile
So how to combine these two rules at once?
Just specifying the model name should work.
'mobile' => 'required|unique:User|unique:Members',
But you may specify the column name too.
'mobile' => 'required|unique:User, mobile|unique:Member,mbr_mobile',
You can do it exact,y same way as for users table :
$data = $request->validate([
'fname' => 'nullable',
'lname' => 'nullable',
'gender' => 'required',
'mobile' => 'required|unique:users,usr_name|unique:members,mbr_mobile',
'ncode' => 'nullable',
'password' => 'required',
'password_confirmation' => 'required',
]);

Laravel not validating array input

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.

Laravel: Edit value only if it appears in the request?

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()).

can we have less code for validation form in laravel if i have more than 20 inputs

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

How to validate without request in Laravel

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.

Categories