Source of file PhoneField.php
Size: 2,038 Bytes - Last Modified: 2021-12-23T10:51:24+00:00
/var/www/docs.ssmods.com/process/src/src/Fields/PhoneField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 | <?php namespace TheWebmen\Formbuilder\Fields; use SilverStripe\Core\Config\Configurable; class PhoneField extends \SilverStripe\Forms\TextField { use Configurable; /** * {@inheritdoc} */ public function Type() { return 'phone number'; } /** * Validates for RFC 2822 compliant email addresses. * * @see http://www.regular-expressions.info/email.html * @see http://www.ietf.org/rfc/rfc2822.txt * * @param Validator $validator * * @return string */ public function validate($validator) { $this->value = trim($this->value); $pattern = '^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$'; $validationPattern = self::config()->get('validation_pattern'); if (!is_null($validationPattern)) $pattern = $validationPattern; if (!is_array($pattern)) { if ($this->value && !preg_match('/' . $pattern . '/i', $this->value)) { $validator->validationError( $this->name, _t('SilverStripe\\Forms\\PhoneField.VALIDATION', 'Please enter a valid phonenumber.'), 'validation' ); return false; } } else { foreach ($pattern as $idx => $item) { $pattern[$idx] = ($this->value && preg_match('/' . $item . '/i', $this->value)); } if (array_search(true, $pattern) === false) { $validator->validationError( $this->name, _t('SilverStripe\\Forms\\PhoneField.VALIDATION', 'Please enter a valid phonenumber.'), 'validation' ); return false; } } return true; } public function getSchemaValidation() { $rules = parent::getSchemaValidation(); $rules['phone'] = true; return $rules; } } |