【发布时间】:2019-03-21 17:58:40
【问题描述】:
我有一个选择列表,其中第一个选项被禁用,这就是当用户没有选择有效选项时,选择列表的结果不会出现在请求中。
在验证中,必填字段,如果其他字段的值为例如:1, in case not 1, the field is not required。
代码:
'city_id' => [
'required',
'integer',
Rule::in(City::availableCities()),
],
'district_id' => new DistrictValidation(request('city_id')),
我怎么能做到这一点,district_id 每次都会抛出验证,不管它是否在请求中。
感谢您的回答,
更新: 也许你看清楚了,如果 DistrictValidation 规则在这里:
class DistrictValidation implements Rule
{
protected $city;
private $messages;
/**
* Create a new rule instance.
*
* @param $cityId
*/
public function __construct($cityId)
{
$this->city = City::find($cityId);
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
dd('here');
if (!$this->city) {
return false;
}
if (!$this->city->hasDistrict) {
return true;
}
$validator = Validator::make([$attribute => $value], [
$attribute => [
'required',
'integer',
Rule::in(District::availableDistricts()),
]
]);
$this->messages = $validator->messages();
return $validator->passes();
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return optional($this->messages)->first('district_id');
}
}
【问题讨论】:
-
您能出示完整的验证码吗?