Use `trans_choice()` in request validation messages with validation rule parameter - laravel

I need a request validation rule to return a custom message upon failure, and since the field being validating is an array with a min:x rule i'd like to have a custom message for both singular and plural variations.
I'm just wondering how to pass to the trans_choice() function the :min parameter from the validation rule:
Translation file:
'array' => [
'field' => [
'min' => 'You need to select at least one item.|you need to select at least :min items',
],
],
Request message() method:
public function messages() {
'my.array.field.min' => trans_choice('translations::array.field.min', ???),
}

It seems like there's nothing built into Laravel to use trans_choice whenever you can pluralise a translation string.
A way you could solve that is by temporarily (or permanently, whatever fits your use-case) changing the replacer for the min rule to something like this:
Validator::replacer('min', function ($message, $attribute, $rule, $parameters, $validator) {
$minValue = $parameters[0];
$message = Str::contains($message, '|')
? trans_choice($message, $minValue)
: $message;
return str_replace(':min', $minValue, $message);
});
Or by extending the Validator factory to work in your favour, but since that requires you to change many of it's methods I don't really recommend that.

Related

Laravel - custom validation

I have this validation:
$data = request()->validate([
'qty100' => ['integer'],
'qty250' => ['integer'],
'qty500' => ['integer'],
]);
I would need to check if at least one of them is bigger than 0... how can this be done?
I think there is no built-in validation rule does something like what you want in Laravel, so you'll need to implement a custom validator, that will let you reuse validation where needed.
this is one way of doing it.
request()->validate([
'intone' => ['required', 'integer', 'greaterThanZeroWithoutAll:inttwo,intthree'],
'inttwo' => ['required', 'integer'],
'intthree' => ['required', 'integer'],
]);
in your AppServiceProvider
public function boot()
{
//here we are creating a custom rule. called 'greaterThanZeroWithoutAll'
//$attribute is the name we are validating,
//$value is the value we get from the request,
//$parameters are the arguments we pass in like greaterThanZeroWithoutAll:inttwo,intthree inttwo and intthree are parameters
//$validator is the validator object.
Validator::extend('greaterThanZeroWithoutAll', function ($attribute, $value, $parameters, $validator) {
//$validator->getData() is all the key value pairs from greaterThanZeroWithoutAll rule.
//array_values($validator->getData()) we are only interested in the values, so this will return all the values.
//implode(array_values($validator->getData())) will turn it into string
//!(int) implode(array_values($validator->getData())) this uses no glue when imploding, then explicitly casts the generated string as an integer, then uses negation to evaluate 0 as true and non-zero as false. (Ordinarily, 0 evaluates as false and all other values evaluate to true.)
if (!(int) implode(array_values($validator->getData()))) {
//means all values are 0
return false;
}
return true;
});
// this is error message
Validator::replacer('greaterThanZeroWithoutAll', function ($message, $attribute, $rule, $parameters) {
return 'not all fields are greater 0';
});
}
!(int) implode(array_values($validator->getData())) this code basically checks all the values are zero, there should many other ways to do this.
The reason we only do on the first value is that, we pass the other two values in and compare with it. So, it does it.

Laravel custom validation rule. How to add possibility for passing rule with a string representation instead of using class name?

What do I mean:
As it's shown at documentation:
...
'field' => [
'required',
'numeric',
new MyCustomRule
],
...
But what if I want to pass it in one string and with some arguments||options signature (as it's implemented with default "exists" rule where i can optionally pass connection, table, field, column and so on)?
...
'field' => 'required|numeric|my_rule:param1.param2,option1,option2',
...
Where should i define signature? Thanks!
Extend your validator inside App\Providers\AppServiceProvider
E.g:
Validator::extend('rule', function (string $attribute, string $value, array $parameters) {
dump($attribute, $value, $parameters);
return $attribute == $value;
});
See a working example here.

Array number/position in custom Laravel validation messages

When validating arrays in Laravel and using custom error messages, is there any way to access the array number/position that is throwing the validation failure?
Trying to manipulate :attribute or :key in the messages array of the Request doesn't work as the placeholders are later translated (read: they aren't the actual variables)
I am trying to present a message like:
object.property.*.required => 'The property on object # is required'
Otherwise you end up with something like:
object.property.3 is required
I'd like to grab the number so I can present a friendlier and more descriptive message.
Well, this can be achieved by the replacer method on the Validator facade. Add the replacer in AppServiceProvider#boot method.
//...
public function boot()
{
Validator::replacer('required', function ($message, $attribute, $rule, $parameters) {
if (str_contains($message, ':nth') && preg_match("/\.(\d+)\./", $attribute, $match)) {
return str_replace(":nth", $match[1]+ 1, $message);
}
return $message;
});
}
//...
The custom validation message for the attribute must contain the palceholder :nth
object.property.*.required => 'The property on object :nth is required'

Laravel 5.3 set second attribute name in custom validation rule

I have the following custom validation rule...
Validator::extend('empty_with', function ($attribute, $value, $parameters, $validator) {
$other = array_get($validator->getData(), $parameters[0], null);
return ($value != '' && $other != '') ? false : true;
}, "The :attribute field is not required with the :other field.");
And am using it like...
$validator = Validator::make($request->all(), [
'officer' => 'sometimes|integer',
'station' => 'empty_with:officer,|integer',
]);
The current error message am getting is
The station field is not required with the:otherfield.
Versus what I would like to have;
The station field is not required with the officer field.
How do I set a the second parameter 'officer' in the error message, the same way :attribute is...??
You'll need to add in a custom replacer to go with your custom validation rule. See 'Defining the error message' here.
\Validator::replacer('empty_with', function ($message, $attribute, $rule, $parameters) {
return str_replace(':other', $parameters[0], $message);
});
This code tells Laravel that when the empty_with rule fails, the message should be run through that closure before being passed back to the user. The closure performs a simple string replacement and returns the amended error message.
For the most part, each validation rule has its own replacement rules for messages since it's dependent on the specific attributes and their order. Although :other being replaced with the first parameter happens for a few rules, it's not automatic and is defined explicitly for each rule that uses it. It's worth looking in the Illuminate\Validation\Concerns\ReplacesAttributes trait to get an idea of how Laravel deals with replacement for its built-in rules.

Laravel Validation sometimes rules for date validation

I currently have a validation rule which looks like this:
public function rules()
{
return [
'startDate' => 'required|sometimes|before_or_equal:endDate',
'endDate' => 'sometimes|required|after_or_equal:startDate',
];
}
The sometimes option works as I understand it on the basis that if the field is present, run the validation rule. However, if the end date is not sent or is null, my before or equal rule kicks in and fails. In some instances within my application, end date will be null. Is there a way to 'cancel' the startDate validation rule in this instance or would I need to create a custom validator for this purpose?
something like before_or_equal_when_present ?
You can use IFs to add and manipulate rules in the rules function. You can access the inputs there referring to $this as the request itself:
public function rules()
{
$rules = [
'startDate' => 'required|sometimes|before_or_equal:endDate',
'endDate' => 'sometimes|required|after_or_equal:startDate',
];
if( $this->input('endDate') > 0)
$rules['endDate'] = "rule". $rules['endDate']
return $rules;
}
This is just a mockup just to let you know that you can manipulate and have access to the fields passed.

Resources