make text field required only if checkbox is unchecked - laravel-5

Basically I want textfield to be required only if checkbox is unchecked
I have read documentation from https://laravel.com/docs/5.2/validation but no option is working till now.
Will requiredif is help in this case? if yes, how I will pass if checkbox is checked or unchecked?
I have tried with requiredif and requiredunless but now getting
is this possible through laravel default validation? any suggestions?
Following is my code
$this->validate($request, [
'exp_company' => 'required',
'exp_designation' => 'required',
'exp_location' => 'required',
'start_date' => 'required',
'end_date' => 'requiredif:current_company,0'
]);
here current_company is checkbox field name.

Try with required_without
'end_date' => 'required_without:current_company'

Related

Adding validation if another validation passed in laravel FormRequest

Is it possible to add validation if another validation passed for example I have the following validation rules
return [
'name' => ['required', 'string', 'min:3', 'max:30'],
'password' => ['required', 'min:6'],
'photo' => ['imageable'],
'business_uuid' => ['required', 'uuid', 'exists:businesses,uuid'],
];
and I would like if the business_uuid validation passed to add validation for the phone number
'phone' => ['nullable', 'phone:'.$countryCode],
notice that I'm the country code will be brought from the business That's why I'm I'd like to add it if the validation passed

Laravel 5.4 field Validation already exists

I have a field called title in table languages.
While creating a new user using register form, user has a option to either chose existing or create a new language. How do I make sure the language created is unique?
users table is different and languages table is different.
my current validation code in create method (Registercontroller)
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'email' => ['required','max:255','unique:users','regex:/(#org.uk)$/',],
'password' => 'required|min:6|confirmed',
'title' => 'required',
I want to add something like: unqiue:languages but how do I do that? do I set langauge field to unique?
laravel validation for unique
here is the solution what you needed . In unique validation it check new entry was unique or not what you do is this just simply given the desired table name and field from which you want to check field is unique or not
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'email' => ['required','max:255','unique:users','regex:/(#org.uk)$/',],
'password' => 'required|min:6|confirmed',
'title' => 'required|unique:languages,title',
hope this will help you

Laravel 5.3 multiple ignore rules for one non-key field

I use soft deletes which I then ignore/exlude in the validation rules in Laravel 5.3.
How do I enforce two ignores in a validation array for updates?
'number' => "required|unique:customers,number,NULL,id,company_id,
$company_id},deleted_at,NULL",
I need to add the exclusion of the current record as well.
I found the answer myself. For your information(field = number) :
$validator = Validator::make($request->all(),
[
'number' => "required|unique:customers,number,{$id},id,company_id,{$company_id},deleted_at,NULL",
'name' => 'required',
'street' => 'required',
'streetnumber' => 'required',
'zipcode' => 'required',
'city' => 'required',
]);

Specify Laravel validation language

is it at all possible to tell the Laravel validator which language to use for validation?
I have an application which has its text in English, but for some of the forms, I need the validation errors for the fields to be returned in a different language.
I researched a little and found out that I can just use \App::setLocale('ro') to set the language of the app and thus the file under resources/lang/ro/validation.php will be used for the validation, but I do not want to temper with the setLocale. I could, in the worst scenario, temper with it and change the language before the validation and change it back after the validation again, but it doesn't seem like a good solution.
I am looking for something more like this:
$this->validate($request, [
'title' => 'required',
'short' => 'required',
], 'lang_that_I_set_in_DB');
If a custom validator is an option for you, you can set the locale of the validators translator:
$validator = Validator::make($request->all(), [
'title' => 'required',
'short' => 'required',
]);
$validator->getTranslator()->setLocale('ro');
$this->validateWith($validator);
I did figure this out, thanks for giving me pointers!
This is how it looks in Laravel 5.1:
$validator = \Validator::make($request->all(), [
'title' => 'required',
'short' => 'required',
]);
$validator->getTranslator()->setLocale('ro');
if ($validator->fails()) {
return redirect()
->back()
->withErrors($validator)
->withInput();
}

Laravel Form validation issue when form edit?

I used Laravel Form validation for validate form:
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users',
'email' => 'required|email|max:50|unique:users',
'mobile' => 'required'
);
this is working well on some thing add. but when i going to edit unique:users part given error "already been taken", so how to write validation to check exclude edit row.
If I wrote this way, does it work?
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users,'.$id,
'email' => 'required|email|max:50|unique:users,'.$id,
'mobile' => 'required'
);
#user3099298 Your second set of code looks alright.
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users,user_name,' . $user_id, // $user_id = Currently being edited user id.
'email' => 'unique:users,email_address,' . $user_id, // $user_id = Currently being edited user id.
'mobile' => 'required',
);
I have added column names in rules - may be you can try that one.
Please check here the documentation
Where they explain how to ignore some rules by providing ID.

Resources