public function IpFieldValidationRule::validate in Field Validation 8
Overrides FieldValidationRuleBase::validate
File
- src/
Plugin/ FieldValidationRule/ IpFieldValidationRule.php, line 83
Class
- IpFieldValidationRule
- IpFieldValidationRule.
Namespace
Drupal\field_validation\Plugin\FieldValidationRuleCode
public function validate($params) {
$value = isset($params['value']) ? $params['value'] : '';
$rule = isset($params['rule']) ? $params['rule'] : null;
$context = isset($params['context']) ? $params['context'] : null;
$settings = array();
if (!empty($rule) && !empty($rule->configuration)) {
$settings = $rule->configuration;
}
if ($value !== '' && !is_null($value)) {
$version = isset($settings['version']) ? $settings['version'] : '';
switch ($version) {
case '4':
$flag = FILTER_FLAG_IPV4;
break;
case '6':
$flag = FILTER_FLAG_IPV6;
break;
case '4_no_priv':
$flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE;
break;
case '6_no_priv':
$flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE;
break;
case 'all_no_priv':
$flag = FILTER_FLAG_NO_PRIV_RANGE;
break;
case '4_no_res':
$flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_RES_RANGE;
break;
case '6_no_res':
$flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE;
break;
case 'all_no_res':
$flag = FILTER_FLAG_NO_RES_RANGE;
break;
case '4_public':
$flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
break;
case '6_public':
$flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
break;
case 'all_public':
$flag = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
break;
default:
$flag = NULL;
break;
}
if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) {
$context
->addViolation($rule
->getErrorMessage());
}
}
}