Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to support standalone value validating #2124

Merged
merged 4 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md).

## 2.2.0 ()

+ [#2124](https://github.com/luyadev/luya/pull/2124) `luya\validators\PhoneNumberValidator` can now be used as standalone validator like this: `(new PhoneNumberValidator())->validate('+411231245')`.

## 2.1.0 (27. April 2022)

+ [#2123](https://github.com/luyadev/luya/pull/2123) Added new property `$events` to add custom events in `luya\behaviors\JsonBehavior`. F.e `['class' => JsonBehavior::class, 'events' => [NgRestModel::EVENT_AFTER_NGREST_FIND => 'decodeAttributes']]`.
Expand Down
35 changes: 26 additions & 9 deletions core/validators/PhoneNumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class PhoneNumberValidator extends Validator
*/
public $autoFormatFormat = PhoneNumberFormat::E164;

/**
* @var string This property will recieved the formated value (the parsed value lets say).
* @since 2.2.0
*/
public $formatedValue = null;

/**
* @var integer If enabled, the validator will check the type of number. This can be usefull to test for mobile phone numbers.
*
Expand All @@ -57,28 +63,39 @@ class PhoneNumberValidator extends Validator
*/
public function validateAttribute($model, $attribute)
{
$phoneUtil = PhoneNumberUtil::getInstance();
parent::validateAttribute($model, $attribute);

$value = $model->{$attribute};
if ($this->autoFormat) {
$model->{$attribute} = $this->formatedValue;
}
}

/**
* {@inheritDoc}
*/
protected function validateValue($value)
{
$phoneUtil = PhoneNumberUtil::getInstance();

try {
$number = $phoneUtil->parse($value, $this->country);

if (!$number || !$phoneUtil->isValidNumber($number)) {
return $this->addError($model, $attribute, Yii::t('luya', 'Invalid phone number.'));
return [Yii::t('luya', 'Invalid phone number.'), []];
}

if ($this->type !== null && ($phoneUtil->getNumberType($number) !== $this->type)) {
$typeName = PhoneNumberType::values()[$this->type];
return $this->addError($model, $attribute, Yii::t('luya', 'The phone number does not match the required type {name}.', ['name' => $typeName]));
return [Yii::t('luya', 'The phone number does not match the required type {name}.', ['name' => $typeName]), []];
}

// refactor the phone number
if ($this->autoFormat) {
$model->{$attribute} = $phoneUtil->format($number, $this->autoFormatFormat);
}
// refactor the phone number and store in property for later use
$this->formatedValue = $phoneUtil->format($number, $this->autoFormatFormat);

} catch (NumberParseException $exception) {
$this->addError($model, $attribute, Yii::t('luya', 'Invalid phone number, ensure it starts with the correct country code.'));
return [Yii::t('luya', 'Invalid phone number, ensure it starts with the correct country code.'), []];
}

return null;
}
}
16 changes: 16 additions & 0 deletions tests/core/validators/PhoneNumberValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ public function testTypeCompare()
);
$this->assertTrue($model->hasErrors());
}

public function testStandAloneValidationError()
{
$validator = new PhoneNumberValidator(['type' => PhoneNumberType::MOBILE]);
$validator->validate('123123', $error);
$this->assertSame('Invalid phone number, ensure it starts with the correct country code.', $error);
}

public function testStandAloneValidationSuccess()
{
$validator = new PhoneNumberValidator(['type' => PhoneNumberType::MOBILE]);
$validator->validate('+41791234567', $error);
$this->assertEmpty($error);
$this->assertSame('+41791234567', $validator->formatedValue);

}
}

class StubModelValidatorPhone extends Model
Expand Down