Laravel - List of all default validation error messages - php

I'm trying to use the Laravel Validator class for a PHP project that is not made in laravel. Essentially I'm doing something similar to Illuminate Validator in stand-alone non-Laravel application
Here's my code:
$filesystem = new \Illuminate\Filesystem\Filesystem();
$fileLoader = new \Illuminate\Translation\FileLoader($filesystem, '');
$translator = new \Illuminate\Translation\Translator($fileLoader, 'en_US');
$messages = [
'required' => 'The :attribute field is required.',
'string' => 'The :attribute field must be a string.',
];
$factory = new \Illuminate\Validation\Factory($translator);
$rules = [
'title' => 'required'
];
$validator = $factory->make($this->request->post(), $rules, $messages);
$validator = new \Illuminate\Validation\Validator($translator, $this->request->post(), $rules);
if ($validator->fails()) {
$errors = $validator->errors();
foreach ($errors->all() as $message) {
var_dump($message);
}
}die;
My problem here is that I need to create all the error validation messages myself. I was wondering if there is a way use the default Laravel error messages. I'm even considering setting all those default error messages as one complete array in a seperate file, if I could get those. Any ideas where I can find all the default validation error messages for Laravel?

This is the list of all default error messages
https://github.com/laravel/laravel/blob/master/resources/lang/en/validation.php
[
'accepted' => 'The :attribute must be accepted.',
'active_url' => 'The :attribute is not a valid URL.',
'after' => 'The :attribute must be a date after :date.',
'after_or_equal' => 'The :attribute must be a date after or equal to :date.',
'alpha' => 'The :attribute may only contain letters.',
'alpha_dash' => 'The :attribute may only contain letters, numbers, dashes and underscores.',
'alpha_num' => 'The :attribute may only contain letters and numbers.',
'array' => 'The :attribute must be an array.',
'before' => 'The :attribute must be a date before :date.',
'before_or_equal' => 'The :attribute must be a date before or equal to :date.',
'between' => [
'numeric' => 'The :attribute must be between :min and :max.',
'file' => 'The :attribute must be between :min and :max kilobytes.',
'string' => 'The :attribute must be between :min and :max characters.',
'array' => 'The :attribute must have between :min and :max items.',
],
'boolean' => 'The :attribute field must be true or false.',
'confirmed' => 'The :attribute confirmation does not match.',
'date' => 'The :attribute is not a valid date.',
'date_format' => 'The :attribute does not match the format :format.',
'different' => 'The :attribute and :other must be different.',
'digits' => 'The :attribute must be :digits digits.',
'digits_between' => 'The :attribute must be between :min and :max digits.',
'dimensions' => 'The :attribute has invalid image dimensions.',
'distinct' => 'The :attribute field has a duplicate value.',
'email' => 'The :attribute must be a valid email address.',
'exists' => 'The selected :attribute is invalid.',
'file' => 'The :attribute must be a file.',
'filled' => 'The :attribute field must have a value.',
'gt' => [
'numeric' => 'The :attribute must be greater than :value.',
'file' => 'The :attribute must be greater than :value kilobytes.',
'string' => 'The :attribute must be greater than :value characters.',
'array' => 'The :attribute must have more than :value items.',
],
'gte' => [
'numeric' => 'The :attribute must be greater than or equal :value.',
'file' => 'The :attribute must be greater than or equal :value kilobytes.',
'string' => 'The :attribute must be greater than or equal :value characters.',
'array' => 'The :attribute must have :value items or more.',
],
'image' => 'The :attribute must be an image.',
'in' => 'The selected :attribute is invalid.',
'in_array' => 'The :attribute field does not exist in :other.',
'integer' => 'The :attribute must be an integer.',
'ip' => 'The :attribute must be a valid IP address.',
'ipv4' => 'The :attribute must be a valid IPv4 address.',
'ipv6' => 'The :attribute must be a valid IPv6 address.',
'json' => 'The :attribute must be a valid JSON string.',
'lt' => [
'numeric' => 'The :attribute must be less than :value.',
'file' => 'The :attribute must be less than :value kilobytes.',
'string' => 'The :attribute must be less than :value characters.',
'array' => 'The :attribute must have less than :value items.',
],
'lte' => [
'numeric' => 'The :attribute must be less than or equal :value.',
'file' => 'The :attribute must be less than or equal :value kilobytes.',
'string' => 'The :attribute must be less than or equal :value characters.',
'array' => 'The :attribute must not have more than :value items.',
],
'max' => [
'numeric' => 'The :attribute may not be greater than :max.',
'file' => 'The :attribute may not be greater than :max kilobytes.',
'string' => 'The :attribute may not be greater than :max characters.',
'array' => 'The :attribute may not have more than :max items.',
],
'mimes' => 'The :attribute must be a file of type: :values.',
'mimetypes' => 'The :attribute must be a file of type: :values.',
'min' => [
'numeric' => 'The :attribute must be at least :min.',
'file' => 'The :attribute must be at least :min kilobytes.',
'string' => 'The :attribute must be at least :min characters.',
'array' => 'The :attribute must have at least :min items.',
],
'not_in' => 'The selected :attribute is invalid.',
'not_regex' => 'The :attribute format is invalid.',
'numeric' => 'The :attribute must be a number.',
'present' => 'The :attribute field must be present.',
'regex' => 'The :attribute format is invalid.',
'required' => 'The :attribute field is required.',
'required_if' => 'The :attribute field is required when :other is :value.',
'required_unless' => 'The :attribute field is required unless :other is in :values.',
'required_with' => 'The :attribute field is required when :values is present.',
'required_with_all' => 'The :attribute field is required when :values is present.',
'required_without' => 'The :attribute field is required when :values is not present.',
'required_without_all' => 'The :attribute field is required when none of :values are present.',
'same' => 'The :attribute and :other must match.',
'size' => [
'numeric' => 'The :attribute must be :size.',
'file' => 'The :attribute must be :size kilobytes.',
'string' => 'The :attribute must be :size characters.',
'array' => 'The :attribute must contain :size items.',
],
'string' => 'The :attribute must be a string.',
'timezone' => 'The :attribute must be a valid zone.',
'unique' => 'The :attribute has already been taken.',
'uploaded' => 'The :attribute failed to upload.',
'url' => 'The :attribute format is invalid.'
]

