Php 覆盖所需的单个消息,而不覆盖所有laravel
我在laravel中至少有一个字段使用了required_,但没有使用全部。 这是我的规则代码Php 覆盖所需的单个消息,而不覆盖所有laravel,php,laravel,Php,Laravel,我在laravel中至少有一个字段使用了required_,但没有使用全部。 这是我的规则代码 '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
'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',
这是我的自定义消息,它将覆盖由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.',
而不是向用户显示多行错误消息。有没有办法把这四条信息总结成一条信息,比如
接下来的一个季度,租赁公司,。。。必须在场
提前感谢。如果您正在使用表单请求,您可以在
messages()
方法中执行此操作:
return [
'required_without_all' => 'One of the field must be present.'
];
如果您使用的是手动验证器,则可以执行以下操作:
$messages = [
'required_without_all' => 'One of the field must be present.',
];
$validator = Validator::make($input, $rules, $messages);
编辑:另一种方法是检查您的MessageBag是否包含其中一个字段的错误,然后在视图中相应地显示另一个字段。例如:
@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
}
诚然,这并不漂亮,但我似乎找不到另一种方法 通过添加自定义规则来解决
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.';
}
在表单请求中,我有一个覆盖getValidatorInstance()
方法,并在验证后调用,以手动添加规则,并在带有自定义名称的错误包上触发
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;
}
有一个非常简单的方法可以实现这一点。将规则应用于一个组合的
ids
字段,然后包括在required\u之后要对其应用验证的所有实际字段,而不包括\u all
。只有一条对应的验证消息
$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.'
]);
所以。。。我不一定推荐这样做,但当在多个字段上使用
required\u而不使用\u all
时,有一种快速而肮脏的方法可以防止重复的错误消息,目的是“至少需要一个字段”类型的规则:
:属性
):公共函数foo(请求$Request)
{
$request->validate(
[
//“其中至少一个”字段必须在此处分组。
'field_1'=>'required_without_all:field_2 | nullable | etc',
'field_2'=>'required_without_all:field_1 | nullable | etc',
//等等。。。
],
[
'字段\u 1.必填\u不带\u all'=>'至少需要一个',
'字段\u 2.必填\u不带\u all'=>'至少需要一个',
//等等。。。
//如果此字段列表是您唯一一次使用
//必选项如果没有全部,您可以只使用一个“全部捕获”
//而是['required\'u without\'u all'=>'message']
]
);
}
@if($errors->any())
@foreach($errors->all()作为$error)
@如果($error!==($prevererror???))
- {{$prevError=$error}
@恩迪夫
@endforeach
@恩迪夫
PHP的一个怪癖(?)是它在变量赋值时返回赋值,这就是为什么我们的
项仍然包含“至少一个必需”错误文本的原因。空合并?
只是为了在分配$preveror
之前,foreach
不会在第一个循环中中断
再次。。。我不确定我是否能恰当地推荐这样做——有几个原因说明这是次优的。但是,如果您需要一种快速而肮脏的方法来实现“至少其中一个”类型规则,您可能会发现它很有用。我使用的是表单请求,但我必须在同一表单中的其他字段和其他案例中使用required_而不使用_。对不起,我误导了我的信息。我已经编辑了我的问题。在这种情况下,我不确定从验证器的角度是否可以实现。您可以推荐我以其他方式编辑我的答案以添加可能的替代方案。不过,我不太喜欢它。