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',
]
);
Related
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>
I have a simple validation rule, to just require a photo being uploaded
yet, some images go through no problem, and some of them tell me, 'image required'.
Am I missing something in my code? what would let some get through and some not?
// getting all of the post data
$file = array('image' => Input::file('image'));
// setting up rules
rules = array('image' => 'required',);
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;
}
The fields I am making required to fill out, should repopulate the correctly filled out fields, while NOT submitting the form and posing errors for the incorrectly filled out fields. What is the best way to do that?
Please note that with this code, I am repopulating the fields as they should be upon submitting the form correctly and they are all displaying when page is reloaded.
<div class="dropdown_structure">
<?php
if($user['location'] == "empty")
{
echo country_dropdown('location');
}
else
{
echo country_dropdown('location',$user['location']);
}
?>
</div>
Also please note that I've tried inserting the value in the input fields.
$data = array( 'name' => 'location', 'value' => $this->input->post('location'));
echo relation_dropdown('location');
Thanks in advance
Hi if you are using the following country dropdown helper you can set value validation in following way
Country dropdown helper
country_dropdown('location',array(),set_value('location'))
even in your second dropdown use set_value('field_name') will work. if validation is failed your selected value will be remain.
echo form_textarea('general4', set_value('general4'), 'class="general"');
the set_value function doesn't seem to work with the textarea so I tried this:
<textarea name='general4' class="general"><?=set_value('general4')?></textarea>
But still not working, any ideas?
to use form_textarea() in CI you pass parameters rows and coloumns as below
$data = array(
'name' => 'txt_area',
'id' => 'txt_area',
'value' => 'johndoe',
'rows' => '5',
'cols' => '10',
'style' => 'width:50%',
);
echo form_textarea($data);
for more details refer CI user guide https://www.codeigniter.com/user_guide/helpers/form_helper.html#form_textarea
What you did is set the name of the textarea field to: 'general4'. I think what you meant to do is return an actual string to your textarea to pre-populate it with data from a post request or a MySQL database or something like that. There are a number of ways to achieve this.
Method 1:
Set a second parameter in the set_value() function eg:
<textarea name='general4' class="general"><?=set_value('general4', $foo)?></textarea>
Method 2:
You could always use the built in form_textarea() function. Docs found here
Examples:
Generic
<?=form_textarea('name', 'value', 'attributs')?>
Case
<?=form_textarea('general4', $general4, "class = 'foo'")?>
From the CI Documentation:
set_value()
Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form.
<input type="text" name="quantity" value="<?php echo set_value('quantity', '0'); ?>" size="50" />
The problem was that I didn't need the textfield to be required. So I didn't set any rules in the action url. So I added this:
$this->form_validation->set_rules('general4', 'general question' , 'trim|xss_clean');
And it worked fine!