Related

"These credentials do not match our records."

I am getting this error while I try to login. user registers successfully, but when trying to login, I get the error "These credentials do not match our records."
This is my login function in UserController:
function login(Request $req)
{
$validatedData = $req->validate([
'name' => 'required|max:255',
'email' => 'required|email',
'password' => 'required',
], [
'name.required' => 'The name field is required.',
'email.required' => 'The email field is required.',
'email.email' => 'The email must be a valid email
address.',
'password.required' => 'The password field is
required.',
'password.min' => 'The password must be at least 8
characters.',
]);
$credentials = $req->only('email','password');
if (Auth::attempt($credentials)) {
return redirect()->intended('/dashboard')
->with('success', "Loggedin successfuly");
}
return redirect()->back()->withErrors(['email' =>
'These credentials do not match our records.']);
}

Is there a way to validate phone number globally inLaravel?

I have validated UAE-based phone numbers in Laravel. Is there any way to validate all countries numbers (not only UAE-based)?
$messages = [
'required' => 'The :attribute field is required.',
'integer' => 'The :attribute field value should be an integer.',
'regex' => 'The :attribute is invalid',
'digits' => 'The :attribute is invalid',
];
$validator = Validator::make($request->all(),
[
'number' => 'required|digits:12|regex:/(9715)[0-9]{8}/',
'service' => 'required|integer'
],
$messages
);

Validation in Laravel 5.6

This question already asked but that is not solving my issue.
I have try to use validation in my laravel project. But it not working this is my code
$rules = array(
'coupon_title' => 'max:10',
'coupon_type_id' => 'numaric',
'expiry_start_date' => 'required|before_or_equal:expiry_end_date',
'expiry_end_date' => 'required',
);
$messages = [
'before_or_equal' => 'The :attribute must be smaller than the End Date',
'before' => 'The :attribute must be Greater than the Interview Start Date',
'after' => 'The :attribute must be Greater than the Interview End Date',
'after_or_equal' => 'The :attribute must be Greater than the Start Time',
//'max' => 'asdasd'
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails())
{
$messages = $validator->messages();
return response()->json(['message'=>$messages],401);
}
In my code only expiry_start_date field validated. But coupon_title and coupon_type_id fields not validated.
You need to correct spell numaric to numeric and add nullable keyword in validation rule
'coupon_title' => 'string|max:10|nullable',
'coupon_type_id' => 'numeric',
Also you can add custom message for numeric and max
$messages = [
'before_or_equal' => 'The :attribute must be smaller than the End Date',
'before' => 'The :attribute must be Greater than the Interview Start Date',
'after' => 'The :attribute must be Greater than the Interview End Date',
'after_or_equal' => 'The :attribute must be Greater than the Start Time',
'max' => 'The :attribute has crossed the limit',
'numeric' => 'The :attribute must have numeric value'
];

