So I am trying to validate the uploaded images in my laravel project, however the File::image() validaton invalidates my input even though I am uploading a file. I am using postman to do this.
Here is my validatio rules.
'product_name' => 'required|string|unique:' . Product::class,
'product_stock_amount' => 'required|integer|numeric|gte:0',
'product_price' => 'required|integer|numeric|gte:0',
'product_price_currency' => 'required|string|'. Rule::in(config('lappee.accepted_currency')),
'product_description' => 'nullable|string',
'product_images' => [
'nullable',
File::image()->max(12 * 1024)->dimensions(Rule::dimensions()->maxWidth(config('lappee.allowed_image_size.width'))->maxHeight(config('lappee.allowed_image_size.height')))
],
Here's my postman.
I am I doing someething wrong?
Your validation expects that product_images is only one file, but you are sending product_images as an array. So you have to update your validation like this:
'product_name' => 'required|string|unique:' . Product::class,
'product_stock_amount' => 'required|integer|numeric|gte:0',
'product_price' => 'required|integer|numeric|gte:0',
'product_price_currency' => 'required|string|'. Rule::in(config('lappee.accepted_currency')),
'product_description' => 'nullable|string',
'product_images.*' => [
'nullable',
File::image()->max(12 * 1024)->dimensions(Rule::dimensions()->maxWidth(config('lappee.allowed_image_size.width'))->maxHeight(config('lappee.allowed_image_size.height')))
],
Related
I created a new factory in Laravel called "CarFactory.php" and I want to use https://github.com/pelmered/fake-car.
This is the fake data that I will insert into my database using Tinker:
return [
'name' => $this->faker->vehicleBrand(),
'founded' => $this->faker->biasedNumberBetween(1998,2017, 'sqrt'),
'description' => $this->faker->paragraph()
];
In the Laravel usage code, I'm not sure where to put this (e.g. Car Model, CarFactory):
$faker->addProvider(new \Faker\Provider\Fakecar($faker));
$v = $faker->vehicleArray();
return [
'vehicle_type' => 'car',
'vin' => $faker->vin,
'registration_no' => $faker->vehicleRegistration,
'type' => $faker->vehicleType,
'fuel' => $faker->vehicleFuelType,
'brand' => $v['brand'],
'model' => $v['model'],
'year' => $faker->biasedNumberBetween(1998,2017, 'sqrt'),
];
Help is needed to know how to set this up.
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()).
im using laravel 7 and im using php faker ..
if i want to seed customer i did this code ..
$factory->define(App\Models\Customer::class, function (Faker $faker) {
return [
'number' => $faker->numberBetween(1,1000000),
'name' => $faker->name,
'foreign_name' => $faker->name,
'financial_account_type_id' => 2,
'address' => $faker->address,
'tax_id' => $faker->numberBetween(1,100000),
'phone' => $faker->e164PhoneNumber,
'mobile' => $faker->e164PhoneNumber,
'email' => $faker->email,
'sales_payment_term_id' => $faker->numberBetween(1,7),
'purchase_payment_term_id' => $faker->numberBetween(1,7),
'note' => $faker->sentence,
];
});
now in my app.php i did this code also ..
'faker_locale' => 'ar_SA',
i want the 'foreign_name' => $faker->name, to be in english language just this line ..
is that possible to change only one line locale ..
thanks ..
I have one form where I have 5 text fields and 2 input type file fields.
for all 7 fields, I have written custom validation rules in laravel and I want all field required if a type is a business so I used required_if in all fields, for text field it's working but for an image (input type file) it's not working it always consider as file present in request and not give any error if file not uploaded when I changed it to required than it only gives me error for input type file.
public function rules()
{
return [
'country' => 'required_if:type,business',
'country2' => 'required_if:type,business',
'company' => 'required_if:type,business',
'number' => 'required_if:type,business',
'expiry' => 'required_if:type,business',
'profile_pic' => 'required_if:type,business | mimes:jpeg,jpg,png,pdf',
'document_pic' => 'required_if:type,business | mimes:jpeg,jpg,png,pdf',
];
}
You can try this:
public function rules()
{
return [
'country' => 'required_if:type,business',
'country2' => 'required_if:type,business',
'company' => 'required_if:type,business',
'number' => 'required_if:type,business',
'expiry' => 'required_if:type,business',
'profile_pic' => 'required_if:type,business|image',
'document_pic' => 'required_if:type,business|image',
];
}
Also can edit mine :https://laravel.com/docs/5.3/validation#rule-mimes
The issue is coming because of extra space invalidation rules.
Removed space between required if and mime type and its working properly.
public function rules()
{
return [
'country' => 'required_if:type,business',
'country2' => 'required_if:type,business',
'company' => 'required_if:type,business',
'number' => 'required_if:type,business',
'expiry' => 'required_if:type,business',
'profile_pic' => 'required_if:type,business|mimes:jpeg,jpg,png,pdf',
'document_pic' => 'required_if:type,business|mimes:jpeg,jpg,png,pdf',
];
}
I am sending a post request to the GetResponse API. Everything works fine until I add a custom field (customFieldValues) to save along with my new email contact.
$body_data =
[
'name' => $input['name'],
'email' => $input['email'],
'campaign' => [
'campaignId' => $campaign_id
],
'customFieldValues' => ['customFieldId' => 'LDe0h', 'value' => ['Save this test string.'] ]
];
When I send the request I get the following error message:
"errorDescription": "CustomFieldValue entry must be specified as array"
I have tried a few things now and not sure how to format this properly to have the API accept it.
Reference link:
http://apidocs.getresponse.com/v3/case-study/adding-contacts
I found the solution on github in an example for their php api here:
https://github.com/GetResponse/getresponse-api-php
I suppose I had to wrap an array inside an array inside of an array...geez:
'customFieldValues' => array(
array('customFieldId' => 'custom_field_id_obtained_by_API',
'value' => array(
'Y'
)),
array('customFieldId' => 'custom_field_id_obtained_by_API',
'value' => array(
'Y'
))
)
For GetResponse V3
'customFieldValues' => [
'customFieldId' => 'custom_field_id_from_API',
'name' => 'Website',
'value' => ['https://scholarshipspsy.com']
]
Please note the 'name' field is optional. The site sampled is an international scholarship platform.