Laravel 5.4 field Validation already exists - laravel-5

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

Related

How to unique validation from multiple tables

I want to unique validation a input field from multiple table. But i don't know how to do this. I already try some way but nothing work. Here is my code.
$validate = Validator::make($request->all(), [
'gender' => 'required|max:5',
'email' => 'required|unique:customers,email|unique:subscribers,email|max:128',
]);
You can use the unique rule twice, like so:
$validate = Validator::make($request->all(), [
'gender' => 'required|max:5',
'email' => 'required|unique:customers|unique:subscribers|max:128',
]);
You also don't have to provide the column name, if you leave it unspecified laravel will assume the field name, which is also email in this case.

How to validate data (coming in request) from another (buyers) table using laravel 8

I have 2 different tables based on their types: User & Buyer.
I am validating "buyers" data but it checks in the "users" table rather than in the "buyers" table, Is there any way to ask Validator Facade to check in the "buyers" table.
$validator = Validator::make($request->all(), [
'name' => 'required',
'user_type' => 'required',
'phone' => 'required|unique:users|max:11|min:11',
'email' => 'required|email|unique:users',
'password' => 'required|min:8',
'passowrd_confirmation' => 'required|same:password',
]);
https://laravel.com/docs/8.x/validation#specifying-a-custom-column-name
According to the documentation, you can indicate a specific field of any of your tables for validation. In this way you can do:
'name' => 'required:buyers, name'

Required_if laravel request Id 's name must be equal to value-Validation

In my Project I want to perform some validations
public function rules()
{
return [
'first_name' => 'required|string|max:255',
'last_name' => 'string|max:255',
'email' => 'email|max:255|required_without:phone|unique:users',
'phone' => 'string|max:255|required_without:email|unique:users',
'password' => 'required|confirmed|min:8',
'school_id' => 'required|exists:schools,id',
'city' => 'required|string',
'class_id' => 'required|exists:klasses,id',
'school_name'=>'required_if://here i need validation
];
}
Here the school_id is holding the id of the school.. so in my case, If the user passed the id of a school whose having name Others I want to set the school_name as mandatory in the request how do I can achieve this?..
To make school_name mandatory when school_id is empty, you can simply use the required_if rule with the input.
Rule::requiredIf(function () {
return empty($this->input('school_id'));
}
It will check if there's input for the school_id, and if there's not, then the school_name is required.

Using Unique in Laravel

I am building a Book Portal with Laravel. I have a unique validation rule on name to books.
$request->validate([
'name' => 'required|unique:books',
'about' => 'required',
'dsecription' => 'required',
'image' => 'required|mimes:jpeg,bmp,jpg,png|between:1, 6000',
'author_id' => 'required',
'publisher' => 'required',
'recommended' => 'required',
'epub_url' => 'required',
'year'=> 'required',
'pages' => 'required',
]);
I found out that an Author can have the same book name. Using unique on the name, will not allow me to upload.
I am thinking, if there is a way, I can check the author and the name of the book i.e
Check if the author has the same name, then apply the unique rule on name else upload.
I didn't find any solution for this. That's why I made my own custom validation like this :
$request->validate([
'name' => 'required|unique:books',
'author_id' => 'required',
// ...
]);
$data = Book::where('author_id', $request->author_id)->where('name', $request->name)->first();
if(!empty($data)) {
return redirect()->back()->withErrors('This name already taken'); // error message
}
You can try using the extra where conditions that can be passed to the unique rule to apply the unique constraint under certain conditions:
// we will make sure 'author_id' is an existing value
'author_id' => 'bail|required|integer|exists:authors,id',
'name' => [
'bail',
'required',
Rule::unique('books')
->where('author_id', (int) $request->input('author_id')),
],
Assuming author_id is on the books table.
Laravel 7.x Docs - Validation - Rules - unique

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