Chunking Laravel Collections with Chunk and ChunkWhile
When PHP code has loaded items into a Laravel collection, chunk() and chunkWhile() divide them into smaller collections. chunk() sets a maximum…
Laravel checks data submitted by users on the server. When a value fails a validation rule, Laravel returns an error message. To show the message in the user’s language, set the active locale and add translated text to Laravel’s validation language files. A locale is the language setting Laravel uses to find text, such as en for English.
Laravel selects a message using the field and rule that failed, any custom messages you provide, and the translations in the active locale. You can translate built-in rules for general use, write a message for one field and rule, or give a message directly to a validator.
Laravel stores built-in validation messages in a PHP file named validation.php in each locale’s directory. The Laravel 13 application skeleton does not include the lang directory by default. Run php artisan lang:publish to add the framework’s language files, then edit or add the file for your locale, such as lang/es/validation.php. Laravel documents the language file layout and publishing command.
A validation file contains general rule messages and can also contain custom messages for particular fields. For example, this structure changes the general required message and gives email its own message when that field fails the required rule:
'The :attribute field is required.',
'custom' => [
'email' => [
'required' => 'Enter an email address before continuing.',
],
],
];
Put translated wording in the file for the locale you want. A general line such as required applies whenever that rule fails. The custom.email.required line applies only when the required rule fails for the email field. Laravel’s validation documentation describes this custom structure and the built-in rule messages.
Set the default locale in the application configuration, commonly through APP_LOCALE. Laravel also supports changing the locale at runtime with App::setLocale(), which is useful when a request determines the language. Configure a fallback locale separately through APP_FALLBACK_LOCALE; Laravel uses it when the active locale lacks a translation.
In Laravel 13, the validator checks possible messages in a defined order. First, it checks messages passed directly to the validator. These include a key for one field and rule, such as email.required, or a key for a rule across fields, such as required. Next, it checks the language file for a field-and-rule message under validation.custom. The framework’s message-formatting code also supports wildcard message keys.
Rules such as min and max use different messages for strings, numbers, and files. Laravel handles these size rules separately and selects a message that matches the value’s type. When no custom message applies, Laravel uses the rule’s general translation, such as validation.required. If that translation is missing from the active locale, Laravel uses it from the fallback locale when it is available there.
Pass direct messages to Validator::make() or define them in a form request’s messages() method. For example, a form request can provide an email.required message that applies only to that request. Use these options when a message belongs to one validator or request. Use language files to manage translations in one place.
A selected message can contain placeholders such as :attribute. Laravel replaces each placeholder when it creates the final error message. Add an attributes array to the validation language file to show a user-facing name instead of an internal field name such as email. A values array can give values used in some messages clearer wording. These settings change how Laravel fills in a message, but they do not affect which rule message Laravel selects.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com