how to validate multiple input jquery repeater in laravel 6 - laravel

I would like to validate my form using jquery repater and how to get value
this is my input :
<input type="text" class="form-control" id="test_1_qty" placeholder="0" data-name="qty" name="test[1][qty]">
this is my controller :
$rules = [
'qty' => 'required',
];
$message = [
'qty.required' => 'This field is Required',
];
$validator = Validator::make($request->all(), $rules,$message);

You are getting your qty field from an array that is test
$rules = [
'test.*.qty' => 'required',
];
$message = [
'test.*.qty.required' => 'This field is Required'
];

Related

Laravel error: (1/1) ErrorException count(): Parameter must be an array or an object that implements Countable

I'm trying to update data from my website to the database but I have the error given in the title.
Part of my Controller:
$user = auth()->user();
$count = count($request->get('alt_rs','alt_ci','linkrs'));
// On recupere les données de la BDD dans la variable $data
for($i = 0 ; $i < $count ; $i++) {
$data = [
'nom' => $request->get('nom'),
'prenom' => $request->get('prenom'),
'date_de_naissance' => $request->get('date_de_naissance'),
'job' => $request->get('job'),
'adresse' => $request->get('address'),
'code_postal' => $request->get('code_postal'),
'ville' => $request->get('ville'),
'telephone' => $request->get('phonenumber'),
'accroche' => $request->get('accroche'),
'email' => $request->get('email'),
'permis_b' => $request->get('permis'),
'photo_profil' => '../public/img/'. $filename,
'password' => $request->get('password'),
'logo_rs' => '../public/img/'. $filename,
'logo_ci' => '../public/img/'. $filename,
'description_ci' => $request->get('altci')[$i],
'description_rs' => $request->get('altrs')[$i],
'url' => $request->get('linkrs')[$i],
];
View example:
#foreach ($contact_info as $contact)
<label for="logo_rs">
<input type="file" id="logo_rs" name="logo_rs[]" accept="image/png, image/jpeg" >
</label>
<label for="linkrs">
<input type="text" placeholder="lien réseau social" name="linkrs[]" id="linkrs" value="{{$contact['url']}}">
</label>
<label for="altrs">
<input type="text" name="altrs[]" placeholder="Descriptif" id="altrs" value="{{$contact['description_rs']}}">
</label>
<br>
#endforeach
What can I do to resolve this?
The request->get('a','b','c') you are using is not returning an array. Try putting all the data you need in an array before using the count function. Something like:
$countable[] = request->get('a');
The parameter of the count() function must be iterable (array,object)
Problem resolved i just had to choose one thing in $count
$count = count($request->get('alt_rs','alt_ci','linkrs'));
became
$count = count($request->get('linkrs'));

Store image path in a DB

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');
}
}

Required input is not working?

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',
];
}

Field is required when other three fields are empty

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

How to apply a validation rule to every element present within an array?

How to apply validation rules to every item within an items[] array? For example:
...->validate($request, [
'items[]' => 'required' // <-- what is the correct syntax?
]);
Try something like this
$validator = Validator::make($request->all(), [
'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',
]);
Where person is the name of the input field and email is the key
Laravel 5.2 has an array validation all you need to do is :
In your view assuming that you have an inputs like this :
<input type="text" name="example[]" />
<input type="text" name="example[]" />
The [] are the key for this :)
And in your controller you can just do :
$this->validate($request, [
'example.*' => 'required|email'
]);
$this->validate($request, [
'items' => 'required|array',
'items.*.title' => 'required',
]);

Resources