I can not customize the error message for mimes
I try the solution given here laravel 5.4 custom error message for MIME not working? but without success
Request :
public function rules()
{
$rules = [
'album'=> 'required',
'add_image.*' => 'image|mimes:png|max:2000',
];
return $rules;
}
public function messages()
{
$messages = [
'album.required' => 'Album is required',
'add_image.mimes' => "Image isn't png",
];
return $messages;
}
Form view :
<form class="addImage" method="post" action="{{route('addImage.form')}}" enctype="multipart/form-data">
{{csrf_field()}}
<select name="album">
<option value="">-----</option>
#foreach($albums as $album)
<option value="{{$album}}">{{$album}}</option>
#endforeach
</select>
<input multiple="multiple" name="add_image[]" type="file" />
<button class="buttonADM" type="submit">Send</button>
</form>
Thanks for your help and sorry for my bad english
You are validating an array, so try this instead:
'add_image.*.mimes' => "Image isn't png",
I used your form on my project, and tried this:
$valid = Validator::make(request()->all(), [
'add_image.*' => 'image|mimes:png'
], [
'add_image.*.mimes' => 'Image isnt png'
]);
dd($valid->errors());
// output
{
"add_image.0": [
"Image isnt png"
],
"add_image.1": [
"Image isnt png"
],
"add_image.2": [
"Image isnt png"
]
}
and I got the custom message. So please check your error bag if it contains the correct messages, and make sure that you print it okay.
i have not same result
Controller
public function addImage(/*addImageRequest $request*/){
if(session('user')['isA'] == 1){
$valid = Validator::make(request()->all(), [
'add_image.*' => 'image|mimes:png'
], [
'add_image.*.mimes' => 'Image isnt png'
]);
dd($valid->errors());
}
}
Return :
MessageBag {#224 ▼
#messages: array:1 [▼
"add_image.0" => array:1 [▼
0 => "The add_image.0 failed to upload."
]
]
#format: ":message"
}
Related
this is data from the skills column of the resumes table:
{"en_title":"resume title in english","en":["item 1","item 2"]}
I want to update the en column in livewire. I can add an item to the en array but can't save or update the database. this is livewire Class:
public Resume $resume;
public $en;
protected $rules = [
'en' => 'string',
];
public function save()
{
$this->validate();
$items = $this->resume->skills['en'];
$items[] = $this->en;
$this->resume->update(['skills.en' => $items]);
}
And View:
<form wire:submit.prevent="save">
<input type="text" wire:model.defer="en">
<button type="submit">ADD</button>
</form>
dd of $this->resume->skills['en'] is:
array:2 [▼
0 => "item 1"
1 => "item 2"
]
And dd of $items[] = $this->en is:
array:3 [▼
0 => "item 1"
1 => "item 2"
2 => "new item"
]
but can't save in database by: $this->resume->update(['skills.en' => $items]);
I think the skills.en name has problem but can't solve.
$this->resume->update([
'skills' => [
'en' => $items
],
]);
I’m working on a CRUD system for inventory management, in which images for each product should be included. Every time that I try to save the path of the image in the DB this error appears:
Undefined variable: image
My controller looks like this:
public function store(Request $request)
{
if (Auth::user('logistics')) {
$product = $this->validate(request(), [
'Product_Name' => 'required',
'Amount' => 'required|numeric',
'MinAmount' => 'required|numeric',
'Status' => 'required',
'Supplier' => 'required',
'WebLink' => 'required',
]);
if ($request->hasFile('Product_Image')) {
$image = Storage::putFile('public/pictures/LogInv/', $request->Product_Image);
}
$product['Product_Image'] = $image;
$product['Employee_id'] = Auth::user()->id;
LogisticsInv::create($product);
return back()->with('success', 'Product has been added');
} else {
return view('/restricted_area');
}
}
and my input looks like this:
<form method="post" action="{{url('loginv')}}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="row">
<div class="col-md-12"></div>
<div class="form-group col-md-12">
<label for="Product_Image">Product Image:</label>
<input type="file" class="form-control" name="Product_Image">
</div>
</div>
and dd($request->all()); delivers this
array:8 [▼ "_token" => "P7m8GP4A35G1ETUosduBSWtMpJuPaNILn2WI6Al3"
"Product_Image" => "6.jpg" "Product_Name" => "asd" "Amount" =>
"123" "MinAmount" => "1" "Status" => "Ok" "Supplier" => "asd"
"WebLink" => "asd" ]
Change your code to
public function store(Request $request)
{
if (Auth::user('logistics')) {
$product = $this->validate(request(), [
'Product_Name' => 'required',
'Amount' => 'required|numeric',
'MinAmount' => 'required|numeric',
'Status' => 'required',
'Supplier' => 'required',
'WebLink' => 'required'
]);
if ($request->hasFile('Product_Image')) {
$image = Storage::putFile('public/pictures/LogInv/', $request->Product_Image);
$product['Product_Image'] = $image;
}
$product['Employee_id'] = Auth::user()->id;
LogisticsInv::create($product);
return back()->with('success', 'Product has been added');
} else {
return view('/restricted_area');
}
}
I'm trying to use old() in Laravel but it doesn't work.
In my controller:
dd(back()->withInput());
this is the session result
#session: Store {#364 ▼
#id: "dyNmtFJVQCQmcCob4SPYEBzWHJ6TJeM9X0mzWWu7"
#name: "laravel_session"
#attributes: array:6 [▼
"_token" => "NVJATvxuHRlGbAC1jUEIjS6gbLQQEWPIIV41fLYd"
"url" => []
"_previous" => array:1 [▼
"url" => "http://localhost:8000/units/23/rents/create"
]
"_flash" => array:2 [▼
"old" => []
"new" => array:1 [▼
0 => "_old_input"
]
]
"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d" => 1
"_old_input" => array:5 [▼
"_token" => "NVJATvxuHRlGbAC1jUEIjS6gbLQQEWPIIV41fLYd"
"contract_id" => "20"
"unit_id" => "23"
"amount_paid" => "1"
"submit" => "submit"
]
]
#handler: FileSessionHandler {#363 ▶}
#started: true
value="{{ old('amount_paid')}}" I would get empty field
value="{{ old('amount_paid','test') }}" I would get 'test' in the field
I'm using Laravel 5.6
EDIT
to clarify the above code it to debug
this is my original code
controller
return back()->withInput();
view
<div class="form-group">
<label> amount paid </label>
<input type="float" class="form-control" name="amount_paid" value="{{ old('amount_paid')}}" required>
</div>
adding the method that handle the request
public function store(Request $request)
{
// updating the amount paid
$rent = new Rent;
$rent->amount_paid = $request->amount_paid;
try {
$rent->save();
} catch (\Illuminate\Database\QueryException $e) {
//return to the form and populate it
return back()->withInput();
}
return redirect('units/'.$request->unit_id);
** update **
It appear that session->flash is not working.
I tried a new installation of laravel and it is working fine
The problem was related to session it self.
I have 2
\Illuminate\Session\Middleware\StartSession::class,
one in middleware and another in $middlewareGroups[web].
this was a result of following a tutorial in youtube months ago to implement localisation :(
I have this input but its not working because i have in name []. Any suggestion how can i fix this? If i remove this [{{$language->code}}] required is working.
#foreach ($languages as $language)
<input type="text" id="text-title" name="article_title[{{$language->code}}]" value="" class="form_input" required="required">
#endforeach
<button type="submit" class="submit_property bg_green pull-right">CREATE ARTICLE</button>
validation rules:
public function rules()
{
return [
'article_title' => 'required:articles',
'slug' => 'required|unique:articles',
}
Problem is that i need required rule only if $language->id = 1
You can use Laravel's native validation of array:
$validator = Validator::make($request->all(), [
'article_title.*' => 'required',
]);
The rule will be
public function rules()
{
return [
'article_title.*' => 'required',
'slug' => 'required|unique:articles',
];
}
I have this input, where i enter for languages some text. Now i want to set required field but only if its language code [de]. When i do like this i get required field for all languages. Any suggestion how can i fix this?
#foreach ($languages as $language)
<input type="text" id="text-title" name="article_title[{{$language->code}}]" value="" class="form_input" />
#endforeach
public function rules()
{
return [
'article_title[de]' => 'required:articles',
'slug' => 'required|unique:articles',
'article_intro[de]' => 'required:articles',
'article_content[de]' => 'required:articles',
'article_category[de]' => 'required|exists:categories,id',
];
}
EDIT:
I found this solution, it works for me.
https://laracasts.com/discuss/channels/general-discussion/laravel-5-dynamic-form-validation?page=1
You can check langauge with reuquest if you have url like: www.xxxxx.com/de or you can pass hidden input with lang value and than:
<input type="hidden" name="lang" value="de">
Request:
if(request()->segment(2) == 'de') {
return [
'article_title[de]' => 'required:articles',
'slug' => 'required|unique:articles',
'article_intro[de]' => 'required:articles',
'article_content[de]' => 'required:articles',
'article_category[de]' => 'required|exists:categories,id',
]
} else {
return [];
}
or with hidden input
if(request()->lang == 'de') {
return [
'article_title[de]' => 'required:articles',
'slug' => 'required|unique:articles',
'article_intro[de]' => 'required:articles',
'article_content[de]' => 'required:articles',
'article_category[de]' => 'required|exists:categories,id',
]
} else {
return [];
}