Custom error message for Laravel validation rule: Dimensions - php

I'm trying to validate an image upload that looks like the following:
$this->validate($request, [
'admin_image'=> 'nullable|image|dimensions:min_width=600,min_height=600',
]);
when the selected image too small then laravel shows error:
The Admin Image has invalid image dimensiona
I think that message is not saying specifically that in which dimension the image small, eg: width or height.
I'm expecting error message like:
The Admin Image width cannot be less than 600px and
The Admin Image height cannot be less than 600px
here 'Admin image' is the attribute name & '600' is the value I given in rules
So, I wanted to make a custom error message for min_width and max_widthin custom messages array on validation.php, that look like the following:
'admin_image' => [
'dimensions.min_width' => 'The :attribute dimension (width) cannot be less than :min_width px',
'dimensions.min_height' => 'The :attribute dimension (height) cannot be less than :min_height px',
],
But unfortunately that doesn't work & Laravel continues to show the default message.
Please understand that
I need 2 separate error message for dimensions:min_width=600 & dimensions:min_height=600
like I tried in the custom error messages array.
I know this is very simple but I'm doing something wrong.
Any help will be highly appreciated

Add the custom error message in validation.php as below:
'admin_image' => [
'dimensions' => [
'min_width' => 'The :attribute dimension (width) cannot be less than :min_width px'
]
]

Related

How validate a require field only when another has a value laravel 5.7?

I have a form that have 2 options, image or video, only one of these are required at the same time, so if the user send a image the video field is not longer required, but the video field could still be present and empty in the payload, i have a form request where i need something like this.
'image' => 'required_if_empty:video|image|max:3000',
'video' => 'required_if_empty:imagen|url|max:255'
if image is empty and video is not empty: pass
if image is not empty and video is empty : pass
if image is not empty and video is not empty : fail
if imge is empty and video is empty : fails
This should work:
'image' => 'required_without:video',
'video' => 'required_without:image',
However this will not satisfy following condition:
if image is not empty and video is not empty : fail
I would leave this as a comment as it's not complete answer but I don't have enough points and quote below seems to contradict the quote above so this might be what you're looking for.
if the user send a image the video field is not longer required, but the video field could still be present and empty in the payload
For more check predefined validation rules here, and custom validation rules here.

Laravel Image Upload Validation Fails

When I try to upload an image within Laravel the validation is not working. Here my validation string...
'cover' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
The validation error I get is The cover must be an image. It does work however when I only have the required rule.
What is the problem?
You can remove the rule 'image' on your validation :
'cover' => 'required|mimes:jpeg,png,jpg,gif,svg|max:2048',
Hope it help u :)
try this
'cover' => 'required|image|max:2048',

Image validation with multiple dimensions not working

so Im using Laravel 5.6 and have this validation rule like this:
$request->validate([
'image-x' => 'image|mimes:jpeg,png|max:4096|dimensions:
max_width=2000,
max_height=2000,
min_width=512,
min_height=512,
ratio=1/1'
]);
I would like to have pictures that are bigger than 512x512px, smaller than 2000x2000px, and with aspect ratio 1:1. I've been playing with this for a good hour now (different solutions from google), but anytime I insert an image with dimensions like 3840x1080px or so, it passes the validation. Does anybody have experience with setting multiple dimensions values? Thanks.
may be work try this:
$request->validate([
'image-x' => 'image|mimes:jpeg,png|max:4096|'.Rule::dimensions()->minWidth(512)->minHeight(512)->maxWidth(2000)->maxHeight(2000)->ratio(1/1)]);

Mailchimp api v3 - can't create segment based on a TEXT merge field

I'm trying to create a segment based on a new merge field of type TEXT that I just created, by using condition_type = TextMerge since it seems to be the only option from their documentation that matches my field:
http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#create-post_lists_list_id_segments
However, the description for TextMerge looks identical to EmailAddress. To be more specific they both apply only for the EMAIL / MERGE0 field.
TextMerge vs EmailAddress
I tried the following combinations for 'conditions':
{
'condition_type': 'TextMerge',
'field': 'EVENTS',
'op': 'contains',
'value': 'test'
}
and
{
'condition_type': 'TextMerge',
'field': 'EMAIL',
'op': 'contains',
'value': 'test'
}
The first one returns an error:
{
"type":"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/",
"title":"Invalid Resource",
"status":400,
"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
"instance":"",
"errors":
[
{
"field":"options.conditions.item:0",
"message":"Data did not match any of the schemas described in anyOf."
}
]
}
The second one works.
My question is: how can I create a segment based on a custom merge field of type TEXT? To me this looks like a bug from their side. Did anyone else have this problem? Does anyone have a solution?
I finally managed to solve my issue specific to TEXT fields in my mailchimp audience merge tags.
Apparently, when adding a TEXT field, it defaults to a maximum size of 25 characters. I was trying to put more text than that into it. It fails with the error:
#body={
"title"=>"Invalid Resource",
"status"=>400,
"detail"=>"The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
"errors"=>[{"field"=>"merge_fields.MMERGE_16",
"message"=>"Data did not match any of the schemas described in anyOf."}]
}
I increased the field size to 255 via Mailchimp's API.

Laravel: Display html in app/http/requests custom error messages

I'm working on a custom request validator with custom error messages for Laravel 5. Is there a way I could display HTML in returned messages?
My goal is to be able to include links in the message, ie:
"You need a lottery ticket number. Don't have one? <a href='#'>Register Here.</a>"
I've searched different forums and couldn't find a solution. Below isn't applicable in my case:
https://laracasts.com/discuss/channels/general-discussion/html-in-httprequests-custom-error-messages
EDIT:
SOLVED.
I'm using custom messages set in a custom Request:
public function messages()
{
return [
'dob.eighteen' => 'You must be 18 or older to take part in this competition.',
'photo.required' => 'You must upload a photo of the space.',
'photo.mimes' => 'The uploaded file must be a photo in .jpeg or .png format.',
'name.required' => 'Please enter your full name.',
'ticket.required' => 'You need a ticket number to enter. Don\'t have one? Register Here.',
'email.required' => 'The e-mail field is required.',
'dob.required' => 'The date of birth is required.',
'postcode.required' => 'The date of birth is required.',
'postcode.postcode' => 'The postcode you entered is incorrect.', ];
}
The reason why the above link wasn't applicable is because I want to keep my code neat and store all the error messages in one file together with request validation rules.
The solution was so simple that I completely missed it before.
Thanks for helping.
The errors are displayed using {{ $error }} tag in a blade template.
To display a message that contains html = If you don't want the data to be escaped, you should use the following syntax: {!! $error !!}
Thanks for helping.

Categories