Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Commit

Permalink
Add support for default prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtak-CZ committed Nov 15, 2014
1 parent 566eedd commit 3daa251
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ $form->addComponent(new \Nella\Forms\Controls\PhoneNumberInput('Phone'), 'phone'
\Nella\Forms\Controls\PhoneNumberInput::register();
$form->addPhone('phone', 'Phone');

// Optional phone numnber validation
// Optional phone number validation
$form['phone']
->addCondition(\Nette\Application\UI\Form::FILLED)
->addRule([$form['phone'], 'validatePhoneNumber'], 'Phone number is invalid');


// Optional phone number default prefix
$control = $form->addPhone('phone', 'Phone');
$control->setDefaultPrefix('+420');
```

Manual rendering
Expand Down
19 changes: 18 additions & 1 deletion src/Nella/Forms/Controls/PhoneNumberInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ class PhoneNumberInput extends \Nette\Forms\Controls\BaseControl
/** @var string */
private $number;

/** @var string */
private $defaultPrefix;

/**
* @param string
* @return \Nella\Forms\Controls\PhoneNumberInput
Expand Down Expand Up @@ -364,6 +367,20 @@ public function getValue()
return $value;
}

/**
* @param string
* @return \Nella\Forms\Controls\PhoneNumberInput
*/
public function setDefaultPrefix($prefix)
{
if (!in_array($prefix, static::$phonePrefixes, TRUE) && !is_null($prefix)) {
throw new \Nette\InvalidArgumentException('This prefix is not supported');
}
$this->defaultPrefix = $prefix;

return $this;
}

/**
* @return bool
*/
Expand Down Expand Up @@ -396,7 +413,7 @@ public function getControlPart($key)
$control = \Nette\Forms\Helpers::createSelectBox(
array_combine(static::$phonePrefixes, static::$phonePrefixes),
array(
'selected?' => $this->prefix,
'selected?' => $this->prefix === NULL ? $this->defaultPrefix : $this->prefix,
)
);
$control->name($name . '[' . static::NAME_PREFIX . ']')->id($this->getHtmlId());
Expand Down
29 changes: 29 additions & 0 deletions tests/Nella/Forms/Controls/PhoneNumberInputTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,35 @@ class PhoneNumberInputTest extends \Tester\TestCase
Assert::same($form, $control->getForm());
}

public function testDefaultPrefix()
{
$control = $this->createControl();

$dq = \Tester\DomQuery::fromHtml((string) $control->getControlPart('prefix'));
Assert::false($dq->has("select option[selected]"));

$control->setDefaultPrefix('+420');

$dq = \Tester\DomQuery::fromHtml((string) $control->getControlPart('prefix'));
Assert::true($dq->has("select option[selected]"));
Assert::true($dq->has("select option[value=+420][selected]"));

$control->setDefaultPrefix(NULL);

$dq = \Tester\DomQuery::fromHtml((string) $control->getControlPart('prefix'));
Assert::false($dq->has("select option[selected]"));
}

/**
* @throws \Nette\InvalidArgumentException
*/
public function testDefaultPrefixInvalid()
{
$control = $this->createControl();

$control->setDefaultPrefix(FALSE);
}

private function createControl($data = array())
{
$_SERVER['REQUEST_METHOD'] = 'POST';
Expand Down

0 comments on commit 3daa251

Please sign in to comment.