silverstripe-subsites/code/forms/WildcardDomainField.php

51 lines
1.0 KiB
PHP
Raw Normal View History

2017-05-29 13:42:42 +02:00
<?php
namespace SilverStripe\Subsites\Forms;
2017-05-29 13:42:42 +02:00
use SilverStripe\Forms\TextField;
/**
* A text field that accepts only valid domain names, but allows the wildcard (*) character
*/
class WildcardDomainField extends TextField
{
2017-05-29 13:42:42 +02:00
/**
* Validate this field as a valid hostname
*
* @param Validator $validator
* @return bool
*/
public function validate($validator)
{
if ($this->checkHostname($this->Value())) {
return true;
}
$validator->validationError(
$this->getName(),
_t("DomainNameField.INVALID_DOMAIN", "Invalid domain name"),
"validation"
);
2017-05-29 13:42:42 +02:00
return false;
}
/**
* Check if the given hostname is valid.
*
* @param string $hostname
* @return bool True if this hostname is valid
*/
public function checkHostname($hostname)
{
return (bool)preg_match('/^([a-z0-9\*]+[\-\.])*([a-z0-9\*]+)$/', $hostname);
}
public function Type()
{
return 'text wildcarddomain';
}
2017-05-29 13:42:42 +02:00
}