Field is required when other three fields are empty - laravel

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

Related

how to validate multiple input jquery repeater in laravel 6

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

How to validate form field with array values[] in laravel?

I'm trying to validate form field with array value.
$this->validate($request, [
'includes' => 'required',
'excludes' => 'required'
]);
HTML is as follows:
<input type="text" name="excludes[]" id="package_exclude"
class="form-control">
<input type="text" name="includes[]" id="package_include"
class="form-control">
The outputted value of $request->includes gives ["Include Value One","Include Value Two"] which is working fine..
But the validation doesn't works..
I suppose you want the arrays to have at least one value? In this case, you can use the min validation rule:
$this->validate($request, [
'includes' => 'required|min:1',
'excludes' => 'required|min:1'
]);

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

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

Solving Method Not Allowed Http Exception in Laravel 5.2

I want some values in my table editable, so I created this simple custom form. But this will throw error of method not allowed http exception.Any help?
<form action="{{ url('/idx-test/update-this-student/'. $student->id)}}" class="" method="POST">//changing this to put, patch does not solve the error
Route
Route::post('/idx-test/update-this-student/{id}', 'StudentController#updateThisStudent'); //again changing this to patch,or put does not help
Controller
public function updateThisStudent(StudentRequest $request, $id)
{
$student = Student::findOrFail($id);
$student->update($request->all());
// return redirect('city');
echo "updated";
}
StudentRequest
public function rules()
{
return [
'firstname' => 'required|alpha|min:2|max:10',
'lastname' => 'required|alpha|min:2|max:10',
'bday' => 'required|date',
'address' => 'required|min:10',
'zip' => 'required|min:4|max:10',
'phone' => 'required|digits:7',
'mobile' => 'required|digits:11',
'email' => 'required|email',
'city_id' => 'required',
'yearlevel_id' => 'required',
'section_id' => 'required',
];
}
By adding this small piece of code
<input type="hidden" name="_method" value="PATCH">
My problem solve

Resources