I have made a custom rule in Laravel 5.5, but also want to get a custom translation with it from the lang validation file. For that I have now done:
'custom' => [
'validate' => [
'correct_password' => 'The :attribute is incorrect.',
],
],
And I put this in the custom rule file:
return trans('validate.correct_password');
What have I done wrong to get the custom message? Because I get now only back the key: validate.correct_password as a message.
If you want to pull a key from the translation file, then you need to pass it a key path in the form of file.key.subkey.subkey.
return trans('validation.custom.validate.correct_password');
Related
I need to validate that a customer is using a valid subdomain name as well as making sure some other required fields are present. I have created the following rule in my model class:
public function rules()
{
return array(
array(',c_customerSubdomain, c_custAdminUser,c_adminPassword,c_adminContact', 'required', 'message' => "{attribute} is TEST required."),
array('c_customerSubdomain', 'match', 'pattern' => '/^[a-z0-9_]*$/', 'message' => "Customer Subdomain should contain only alphanumeric and underscore.", "allowEmpty" => false),
);
}
I am testing whether or not the rule is firing by changing the message for required values to: {attribute} is TEST required.
When I submit the form with all of the field blank, the result is as expected.
However, when I populate the field for subdomain with an illegal value, such as "ASD# a", I am expecting to get a validation error. However, instead, the rules bypass all of the other validation errors and attempt to save the model.
I have the following validation code in a controller. The problem is that laravel is sending the column name in the validation error , which is most of the time abbreviations. In this case the column name is "ans". Is there a way to show a different name to the user ?
Validation :
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
]);
Error :
The ans field is required.
If you want to customize the attribute name you can do that; you don't have to customize the message to do this. The validate method takes an array of "custom attribute names" to use (4th argument), just like it takes an array of custom messages (3rd argument).
$this->validate(
$request,
['ans' => 'required|max:5000|min:10'], // rules
[], // custom messages
['ans' => 'Answer'] // custom attribute names
);
You should always have the option to specify custom messages and attributes with the validation methods that are available. These custom attribute names get replaced in the messages where :attribute is used.
If you don't do it this way you would have to create 3 custom messages, one for each rule you have defined to use for your field 'ans', since any of those rules could fail and will include the attribute name.
Yes, you can achieve with the following code.
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
], [
'ans.required' => 'The answer field is required.'
]);
Take a look at the laravel documentation to know more.
You can send custom validation message,define all
your validation message in a variable like this
$messages = [
'ans.required' => 'The answer field is required.'
]
$this->validate($request, [
'ans' => 'required|max:5000|min:10'
], $messages);
Thanks
i have a little problem.
I'm developing a php application using Yii2 framework and i want to save a log messagges into db table.
I'm coding my own component witch extends DbTarget, in this component i rewrite export() function to save data into my table. It's works fine, but i can't get the log message.
For example, when i call Yii:log('message log'), all my data are saved in my db except 'message log' because i don't know how to get this value in my component.
Any solutions?
thanks
P.s. I'm newbee with yii2 and i have read the official documentation, but i didn't find any solution.
It seems you should specify level of the message
Yii:log('message log', Logger::LEVEL_TRACE);
or use shortcut methods
Yii::info('message log');
Yii::trace('message log');
Yii::error('message log');
Check your config for another targets. May be your message goes to level or category of another target. Notice that default category is 'application'.
To be shure you can make this configuration
'components' => [
'log' => [
'targets' => [
[
'class' => 'YourDbTarget',
'levels' => ['info'],
'categories' => ['application'],
],
],
],
],
And try to log info message
Yii::info('message log'); // target = info, category = application
I'm new user that use in Laravel 5.2 framework.
I have problem to modify Validator error messages at the login action. I could change the other part of action for example Register , ResetPasspord ...
but i can't find login action. I found this link when searched at google but it not help me,
I know that Laravel use Route::auth(); in Route file but where is login action that i can change error message language !?
I test this sample code at Requests\Request.php a something this:
public function messages()
{
return [
'required' => 'test error message',
'email.unique' => 'test error message',
];
}
but it wasn't any change !!!
I dont like change action sample login in laravel, and create my custom login, i wanna modify validator custom messages in all app or part of login action
I'm sorry if my language english was not prefect
Thank you
Laravel uses files in resources/lang/en/validation.php (if using english) to determine what to echo. There you'll find your messages and can change them to whatever you want.
One: In config/app.php change
'locale' => 'en' to 'locale' => 'es'
Two: copy all files in the directory resources/lang/en to resources/lang/es
Three: changes the language messages in the file resources/lang/es/validation.php
e.g.
'required' => 'El :attribute es requerido.',
Include this in your Request
Illuminate\Contracts\Validation\Validator
/**
* Get the error messages for the defined validation rules.
*
* #return array
*/
public function messages()
{
return [
'title.required' => 'A title is required',
'body.required' => 'A message is required',
];
}
It will make you to get custom messages.
Is there a simple way to replace the input name in Laravel's validation messages with for example a label?
Right now message is like:
The usrRoleId field is required.
I know there are two options:
Change input names to more readable.
Write EACH message in the validation request to look like this: User Role is required.
I'm looking for something more automatic - like defining a string once and using it every time such field name is used.
Yes you can and it's actually quite easy. You can specify a custom name for a given attribute in your resources/lang/en/validation.php file under the attributes key:
'attributes' => [
'usrRoleId' => 'User Role'
],
And now anywhere you have usrRoleId for the :attribute inside a validator message, it will be replaced with User Role. So instead of:
The usrRoleId field is required.
You'll get the nice:
The User Role field is required.
If you want to have a whole custom message not only a custom attribute name for when the usrRoleId is required, then you can add an entry for that user the custom key in the same file. So adding this:
'custom' => [
'usrRoleId' => [
'required' => 'User Role is required',
],
],
Will use that custom message but without an :attribute placeholder. If you want to use the placeholder you can leave the attributes mapping I described above:
'custom' => [
'usrRoleId' => [
'required' => ':attribute is required',
],
],
And you'll still get your custom message but with the mapped attribute name:
User Role is required.