Php 在Symfony中验证字段的另一种方法
我用Symfony 3.2编写了这个应用程序。我现在正在做的就是编写添加图像/youtube视频功能。我的应用程序的逻辑是,无论用户选择一个对象还是第二个对象,数据都存储在同一个数据库表Php 在Symfony中验证字段的另一种方法,php,validation,symfony,Php,Validation,Symfony,我用Symfony 3.2编写了这个应用程序。我现在正在做的就是编写添加图像/youtube视频功能。我的应用程序的逻辑是,无论用户选择一个对象还是第二个对象,数据都存储在同一个数据库表objects: objectID object\u typeID object\u title 对象\u文件名(如果为空,则为其视频) 对象链接(如果为空,则为其图像) 现在,在添加视频时,链接字段通过以下方法进行验证: /** * Check if link is valid youtube URL.
objects
:
objectID
object\u typeID
object\u title
(如果为空,则为其视频)对象\u文件名
(如果为空,则为其图像)对象链接
链接
字段通过以下方法进行验证:
/**
* Check if link is valid youtube URL.
*
* @Assert\IsTrue(message = "This is not valid....")
*
* @return bool
*/
public function isValidLink() {
$rx = '~
^(?:https?://)? # Optional protocol
(?:www\.)? # Optional subdomain
(?:youtube\.com|youtu\.be)/ # Mandatory domain name
(?:watch\?v=)? # optional
(?:[a-zA-Z0-9]{11}) # mandatory
~x'
;
return preg_match($rx, $this->link);
}
当然,有两种不同的形式来添加每个对象。问题是,在添加图像时也会验证链接字段
因此,如何以这种方式验证链接字段以保持我当前的系统架构?好的,我找到了解决方案。验证我的表单的最佳方法是创建自定义验证器并添加它,即通过表单生成器:
$builder->add('link', TextType::class, array(
'label' => 'Link to the YT video',
'constraints' => [
new YouTubeURL(),
],
))
// ...
我还将为后代编写验证程序文件:)
AppBundle/Validator/Constraints/YouTubeURL.php:
namespace AppBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
class YouTubeURL extends Constraint
{
public $message = 'This is not the valid YouTube URL.';
}
AppBundle/Validator/Constraints/YouTubeURLValidator.php:
namespace AppBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class YouTubeURLValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
$rx = '~
^(?:https?://)? # Optional protocol
(?:www\.)? # Optional subdomain
(?:youtube\.com|youtu\.be)/ # Mandatory domain name
(?:watch\?v=)? # optional part
(?:[a-zA-Z0-9]{11}) # mandatory video id
~x'
;
if (!preg_match($rx, $value))
{
$this->context->buildViolation($constraint->message)
->addViolation();
}
}
}
好的,我找到了解决办法。验证我的表单的最佳方法是创建自定义验证器并添加它,即通过表单生成器:
$builder->add('link', TextType::class, array(
'label' => 'Link to the YT video',
'constraints' => [
new YouTubeURL(),
],
))
// ...
我还将为后代编写验证程序文件:)
AppBundle/Validator/Constraints/YouTubeURL.php:
namespace AppBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
class YouTubeURL extends Constraint
{
public $message = 'This is not the valid YouTube URL.';
}
AppBundle/Validator/Constraints/YouTubeURLValidator.php:
namespace AppBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class YouTubeURLValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
$rx = '~
^(?:https?://)? # Optional protocol
(?:www\.)? # Optional subdomain
(?:youtube\.com|youtu\.be)/ # Mandatory domain name
(?:watch\?v=)? # optional part
(?:[a-zA-Z0-9]{11}) # mandatory video id
~x'
;
if (!preg_match($rx, $value))
{
$this->context->buildViolation($constraint->message)
->addViolation();
}
}
}