Translate validation message in laravel 5

I have a problem translating the laravel messages. I have created a folder called es inside lang folder and I have translated the messages from English to Spanish. Also I have set 'locale' => 'es' on config/app.php. But English messages continues appearing.
I have used the default views for login provided by laravel, to generate it (and routes) I used the command (as says in https://laravel.com/docs/5.2/authentication)
php artisan make:auth
I have changed the default routes by the following:
Route::get('/login', 'UserController#getLogin');
Route::post('/login', 'UserController#postLogin');
And in UserController I have:
public function getLogin(){
return view('auth.login');
}
public function postLogin(Request $request){
$input = $request->all();
if (Auth::attempt(['email' => $input["email"], 'password' => $input["password"]], true))
{
return redirect()->intended('/tables');
}
}
Here are two screenshots of the messages that appears in English
http://i64.tinypic.com/if4k83.png
http://i64.tinypic.com/51zhnp.png
How can I transate these messages?
Thanks
Edit:
resources/lang/en/validation.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
|
| The following language lines contain the default error messages used by
| the validator class. Some of these rules have multiple versions such
| as the size rules. Feel free to tweak each of these messages here.
|
*/
'accepted' => 'The :attribute must be accepted.',
'active_url' => 'The :attribute is not a valid URL.',
'after' => 'The :attribute must be a date after :date.',
'alpha' => 'The :attribute may only contain letters.',
'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.',
'alpha_num' => 'The :attribute may only contain letters and numbers.',
'array' => 'The :attribute must be an array.',
'before' => 'The :attribute must be a date before :date.',
'between' => [
'numeric' => 'The :attribute must be between :min and :max.',
'file' => 'The :attribute must be between :min and :max kilobytes.',
'string' => 'The :attribute must be between :min and :max characters.',
'array' => 'The :attribute must have between :min and :max items.',
],
'boolean' => 'The :attribute field must be true or false.',
'confirmed' => 'The :attribute confirmation does not match.',
'date' => 'The :attribute is not a valid date.',
'date_format' => 'The :attribute does not match the format :format.',
'different' => 'The :attribute and :other must be different.',
'digits' => 'The :attribute must be :digits digits.',
'digits_between' => 'The :attribute must be between :min and :max digits.',
'distinct' => 'The :attribute field has a duplicate value.',
'email' => 'The :attribute must be a valid email address.',
'exists' => 'The selected :attribute is invalid.',
'filled' => 'The :attribute field is required.',
'image' => 'The :attribute must be an image.',
'in' => 'The selected :attribute is invalid.',
'in_array' => 'The :attribute field does not exist in :other.',
'integer' => 'The :attribute must be an integer.',
'ip' => 'The :attribute must be a valid IP address.',
'json' => 'The :attribute must be a valid JSON string.',
'max' => [
'numeric' => 'The :attribute may not be greater than :max.',
'file' => 'The :attribute may not be greater than :max kilobytes.',
'string' => 'The :attribute may not be greater than :max characters.',
'array' => 'The :attribute may not have more than :max items.',
],
'mimes' => 'The :attribute must be a file of type: :values.',
'min' => [
'numeric' => 'The :attribute must be at least :min.',
'file' => 'The :attribute must be at least :min kilobytes.',
'string' => 'The :attribute must be at least :min characters.',
'array' => 'The :attribute must have at least :min items.',
],
'not_in' => 'The selected :attribute is invalid.',
'numeric' => 'The :attribute must be a number.',
'present' => 'The :attribute field must be present.',
'regex' => 'The :attribute format is invalid.',
'required' => 'The :attribute field is required.',
'required_if' => 'The :attribute field is required when :other is :value.',
'required_unless' => 'The :attribute field is required unless :other is in :values.',
'required_with' => 'The :attribute field is required when :values is present.',
'required_with_all' => 'The :attribute field is required when :values is present.',
'required_without' => 'The :attribute field is required when :values is not present.',
'required_without_all' => 'The :attribute field is required when none of :values are present.',
'same' => 'The :attribute and :other must match.',
'size' => [
'numeric' => 'The :attribute must be :size.',
'file' => 'The :attribute must be :size kilobytes.',
'string' => 'The :attribute must be :size characters.',
'array' => 'The :attribute must contain :size items.',
],
'string' => 'The :attribute must be a string.',
'timezone' => 'The :attribute must be a valid zone.',
'unique' => 'The :attribute has already been taken.',
'url' => 'The :attribute format is invalid.',
/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
],
/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap attribute place-holders
| with something more reader friendly such as E-Mail Address instead
| of "email". This simply helps us make messages a little cleaner.
|
*/
'attributes' => [],
];
resources/lang/es/validation.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
|
| The following language lines contain the default error messages used by
| the validator class. Some of these rules have multiple versions such
| as the size rules. Feel free to tweak each of these messages here.
|
*/
'accepted' => ':attribute debe ser aceptado.',
'active_url' => 'El campo :attribute no es una URL válida.',
'after' => 'El campo :attribute debe ser una fecha posterior a :date.',
'alpha' => 'El campo :attribute debe contener solo letras.',
'alpha_dash' => 'El campo :attribute debe contener solo letras números y guiones.',
'alpha_num' => 'El campo :attribute debe contenterdebe contenter letras y números.',
'array' => 'El campo :attribute debe ser un array.',
'before' => 'El campo :attribute debe ser una fecha anterior a :date.',
'between' => [
'numeric' => 'El campo :attribute debe estar entre :min y :max.',
'file' => 'El campo :attribute ebe estar entre :min y :max kilobytes.',
'string' => 'El campo :attribute ebe estar entre :min y :max carácteres.',
'array' => 'El campo :attribute debe tener entre :min y :max elementos.',
],
'boolean' => 'El campo :attribute debe ser verdadero o falso.',
'confirmed' => 'El campo :attribute confirmación no coincide.',
'date' => 'El campo :attribute no es una fecha válida.',
'date_format' => 'El campo :attribute does not match the format :format.',
'different' => 'El campo :attribute y :other deben ser diferentes.',
'digits' => 'El campo :attribute debe ser de :digits dígitos.',
'digits_between' => 'El campo :attribute debe estar entre :min y :max dígitos.',
'distinct' => 'El campo :attribute tiene un valor duplicado.',
'email' => 'El campo :attribute debe ser una dirección de correo válida.',
'exists' => 'El campo :attribute no es válido.',
'filled' => 'El campo :attribute es obligatorio.',
'image' => 'El campo :attribute debe ser una imagen.',
'in' => 'El campo :attribute no es válido',
'in_array' => 'El campo :attribute no existe en :other.',
'integer' => 'El campo :attribute debe ser un entero.',
'ip' => 'El campo :attribute debe ser una dirección IP válida.',
'json' => 'El campo :attribute debe ser una cadena JSON válida.',
'max' => [
'numeric' => 'El campo :attribute no debe ser mayor que :max.',
'file' => 'El campo :attribute no debe ser mayor que :max kilobytes.',
'string' => 'El campo :attribute no debe ser mayor que :max carácteres.',
'array' => 'El campo :attribute no debe contener más de :max elementos.',
],
'mimes' => 'El campo :attribute debe ser un archivo del tipo: :values.',
'min' => [
'numeric' => 'El campo :attribute debe ser de al menos :min.',
'file' => 'El campo :attribute debe ser de al menos :min kilobytes.',
'string' => 'El campo :attribute debe ser de al menos :min carácteres.',
'array' => 'El campo :attribute debe tener al menos :min elementos.',
],
'not_in' => 'El campo selected :attribute no es válido.',
'numeric' => 'El campo :attribute debe ser un número.',
'present' => 'El campo :attribute debe estar presente.',
'regex' => 'El formatp del campo :attribute no es válido.',
'required' => 'El campo :attribute es obligatorio.',
'required_if' => 'El campo :attribute es obligatorio cuando :other es :value.',
'required_unless' => 'El campo :attribute es obligatorio a menos que :other esté en :values.',
'required_with' => 'El campo :attribute es obligatorio cuando :values está presente.',
'required_with_all' => 'El campo :attribute es obligatorio cuando :values está presente.',
'required_without' => 'El campo :attribute es obligatorio cuando :values no está presente.',
'required_without_all' => 'El campo :attribute es obligatorio cuando ninguno de :values está presente.',
'same' => 'Los campos :attribute y :other deben coincidir.',
'size' => [
'numeric' => 'El campo :attribute debe ser de :size.',
'file' => 'El campo :attribute debe ser de :size kilobytes.',
'string' => 'El campo :attribute debe ser de :size carácteres.',
'array' => 'El campo :attribute debe contenter :size elementos.',
],
'string' => 'El campo :attribute debe ser una cadena de texto.',
'timezone' => 'El campo :attribute debe ser una zona válida.',
'unique' => 'El campo :attribute debe ser único.',
'url' => 'El campo :attribute tiene un formato no válido.',
/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
],
/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap attribute place-holders
| with something more reader friendly such as E-Mail Address instead
| of "email". This simply helps us make messages a little cleaner.
|
*/
'attributes' => [],
];
The begining of my config/app.php
return [
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services your application utilizes. Set this in your ".env" file.
|
*/
'env' => env('APP_ENV', 'production'),
/*
|--------------------------------------------------------------------------
| Application Debug Mode
|--------------------------------------------------------------------------
|
| When your application is in debug mode, detailed error messages with
| stack traces will be shown on every error that occurs within your
| application. If disabled, a simple generic error page is shown.
|
*/
'debug' => env('APP_DEBUG', true),
/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/
'url' => env('APP_URL', 'http://localhost'),
/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/
'timezone' => 'UTC',
/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/
'locale' => 'es',
/*
|--------------------------------------------------------------------------
| Application Fallback Locale
|--------------------------------------------------------------------------
|
| The fallback locale determines the locale to use when the current one
| is not available. You may change the value to correspond to any of
| the language folders that are provided through your application.
|
*/
'fallback_locale' => 'es',
Define your language in .env (you can always check what language you are running on with app()->getLocale())
Copy resource/lang/validation.php to resources/lang/{YOUR_LANG}/validation.php
Find and edit this array:
/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
],
Example:
'custom' => [
'mensagem' => [
'required' => 'O campo mensagem é obrigatório.',
],
],

