问题
I have been using required_without_all for at least one field required in laravel. Here is my code for rule
'rental_company_id' => 'required_without_all:camper_id,take_over_station_id,returnable_station_id',
'camper_id' => 'required_without_all:rental_company_id,take_over_station_id,returnable_station_id',
'take_over_station_id' => 'required_without_all:rental_company_id,camper_id,returnable_station_id',
'returnable_station_id' => 'required_without_all:rental_company_id,camper_id,take_over_station_id',
Here are my custom message which will override default message produced by laravel
'rental_company_id.required_without_all' => 'The Rental Companies is required when none of Campers, Takeover stations and Returnable stations are present.',
'camper_id.required_without_all' => 'The Campers is required when none of Rental Companies, Takeover stations and Returnable stations are present.',
'take_over_station_id.required_without_all' => 'The Takeover stations is required when none of Campers, Rental Companies and Returnable stations are present.',
'returnable_station_id.required_without_all' => 'The Returnable stations is required when none of Campers, Rental Companies and Takeover stations are present.',
Instead of showing multiple line of error message to the user. is there any way I can sum up those four messages into single message like
One of the following Season,Rental companies,... must be present.
Thanks in advance.
回答1:
If you are using a form request, you could do this in your messages()
method:
return [
'required_without_all' => 'One of the field must be present.'
];
If you are using a manual validator, you could do this:
$messages = [
'required_without_all' => 'One of the field must be present.',
];
$validator = Validator::make($input, $rules, $messages);
Edit: An alternative would be to check if your MessageBag contains an error for one of those fields, and then display another one accordingly in your view. For example:
@if (
$errors->has('rental_company_id')
|| $errors->has('camper_id')
|| $errors->has('take_over_station_id')
|| $errors->has('returnable_station_id')
) {
// display the error message you need
}
Admittedly, this is not pretty, but I can't seem to find another way of doing it.
回答2:
Solve by adding custom rule php artisan make:rule AssignDataValidation
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$isSet = false;
$input = Input::all();
foreach (Discount::ASSIGNED_DATA_FIELD as $data){
if (isset($input[$data])){
$isSet = true;
break;
}
}
return $isSet;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'One of the following Season, Rental companies,... must be present.';
}
On Form request I have an override getValidatorInstance()
method and invoke after validation to add my rule manually and trigger on error bag with the custom name
protected function getValidatorInstance()
{
$validator = parent::getValidatorInstance();
$validator -> after(function ($validator){
$timeDataValidation = new TimeDataValidation;
$assignDataValidation = new AssignDataValidation;
if (!$timeDataValidation -> passes(TimeDataValidation::FIELD,Discount::TIME_DATA_FIELD)){
$validator -> errors() -> add(TimeDataValidation::FIELD,$timeDataValidation -> message());
}
if (!$assignDataValidation -> passes(AssignDataValidation::FIELD,Discount::ASSIGNED_DATA_FIELD)){
$validator -> errors() -> add(AssignDataValidation::FIELD,$assignDataValidation -> message());
}
});
return $validator;
}
回答3:
There is a very simple way you can achieve this. Apply the rule to a made-up ids
field and then include all the actual fields for which you want validation applied after required_without_all
. And just one corresponding validation message.
$validator = Validator::make($request->all(), [
'ids' => 'required_without_all:rental_company_id,camper_id,take_over_station_id, returnable_station_id'
], [
'required_without_all' => 'One of the following Season,Rental companies,... must be present.'
]);
来源:https://stackoverflow.com/questions/54567441/override-single-message-for-required-without-all-laravel