'/^\(\d{3}\)\s\d{3}-\d{4}(?:\sext\.\s\d{1,6})?$/', self::DOT_FORMAT => '/^\d{3}\.\d{3}\.\d{4}(?:\sext\.\s\d{1,6})?$/', ]; protected $_placeholders = [ self::DASH_FORMAT => '(555) 555-5555 ext. 123', self::DOT_FORMAT => '555.555.5555 ext. 123', ]; protected $_format = self::DOT_FORMAT; public function init() { $pattern = $this->_formatPatterns[$this->_format]; if (!$pattern) { throw new Qs_Exception('Unknown phone pattern.'); } $this->addValidator( 'regex', true, ['pattern' => $pattern, 'messages' => 'Phone number is in wrong format'] ); if (!$this->getFilter('Phone')) { $this->addFilter('Phone', [$this->_format]); } return $this; } public function setFormat($format) { if (!in_array($format, array_keys($this->_formatPatterns))) { throw new Qs_Exception('Unknown phone format.'); } $this->_format = $format; return $this; } public function getFormat() { return $this->_format; } protected function initPlaceholder() { if (null === $this->getAttrib('placeholder') && ($placeholder = Qs_Array::get($this->_placeholders, $this->getFormat())) ) { $this->setAttrib('placeholder', $placeholder); } return $this; } public function getAttribs() { $this->initPlaceholder(); return parent::getAttribs(); } public function loadDefaultDecorators() { if ($this->loadDefaultDecoratorsIsDisabled()) { return $this; } $decorators = $this->getDecorators(); if (empty($decorators)) { $this->addDecorator('ViewHelper') ->addDecorator('Phone') ->addDecorator('Errors') ->addDecorator('Description', ['tag' => 'p', 'class' => 'description']) ->addDecorator('HtmlTag', ['tag' => 'dd', 'id' => $this->getName() . '-element']) ->addDecorator('Label', ['tag' => 'dt']); } return $this; } }