Match one of X validation rules on field

I would like to have a TextInput for a hostname that can either be ->activeUrl() or ->ip(), is there a way I could make sure one of those validation rules match?
2 Replies
deadbeef
deadbeefOP9mo ago
Thanks, for others - ended up with the following:
->rules([
'required',
fn() => function (string $attribute, $value, Closure $fail) {
$validIp = filter_var($value, FILTER_VALIDATE_IP) !== false;
if ($validIp) {
return;
}
$validUrl = filter_var($value, FILTER_VALIDATE_DOMAIN) !== false;
if (!$validIp && !$validUrl) {
$fail('The :attribute must be a valid IP address or domain.');
}
$validDns = checkdnsrr($value) !== false;
if (!$validDns) {
$fail('DNS lookup failed for the provided domain name.');
}
}
])
->rules([
'required',
fn() => function (string $attribute, $value, Closure $fail) {
$validIp = filter_var($value, FILTER_VALIDATE_IP) !== false;
if ($validIp) {
return;
}
$validUrl = filter_var($value, FILTER_VALIDATE_DOMAIN) !== false;
if (!$validIp && !$validUrl) {
$fail('The :attribute must be a valid IP address or domain.');
}
$validDns = checkdnsrr($value) !== false;
if (!$validDns) {
$fail('DNS lookup failed for the provided domain name.');
}
}
])
Want results from more Discord servers?
Add your server