Validation doesn't work as expected
theedov opened this issue · 1 comments
theedov commented
Hi,
I'm new to this bundle and have a problem here.
Here is my entity:
/**
* @AssertPhoneNumber(type="mobile", message="The phone number is not valid")
* @ORM\Column(type="phone_number", nullable=true)
*/
private $phone;
public function getPhone(): ?PhoneNumber
{
return $this->phone;
}
public function setPhone(?PhoneNumber $phone): self
{
$this->phone = $phone;
return $this;
}
Form
->add('phone', PhoneNumberType::class, [
'default_region' => 'CZ',
'format' => PhoneNumberFormat::NATIONAL,
])
But it doesn't work as expected.
When I type just 00, 22 or 346536756347, etc... it passes and is saved in db as +42000, +42022 or +420346536756347, etc..
When I type string or just 0, I get expected error "The phone number is not valid"
Tried with AU region and had the same issue.
theedov commented
Used validating constrains in form instead and it works.
->add('phone', PhoneNumberType::class, [
'required' => false,
'default_region' => 'CZ',
'format' => PhoneNumberFormat::NATIONAL,
'constraints' => [
new PhoneNumber([
'type' => PhoneNumber::MOBILE
])
]
])