How to define image dimension rule message in laravel - laravel

I have the following image validation for my POST request and I am not quite sure how to add custom validation message specifically for min_width, min_height and ratio?
protected function getRules(): array
{
return [
'logo' => 'required|file|mimes:jpg,jpeg,png|dimensions:min_width=400,min_height=400,ratio=1/1'
];
}

You need to define your message like this:
'logo.dimensions' => 'The logo must be at least :min_width x :min_height pixels and have ratio :ratio',

'logo.dimensions' => 'The logo must be at least 400 x 400 pixels!',

Your validation:
"logo" => "dimensions:min_width=400,min_height=400"
Your form, multipart/form-data:
<form method="POST" enctype="multipart/form-data"></form>

Related

Laravel and Images validation

I have a little question about Laravel ...
I have a big form with which it is possible to send images.
I did a "PageStoreRequest" with my validation rules and everything works fine.
Depending on the length of the form, I would like that if an image is not valid, it is simply "deleted" (or "ignored") from the form and that the rest of the form is treated correctly ... So that the rest of the images are not to be sent again ...
I wonder if there is a method that would check a single field?
(like my Image Field)
And if it is not valid, delete it?
(knowing that the other fields must also pass to the Validator)
Thanks for your ideas :)
You can validate image field.
$request->validate([
'image' => 'mimes:jpeg,jpg,png'
]);
And then field is validated;
if ($request->hasFile('image') && $request->file('image')->isValid()) {
// Make something
}

Laravel 5.8.17 - validation image not working as expected

I'm creating Laravel validation, and meet strange issue.
I have a form with input type file, where I can attach multiple files.
<input type="file" name="img[]" class="form-control" multiple="multiple">
In the Controller I have validation rules:
$validationRules = [
'img' => 'required|image'
];
And all the time my validation doesn't work, I always get a message:
"The img must be an image."
There is no difference when I upload 1 file or 2 or 3 at the same time - error is the same. There are another fields at the same form (text fields) and all is ok with them, problem appears only with image. Tried to remove "image"
from the validation and replace it with mimetypes or mimes - anyway system can't recognize image.
Please advice what could be a problem here, thanks a lot.
You need to make validation attribute img to img.*
$input_data = $request->all();
$validator = Validator::make(
$input_data, [
'img.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
],[
'img.*.required' => 'Please upload an image',
'img.*.mimes' => 'Only jpeg,png and bmp images are allowed',
'img.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
]
);

Drupal ajax form, partial render

Good day.
I created custom form in my module and defined submit button like this:
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#ajax' => array(
'callback' => 'fmg_addbanner_ajax_callback',
'method' => 'replace',
'wrapper' => 'banner_add_wrapper'
)
);
Then outputted my form like this:
$form = drupal_get_form('fmg_banner_add_form', $region_id);
print render($form);
But ajax requests don't work. I think because of there are no needed drupal js files. How can I solve this problem? Thanks.
The normal case will be to have #ajax attached to a common input, like checkbox, text box etc.
The behavior is that when user change the input value, it'll fire your callback through ajax and send entire form over, and then you can process this form behind the scene and adjust certain things before sending the form back and change the element to the wrapper you defined.
One of the question that I had for partial form rendering is when I need to do some ajax call or inject partial form element to certain area of the page while keeping the integrity of the form rendering. (I don't know exactly what form build did, i guess i don't want to know)
Since form rendering uses the renderable array, if you just want to extract certain element, you could just do
$form = drupal_get_form('application_form');
$body = render($form['body']);
The following is a real example that i took a form and split into header, footer and the rest of form elements.
// get a form for updating application info.
$form = drupal_get_form('pgh_awards_review_application_form', $app);
$elements = array();
$form_render = render($form);
$elements = array(
'qualify' => render($form['qualify']),
'threshold_met' => render($form['threshold_met']),
'submit' => render($form['submit']),
);
if (preg_match('/<form.+div>/', $form_render, $matches)) {
$elements['header'] = $matches[0];
}
if (preg_match('/<input type="hidden".+form>/s', $form_render, $matches)) {
$elements['footer'] = $matches[0];
}
$vars['form'] = $elements;
It's definitely not pretty approach, it just serves the needs at the moment.

Laravel 3 - How to validate checkbox array, for at least 1 checked?

