Laravel validation by extension - laravel

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

Related

Laravel make custom error validation messages

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);

Laravel Validator: How to check exists rule in multiple column database

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

How to modify validation message of Laravel?

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

Modify unique attribute validation in Laravel

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!',
],
]

Laravel 5 : Customize HTML Form validation in controller file

I am using following code in controller file. I have an element name title in view form. Following code is showing correct validation. But, i need to customize this error.
$v = Validator::make($request->all(), [
'title' => 'required|max:255',
]);
if ($v->fails())
{
return redirect()->back()->withErrors($v->errors());
}
You can pass as third argument of Validator your custom messages.
$messages = [
'title.required' => 'You need to insert a pretty title'
];
v = Validator::make($request->all(), [
'title' => 'required|max:255',
], $messages
);

Resources