Laravel - Validation - Require if field is null - php

Let's say I have this html code:
<input type="email" name="email">
<input type="password" name="password">
Or this:
<input type="hidden" name="user_id" value="1">
(If the user is logged in, only the user_id field will be shown. else - the credentials fields).
When I create a request, there's a validation that checks the user. if the field user_id exists (i.e if user_id exists in the users table), then there's no need to require email and password inputs. If there's no user_id, then the email and password fields will be required.
In other words, I want to do something like this:
public function rules()
{
return [
'user_id' => 'exists:users,id',
'email' => 'required_if_null:user_id|email|...',
'password' => 'required_if_null:user_id|...'
];
}

After reading the Validation docs again, I found a solution. I just needed to do the opposite, using the required_without validation:
public function rules()
{
return [
'user_id' => 'exists:users,id',
'email' => 'required_without:user_id|email|unique:users,email',
'password' => 'required_without:user_id',
}

Related

Laravel 9 required_if not validating

I'm using Laravel 9 and Livewire 2.0
I have an integer field called 'new_weight' that should validate required if the boolean checkbox 'stripped' is selected. Although 'stripped' is not selected, it's still validating as required. The validation $rules are being hit because if I remove the rule, it doesn't validate at all.
I also recently noticed that if I die and dump '$this->stripped' when the checkbox is selected, it dumps 'true' to the console and not '1', but if I leave unchecked, it dumps '0'--not sure if this matters.
edit.php
...
protected $rules = [
'car_color' => 'required|string',
'new_weight' => 'required_if:stripped,accepted|integer|min:1|max:999',
];
protected $messages = [
'car_color' => 'Please enter car color.',
'new_weight' => 'New Weight Value must be great than 1 but less than 999'
];
...
edit.blade.php
...
<div class="flex items-center h-5">
<input wire:model.defer="stripped" id="stripped" name="stripped"
wire:click="updateStripped"
type="checkbox">
</div>
<div>
<input wire:model.defer="new_weight" id="new_weight" name="new_weight"
type="text"
placeholder="New Vehicle Weight in lbs:">
</div>
...
Since stripped itself is not a rule, I would consider using a custom callback to evaluate the stripped input.
'new_weight' => [
Rule::requiredIf(function () use ($request) {
return $request->input('stripped');
}),
'integer|min:1|max:999'
]
I have this problem too, so I use Rule::when like this and get me same result:
[Rule::when(this->stripped == 'accepted', 'required|integer|min:1|max:999')]
when condition ( this->stripped == 'accepted' ) is true, the rules run
of course if stripped is a check box, you must find out what that's return, so first use dd for that, then replace that with 'accepted' ( I think check box return true and false )
The new_weight field had a default null value in the database. Once I re-migrated with a default empty value, my problem was solved.

Laravel 5.6: Skip validation step if field exists and isn't changed. If field IS changed it is Required - but not unique

I have situation where an admin edits an employee form: The first name, last name, and SSN are required on ADDING an employee. No problems there. Where I have an issue is when EDITING the form. I have no problem validating the SSN as it is a unique field.
'ssn_edit' => 'required|unique:employees,ssn,' . $id
But what I DO have an issue with is the non-unique fields. I don't know how to set the validation to skip by ID when the field is NOT unique. Here is the entire rules section of the FormRequest:
public function rules()
{
$id = $this->input('employee_id');
return [
'first_name' => 'required',
'last_name' => 'required',
'ssn_edit' => 'required|unique:employees,ssn,' . $id
];
}
Obviously - this throws the validation error on first_name and last_name regardless if the field is populated or not.
Any help some of you Laravel gurus can throw my way would be GREATLY appreciated!
There is so many tricks you can do to solve the problem. But I only got two ways..
The first is: Prevent to send your ssn_edit value when you want to edit the employee
example:
<input type="text" value="{{ isset($employee) ? $employee->ssn_edit : old('ssn_edit') }}" #isset($employee) disabled #endisset name="ssn_edit">
public function rules()
{
$id = $this->input('employee_id');
return [
'first_name' => 'required',
'last_name' => 'required',
'ssn_edit' => 'required|sometimes|unique:employees,ssn,' . $id
];
}
The second is: Check your method before validate the employee.. is it POST or PUT, if it's PUT don't add the unique rule in your validation.
Conclusion: The validation will work every time you call the validation, no matter if you edit or add new employee. #CMIIW
You mustn't have error with validating non-unique fields, probably you have wrong edit form, set value attribute to input like
<input type="text" value="{{ $employee->first_name }}" name="first_name">

Laravel: How to make a field required only if another specific field exists?

I have a form that asks for several files, and description of these files. Something like
<input type="file" name="file1">
Describe your file:
<input type="text" name="desc1">
I want the user to describe the contents of the file, instead of only showing something like Invoices-final-FinalV30.docx he might say "Invoices for January, 2018", so when I validate the form, I know how to ask if a field follows a regex, or if the field is required and so on, using the validate() method, but I want something custom, something that makes "desc1" required ONLY if there's a "file1", if there's no "file1" I can safely ignore whatever "desc1" carries.
Try required_with:anotherfield validation
https://laravel.com/docs/5.7/validation
$validator = Validator::make(
$request->all(),
[
'file1' => 'mimes:jpeg,bmp,png', //your file validation
'desc1' => 'bail|required_with:file1' //add other description validations
]
);
For array fields, example named upload[][file] , upload[][desc]
$validator = Validator::make($request->all(), [
'upload.*.file' => 'mimes:jpeg,bmp,png',
'upload.*.desc' => 'bail|required_with:upload.*.file',
]);

Validate 2 fields should be the same value without one of them has _confirmation in its name in laravel 5

I have debit field and debit field
<input type="text" class="form-control" name="debit" value="{{ isset($expense->debit) ? $expense->debit : old('debit')}}">
and
<input type="text" class="form-control" name="credit" value="{{ isset($expense->credit) ? $expense->credit: old('credit')}}">
i need to make sure the values are equal when submit the form.
i know I can do that if name them like this:
debit and debit_confirmation and in rule array
return Validator::make($data, [
'debit' => 'required|confirmed',
]);
but I don't want to change their names.
any build-in validation in laravel 5 do that.
You could use the same validation rule to match the fields you want.
return Validator::make($data, [
'debit' => 'required|same:credit',
]);
You could take a look at the laravel documentation for more information about validation rules
Among many validators that Laravel offers there is one you're looking for: same. In order to validate if values of 2 different fields (field1, field2) match, you need to define the following rules
$rules = [
'field1' => 'same:field2'
];
You can see a list of all available validation rules here: http://laravel.com/docs/5.1/validation#available-validation-rules

How to make Laravel 'confirmed' validator to add errors to the confirmation field?

By default, Laravel 'confirmed' validator adds the error message to the original field and not to the field which usually contains the confirmed value.
'password' => 'required|confirmed|min:8',
Is there any simple way to extend the validator or use some trick to force it to always show the error on the confirmation field instead of the original field?
If I fail to enter my password twice, the error seems more appropriate to belong the confirmation field and not to the original password field. Or maybe that's just our UX analyst getting nitpicky...
One way to go about it is to use same rule instead of confirmed
// ...
$input = Input::all();
$rules = [
'password' => 'required|min:8',
'password_confirmation' => 'required|min:8|same:password',
];
$messages = [
'password_confirmation.same' => 'Password Confirmation should match the Password',
];
$validator = Validator::make($input, $rules, $messages);
if ($validator->fails()) {
return back()->withInput()->withErrors($validator->messages());
}
// ...
You should design your form as below;
<input type="password" name="password">
<input type="password" name="password_confirmation">
Quote from Laravel: confirmed
"The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input"
Now, you can design your validation as follow;
$request->validate([
"password" => 'required|confirmed'
]);
$rules=[
'username'=>'required|max:20',
'password1'=>'required|min:8',
'password2'=>'required|min:8|same:password1',
];
$error_messages=[
'password2.same'=>'password are not the same password must match same value',
'password1.min'=>'password length must be greater than 8 characters',
'password2.min'=>'confirm-password length must be greater than 8 characters',
];
$validator= validator($request->all(), $rules, $error_messages);
if ($validator->fails()) {
return redirect('control_pannel/change_password')
->withErrors($validator)
->withInput();
}
One solution that quickly comes to mind is to just display the password errors on the password_confirmation field.
If that won't work for you, just label the password_confirmation field as password and the password field as password confirmation so that if there are errors, it will show up near the password_confirmation label rather than the password label.
Otherwise, it's not difficult to add a your own custom validation method.

Categories