Symfony validate telephone in form field
Solution 1
After installing the MisdPhoneNumberBundle, you can use the bundle's validator:
use Doctrine\ORM\Mapping as ORM;
use Misd\PhoneNumberBundle\Validator\Constraints as MisdAssert;
/**
* @ORM\Entity
* @ORM\Table
*/
class Entity
{
// ...
/**
* @ORM\Column(type="string", length=50)
* @MisdAssert\PhoneNumber()
*/
private $phone;
}
Solution 2
You should use MisdPhoneNumberBundle, it gives you all you need regarding phone numbers and Symfony.
Solution 3
I do not understand by what mean you're trying to replace #
by something else. If this is the preg_replace
call, it is not amazing that it does not work, as you're looking for a string that looks like:
#<br, (optional spaces), (Maybe /), >#
If you want to replace <br/>
by \n
like the comment says, you need to put this pattern:
/<br\s*\/?\s*>/i
Note the /
at start and end to limit the pattern, and i
at the end means any case match.
If you want to replace #
, spaces and _
, I think that the best would be to replace any character which is not a number of (
, )
. In which case this regexp should work (not tested):
preg_replace('/[^0-9()]+/', "", $originalDescription);
Which means: replace any sequence of 1 or more character which NOT (because of '^') in 0-9
, (
or )
by an empty string.
Related videos on Youtube
shuba.ivan
Updated on June 04, 2022Comments
-
shuba.ivan over 1 year
I have Symfony 2.6 and form for Personal Information in form field telephone, like rhis
+38 (918) 280-1594
and if developer write "_" or more digits, space. How in action I check this ? Like developer write
+38 (918) 2 801_594
And I set in DB
+38(918)2801594
what are the processes and decisions or bundles to solve this problem?
->add('telephone', null, array('label' => 'Telephone', 'max_length' => 255, 'required' => false)); $builder->get('telephone')->addModelTransformer(new CallbackTransformer( // transform <br/> to \n so the textarea reads easier function ($originalDescription) { return preg_replace('/[^0-9()]+/', "", $originalDescription); }, function ($submittedDescription) { // remove most HTML tags (but not br,p) $cleaned = strip_tags($submittedDescription, '<br><br/><p>'); // transform any \n to real <br/> return str_replace("\n", '<br/>', $cleaned); } ));
But how it works? I write telephone 65454### and this "#" write in my DB this is wrong. Why I not Understand? And I need determine the number of digits (12 digits). Maybe Example please.
I post +30632666$$# and in DB set this +30632666$$# but in form visible this 30632666 this is exactly I need in my DB, I need set in database correct telephone and limit 13 chars - one "+" and 12 - digital
-
shuba.ivan over 8 yearsHow I do solve my problem if I use this bundle ? I need add assert in field my entity or use in form? Example please
-
shuba.ivan over 8 yearsI add in filed entity ` /** * @ORM\Column(type="phone_number") */ protected $telephone;` and in form
->add('phone_number', 'tel', array('default_region' => 'GB', 'format' => PhoneNumberFormat::NATIONAL))
and now error -
shuba.ivan over 8 yearstype field, I check. But I want simple solution. I need 12 digital and not special characters, just digital
-
shuba.ivan over 8 yearsthis is good but I post +30632666$$# and in DB set this +30632666$$# but in form visible this 30632666 this is exactly I need in my DB, I need set in database correct telephone and limit 13 chars - one "+" and 12 - digital
-
Yassine Guedidi over 8 years"type field"? Have you got a more complete error message? Do you follow all installation instruction of the bundle? Especially Doctrine mapping, and update you database schema.
-
shuba.ivan over 8 yearshelp please I post +30632666$$# and in DB set this +30632666$$# but in form visible this 30632666 this is exactly I need in my DB