shuba.ivan shuba.ivan - 6 months ago 59
PHP Question

Symfony validate telephone in form field

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

Answer

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;
}
Comments