I'm starting to learn Laravel and still on the learning curve. Now I'm starting with Laravel 3 but will most probably switch my project into Laravel 4 once I get something working.
Now the question being, how to validate an array of checkbox, I want to validate that at least 1 inside the group is enable(checked). I read somewhere on Laravel forum that we just validate them using a required, but when I dd(input::all()) I don't see anything else but the inputs field and checkbox are not part of them...
Part of my Blade Create code for the checkbox:
<label class="checkbox">{{ Form::checkbox('changeReasons[]', 'ckbCRCertification', Input::had('ckbCRCertification'), array('id' => 'ckbCRCertification')) }} Certification</label>
<label class="checkbox">{{ Form::checkbox('changeReasons[]', 'ckbCRDesignCorrection', Input::had('ckbCRDesignCorrection'), array('id' => 'ckbCRDesignCorrection')) }} Design Correction</label>
My controller (REST) code is:
public function post_create()
{
print "Inside the post_create()";
// validate input
$rules = array(
'ecoNo' => 'min:4',
'productAffected' => 'required',
'changeReasons' => 'required'
);
$validation = Validator::make(Input::all(), $rules);
if($validation->fails())
{
return Redirect::back()->with_input()->with_errors($validation);
}
$eco = new Eco;
$eco->ecoNo = Input::get('ecoNo');
$eco->productAffected = Input::get('productAffected');
$eco->save();
return Redirect::to('ecos');
}
I also want to know the correct code for getting the checkboxes state after a validation fails, I thought I saw the Input::had(checkBoxName) somewhere but that doesn't seem to work, I'm probably not using it correctly and I'm getting a little confuse on that since all example I see are for inputs and nothing else. I assume the validation is roughly the same in L4, would it be?
Going back on this project and making some more researches, I have found the best way for this problem is the following.
My blade view:
<div class="control-group row-fluid">
<?php $arrChangeReasons = Input::old('changeReasons', array()); // array of enable checkboxes in previous request ?>
<label class="checkbox">{{ Form::checkbox('changeReasons[]', 'certification', in_array('certification', $arrChangeReasons)) }} Certification</label>
<label class="checkbox">{{ Form::checkbox('changeReasons[]', 'designCorrection', in_array('designCorrection', $arrChangeReasons)) }} Design Correction</label>
</div>
The explanation of the blade view is a 2 steps process, after a validation occur, is the following:
Pull the checkbox array (in my case 'changeReasons[]') with Input::old
From that array we can then search for individual checkbox and see if they are in there, if they are then change the checkbox as a checked state. That is the job of the in_array() function, returning a true/false will change the state of the checkbox.
My controller (REST) code is exactly as it was written in my question at the beginning. For more information, defining $rules = array('changeReasons' => 'required'); will make sure that at least 1 of the checkboxes is checked.
Please remember that Checkboxes need a value like .
It the Checkbox is checked Input::get('foo') will return 1, but if it is unchecked it will return nothing, because it is not in the post-array.
I'm using this code:
if(Input::get('foo')){
$bar->is_foo = 1;
}
else{
$bar->is_foo = 0;
}

Drupal Webform Validation (webform_form_alter)

I'm doing some webform validation using webform_form_alter. I'm using webform_form_alter because I switch certain content on a "select" field.
In my webform-form-317.tpl.php I defined new fieldsets I set my fields into this new fieldset and unset the original from the webform.
$form['submitted']['ContactInfo'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="ContactInfo">',
'#suffix' => '</div>',
'#weight' => -10,
'#title' => 'Contact Information'
);
$form['submitted']['ContactInfo']['phone_home'] = $form['submitted']['phone_home'];
unset($form['submitted']['phone_home']);
in my webform_form_alter I have the following code:
switch ($form_id)
{
case 'webform_client_form_317':
{
$form['#validate'][] = 'validate_form';
}
}
My Validate_form function looks like:
function validate_form($form_id, $form_values)
{
if ($form_values['submitted_tree']['ContactInfo']['phone_home'] == "")
{
form_set_error('phone_error', t('Please enter a home phone number.'));
}
}
The issue is that the $form_values['submitted_tree']['ContactInfo']['phone_home'] comes back as nothing even is the user has inputted something into the textfield.
Any suggestions on what i'm doing wrong?
As a second question in case somebody also the answers, how do I modify the of these textfields to set the class for "form-text required error" so they show up in red with the rest of the mandatory fields.
Thanks
I hope you don't write this code in the webform module, but have made your a custom module for it.
In the first case, your function should be
function validate_form($form, &$form_state) {
if ($form_state['values']['submitted_tree']['ContactInfo']['phone_home'] == "") {
form_set_error('phone_home', t('Please enter a home phone number.'));
}
}
If you are talking about the error class, Drupal add it to all fields that has an error set like is done on the above code. You need to pass in the name of the form field as first param to the form_set_error function.

Resources