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
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;
}
您应该使用MisdPhoneNumberBundle ,它为您提供有关电话号码和Symfony的所有信息。
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.
您应该使用Symfony Data Transformers执行此任务。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.