I have a select list, where the first option is disabled, that's way when the user does not choose an valid option, the result of the select list will not be in the request.
In the validation, the field required, if the value of an other field is for example : 1, in case not 1, the field is not required.
The code:
'city_id' => [
'required',
'integer',
Rule::in(City::availableCities()),
],
'district_id' => new DistrictValidation(request('city_id')),
How I can do that, the district_id throw the validation every time, regardless of, it is in the request, or not.
Thanks for answers,
Update: Maybe you see clearly, if the DistrictValidation rule is here:
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');
}
}