Laravel Validator always shows required error message

While testing, system always returning me required customized message. For example i want the validator to check if the passwords match. But error message shows me "Password Validation is required" instead of "Passwords does not match".
Here is my validation function and rules.
protected function getRegisterValidator()
{
$message = array(
'same' => ':attribute ve :other tekrarları birbirini tutmalı.',
'between' => ':attribute en az :min en fazla :max karakterden oluşabilir.',
'min' => ':attribute en az :min karakter içermelidir.',
'max' => ':attribute maksimum :max karakterden oluşabilir.',
'alpha_num' => 'girilen :attribute alfanumerik olmalıdır.',
'unique' => 'Bu :attribute ile zaten önceden kayıt olunmuş.',
'email' => ':attribute geçerli bir mail adresi değil.',
'captcha' => 'Doğrulama karakterlerini yanlış girdiniz.',
'required' => ':attribute alanı boş olamaz.'
);
return Validator::make(Input::all(),array(
'Kurum' => 'required',
'Ad' => 'required',
'Soyad' => 'required',
'Görev' => 'required',
'Email' => 'required|email|unique:user',
'Email Tekrarı'=> 'required|same:Email',
'Şifre' => 'required|between:8,12',
'Şifre Tekrarı' => 'required|same:Şifre',
'Doğrulama' => 'captcha|required'
), $message);
}
Thanks in advance.
try this
$message = [
'şifre.required' => 'Şifre alanı boş bırakılamaz',
'şifre.same' => 'Şifre alanları eşleşmiyor',
];
"şifre" is input name.
I found the solution. The space between field names like "Şifre Tekrarı" or "Email Tekrarı" leading an error and laravel validator cannot compare the values between "Şifre" and "Şifre Tekrarı" fields.
The solution is setting field names without spaces in your HTML file (or blade), and it does not involve anything with Turkish characters.
protected function getRegisterValidator()
{
$message = array(
'same' => ':attribute ve :other tekrarları birbirini tutmalı.',
'between' => ':attribute en az :min en fazla :max karakterden oluşabilir.',
'min' => ':attribute en az :min karakter içermelidir.',
'max' => ':attribute maksimum :max karakterden oluşabilir.',
'alpha_num' => 'girilen :attribute alfanumerik olmalıdır.',
'unique' => 'Bu :attribute ile zaten önceden kayıt olunmuş.',
'email' => ':attribute geçerli bir mail adresi değil.',
'captcha' => 'Doğrulama karakterlerini yanlış girdiniz.',
'required' => ':attribute alanı boş olamaz.'
);
return Validator::make(Input::all(),array(
'Kurum' => 'required|min:3|max:60',
'Ad' => 'required|min:3|max:30',
'Soyad' => 'required|min:3|max:30',
'Görev' => 'required|min:1|max:60',
'Email' => 'required|email|unique:user|same:Email_Tekrarı',
'Email_Tekrarı'=> 'required',
'Şifre' => 'required|between:8,12|same:Şifre_Tekrarı',
'Şifre_Tekrarı' => 'required',
'Doğrulama' => 'captcha|required'
), $message);
}
Important note :
If you are showing error messages with these field names like "Email_Tekrarı" (i mean there is a _ character included) the error message ignores "_" character and shows error message as like "Email Tekrarı field is required".
Works smoothly.
Thank you for your patience.

Categories