I have checked my credentials with an authenticator and want to check if the username exists in the phone or email column, but I don't know how.
Thanks for any ideas
For example:
$rules = [
'email' => 'required|exists:users,[email, phone]'
];
$messages = [
'email.exists' => '......',
];
Validator::make($request->all(), $rules, $messages)->validate();
Try this instead
$rules = [
'email' => 'required|exists:users,email',
'phone' => 'required|exists:users,phone',
];
$messages = [
'email.exists' => '...',
'phone.exists' => '...',
];
Related
Greeting, this is my code and I need to make custom error messages for every rule
$validator = Validator::make($request->all(), [
'name' => 'required|min:3|max:100',
'phone' => 'required',
'date' => 'required',
'address' => 'required|min:3|max:100',
'test' => 'required|min:3|max:100',
]);
if ($validator->fails()) {
$errors = $validator->errors();
return response()->json($errors);
}
Its better to create a separate request for validation purpose
public function rules(): array
{
return [
'name' => 'required|min:3|max:100',
'phone' => 'required',
'date' => 'required',
'address' => 'required|min:3|max:100',
'test' => 'required|min:3|max:100',
]
}
public function messages(): array
{
return [
'name' => 'Please enter name'
];
}
you can create your own custom validation messages in two ways:
1- in resources/lang/en/validation.php you can change the validation message for every rule
2- you can pass your custom message for each validation like this:
$validator = Validator::make($input, $rules, $messages = [
'required' => 'The :attribute field is required.',
]);
you can check here for more information
specific to your question:
$messages = [
'required' => 'The :attribute field is required.',
'min' => ':attribute must be more than 3 chars, less than 100'
]
$validator = Validator::make($request->all(), [
'name' => 'required|min:3|max:100',
'phone' => 'required',
'date' => 'required',
'address' => 'required|min:3|max:100',
'test' => 'required|min:3|max:100',
], $messages);
Is there any way to customize an error message for this?
request()->validate([
'emp-status' => 'required'
]);
What I want it to return is like
hey this is a custom message
You can try something like this....
// resources/lang/en/validation.php
'custom' => [
'emp-status' => [
'required' => 'hey this is a custom message',
],
]
You can also do it in this way...
$rules = [
'emp-status' => 'required'
];
$customMessages = [
'required' => 'hey :attribute is a custom message'
];
$this->validate($request, $rules, $customMessages);
You can, even more, customize your code.
$customMessages = [
'emp-status.required' => 'hey this is a custom message'
];
Trying to customize the error message for unique attribute. Tried changing my validation.php.
$messages = [
'custom' => [
'email' => [
'unique:users' => 'Oops, email is taken. Please try again!'
]
]
],
and I call the validation in controller:
request()->validate([
'email' => 'unique:users',
'password' => 'required|min:3',
]);
And I still get this:
The email has already been taken.
using custom validator
You can create a custom validator and pass your messages into it
$messages = [
'required' => 'The :attribute field is required.',
];
$validator = Validator::make($input, $rules, $messages);
For you this would mean
$messages = [
'email' => [
'unique' => 'Oops, email is taken. Please try again!'
]
];
$rules = [
'email' => 'unique:users',
'password' => 'required|min:3',
];
$validator = Validator::make(request()->all(), $rules, $messages);
if ($validator->fails()) {
return redirect('route/when/failed')
->withErrors($validator)
->withInput();
}
using language file
In resources/lang/{{language}}/validation.php you can add
'custom' => [
'email' => [
'unique' => 'Oops, email is taken. Please try again!',
],
]
need help to validate my form input
i need user only able to upload file with extension .log
so far i've tried as the code shown below but the validator still reject the input even the file is .log
thanks in advance
$validator = Validator::make($request->all(),
[
'email' => 'required|email',
'file_log' => 'mimes:text/plain,log'
],
[
'email.email' => 'Please input valid email address',
'file_log.mimes' => 'Mohon hanya mengunggah file yang berekstensi .log ',
]
);
Please check this :
'file_log' => 'mimes:text/x-log '
another way
$validator = Validator::make(
[
'file' => $request->file,
'extension' => strtolower($request->file>getClientOriginalExtension()),
],
[
'file' => 'required',
'extension' =>'required|in:log',
]
);
ok finally i find an answer
thanks for your answer
// Create custom validation rule
Validator::extend('logfile', function ($attribute, $value, $parameters, $validator) {
$regexPattern = '/\.log$/';
$filename = $value->getClientOriginalName();
return preg_match($regexPattern, $filename);
});
$validator = Validator::make($request->all(),
[
'email' => 'required|email',
'file_log' => 'logfile',
],
[
'email.email' => 'Please input valid email address',
'file_log.logfile' => 'Please upload only file .log',
]
);
I have a users table which has a unique validate rule on email and username. When i am trying to update not ignore unique validation. Please see my code below.
UserRequest.php
public function rules()
{
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$id,
];
}
Please try this
public function rules()
{
$id = $this->request->get('id') ? ',' . $this->request->get('id') : '';
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$id,
];
}
In laravel 5.5 you should do like this:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
Check laravel documentaion about rule-unique.
You needed to skip id if you validate for update, like as below
public function rules($id='')
{
$id = $id ? ','.$id.',id':'';
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$id,
];
}
In Laravel docs, you have provide 3rd and 4th param in unique rule
unique:table,column,except,idColumn
You could use something like this:
$id = $this->isMethod('put') ? ',' . auth()->id() : '';
assuming you use put method for update
before line with return
for somebody else faced this issue:
public function rules()
{
return [
'name' => 'required',
'mobile' => 'required',
'email' => 'required|unique:users,email'.$this->user->id,
'usercategory' => 'required',
'username' => 'required|unique:users,username'.$this->user->id,
];
}
I fixed this by using this:
public function rules()
{
$id = $this->user->id ?? null;
return [
"name" => "required",
"mobile" => "required",
"email" => "required|unique:users,email, $id",
"usercategory" => "required",
"username" => "required|unique:users,username, $id",
];
}
Note that for other models other than the User model, the user in $this->user->id will be the model name in lowercase