Skip to content
Snippets Groups Projects
Commit e2d122cd authored by Jared Hancock's avatar Jared Hancock
Browse files

Make phone number field configurable

Fixes #23
parent 7761b165
No related branches found
No related tags found
No related merge requests found
...@@ -500,27 +500,58 @@ class TextareaField extends FormField { ...@@ -500,27 +500,58 @@ class TextareaField extends FormField {
class PhoneField extends FormField { class PhoneField extends FormField {
static $widget = 'PhoneNumberWidget'; static $widget = 'PhoneNumberWidget';
function getConfigurationOptions() {
return array(
'ext' => new BooleanField(array(
'label'=>'Extension', 'default'=>true,
'configuration'=>array(
'desc'=>'Add a separate field for the extension',
),
)),
'digits' => new TextboxField(array(
'label'=>'Minimum length', 'default'=>7,
'hint'=>'Fewest digits allowed in a valid phone number',
'configuration'=>array('validator'=>'number', 'size'=>5),
)),
'format' => new ChoiceField(array(
'label'=>'Display format', 'default'=>'us',
'choices'=>array(''=>'-- Unformatted --',
'us'=>'United States'),
)),
);
}
function validateEntry($value) { function validateEntry($value) {
parent::validateEntry($value); parent::validateEntry($value);
$config = $this->getConfiguration();
# Run validator against $this->value for email type # Run validator against $this->value for email type
list($phone, $ext) = explode("X", $value, 2); list($phone, $ext) = explode("X", $value, 2);
if ($phone && !Validator::is_phone($phone)) if ($phone && (
!is_numeric($phone) ||
strlen($phone) < $config['digits']))
$this->_errors[] = "Enter a valid phone number"; $this->_errors[] = "Enter a valid phone number";
if ($ext) { if ($ext && $config['ext']) {
if (!is_numeric($ext)) if (!is_numeric($ext))
$this->_errors[] = "Enter a valide phone extension"; $this->_errors[] = "Enter a valid phone extension";
elseif (!$phone) elseif (!$phone)
$this->_errors[] = "Enter a phone number for the extension"; $this->_errors[] = "Enter a phone number for the extension";
} }
} }
function to_database($value) { function parse($value) {
return preg_replace('/[()+. -]/', '', $value); // NOTE: Value may have a legitimate 'X' to separate the number and
// extension parts. Don't remove the 'X'
return preg_replace('/[^\dX]/', '', $value);
} }
function toString($value) { function toString($value) {
$config = $this->getConfiguration();
list($phone, $ext) = explode("X", $value, 2); list($phone, $ext) = explode("X", $value, 2);
$phone=Format::phone($phone); switch ($config['format']) {
case 'us':
$phone = Format::phone($phone);
break;
}
if ($ext) if ($ext)
$phone.=" x$ext"; $phone.=" x$ext";
return $phone; return $phone;
...@@ -569,7 +600,7 @@ class ChoiceField extends FormField { ...@@ -569,7 +600,7 @@ class ChoiceField extends FormField {
if (is_numeric($value)) if (is_numeric($value))
return $value; return $value;
foreach ($this->getChoices() as $k=>$v) foreach ($this->getChoices() as $k=>$v)
if (strcasecmp($value, $v) === 0) if (strcasecmp($value, $k) === 0)
return $k; return $k;
} }
...@@ -828,12 +859,17 @@ class TextareaWidget extends Widget { ...@@ -828,12 +859,17 @@ class TextareaWidget extends Widget {
class PhoneNumberWidget extends Widget { class PhoneNumberWidget extends Widget {
function render() { function render() {
$config = $this->field->getConfiguration();
list($phone, $ext) = explode("X", $this->value); list($phone, $ext) = explode("X", $this->value);
?> ?>
<input type="text" name="<?php echo $this->name; ?>" value="<?php <input type="text" name="<?php echo $this->name; ?>" value="<?php
echo $phone; ?>"/> Ext: <input type="text" name="<?php echo $phone; ?>"/><?php
// Allow display of extension field even if disabled if the phone
// number being edited has an extension
if ($ext || $config['ext']) { ?> Ext:
<input type="text" name="<?php
echo $this->name; ?>-ext" value="<?php echo $ext; ?>" size="5"/> echo $this->name; ?>-ext" value="<?php echo $ext; ?>" size="5"/>
<?php <?php }
} }
function getValue() { function getValue() {
...@@ -842,6 +878,7 @@ class PhoneNumberWidget extends Widget { ...@@ -842,6 +878,7 @@ class PhoneNumberWidget extends Widget {
if ($base === null) if ($base === null)
return $base; return $base;
$ext = $data["{$this->name}-ext"]; $ext = $data["{$this->name}-ext"];
// NOTE: 'X' is significant. Don't change it
if ($ext) $ext = 'X'.$ext; if ($ext) $ext = 'X'.$ext;
return $base . $ext; return $base . $ext;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment