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 [];
}
Related
I am trying to add a row to my database for the objects type "Event". Whenever I press the create button on my HTML form, I get the error "Undefined index: location".
This is my save function:
public function save(CreateEvent $request)
{
$validated = $request->validated();
$event = new Event();
$event->event_name = $validated['name'];
$event->event_description = $validated['description'];
$event->event_location_id = $validated['location'];
if ($validated['website'] != null) {
$event->event_website = $validated['website'];
}
if ($validated['facebook'] != null) {
$event->event_facebook = $validated['facebook'];
}
if ($validated['twitter'] != null) {
$event->event_twitter = $validated['twitter'];
}
if ($validated['instagram'] != null) {
$event->event_instagram = $validated['instagram'];
}
$starttime = strtotime($validated['starttime']);
$event->event_start_time = date('H:i', $starttime);
$event->event_duration = $validated['duration'];
$event->event_day = $validated['day'];
if ($validated['image'] != null) {
$imageName = time().'.'.request()->file('image')->getClientOriginalExtension();
$event->event_image = $imageName;
request()->image->move(public_path('images'), $imageName);
}
$event->save();
return redirect()->route('event.show', ['event_id' => $event->event_id]);
}
This is my Form Request:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateEvent extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required',
'description' => 'required',
'location' => 'required',
'starttime' => 'required',
'duration' => 'required',
'day' => 'required',
'website' => '',
'twitter' => '',
'facebook' => '',
'instagram' => '',
'image' => '',
];
}
public function messages() {
return ["Invalid input"];
}
}
Here is the relevant part of the HTML:
<div class="input-wrapper">
<label for="location">Location *</label>
<select id="location">
#foreach ($event_locations as $location)
<option value="{{$location->location_id}}">{{$location->location_name}}</option>
#endforeach
</select>
</div>
When I press create, I get the following error:
ErrorException
Undefined index: location
This is the line the error is on: $event->event_location_id = $validated['location'];
Help is appreciated
your select tag is is missing name attribute and the value is not passing with the form. so location index is missing in $request. just add the name attribute in the select tag.
<select id="location" name="location">
#foreach ($event_locations as $location)
<option value="{{$location->location_id}}">{{$location->location_name}}
</option>
#endforeach
</select>
Your select element does not have a name.
<select id="location">
must be
<select name="location" id="location">
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"
}
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 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 4 input fields. 1 of them has to be filled.
My fields :
<input name="name" placeholder="Name">
<input name="hair_style" placeholder="Style">
<input name="hair_color" placeholder="Color">
<input name="options" placeholder="Options">
My function
$this->validate($request, [
'name' => 'required_if:hair_style,0,',
]);
So when hair_style is 0. Input field name has to be filled. This works but.. I want it like this below but I don't know how:
$this->validate($request, [
'name' => 'required_if:hair_style,empty AND hair_color,empty AND options,empty,',
]);
It has to work like this. When hair_style, hair_color and options are empty name has to be filled. But is this possible with required_if ?
You can try as:
'name' => 'required_if:hair_style,0|required_if:hair_color,0||required_if:options,0',
Update
You can conditionally add rules as:
$v = Validator::make($data, [
'name' => 'min:1',
]);
$v->sometimes('name', 'required', function($input) {
return ($input->hair_style == 0 && $input->hair_color == 0 && $input->options == 0);
});
You can add more logics in the closure if you required...like empty checks.
So all I had to do was :
$this->validate($request, [
'name' => 'required_without_all:hair_style, hair_color, options',
]);
for more information check https://laravel.com/docs/5.3/validation#rule-required-without-all