'/^\(\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, array( 'pattern' => $pattern, 'messages' => 'Phone number is in wrong format') ); if (!$this->getFilter('Phone')) { $this->addFilter('Phone', array($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', array('tag' => 'p', 'class' => 'description')) ->addDecorator('HtmlTag', array('tag' => 'dd', 'id' => $this->getName() . '-element')) ->addDecorator('Label', array('tag' => 'dt')); } return $this; } }