【问题标题】:Best way to validate an url in Symfony 2在 Symfony 2 中验证 url 的最佳方法
【发布时间】:2015-07-03 02:14:41
【问题描述】:

我正在验证我在 Symfony 中的一个表单。我的表格为四个字段;前两个是相同的信息,可以是任何文本或天堂空。我的第三个字段是网站的 url,第四个是我的linkedin 的 url。

现在,我唯一的验证是我的两个 url 是以 http 或 https 开头的 url,但由于我的字段是 url 字段,它已经在开头添加了它,所以它基本上总是正确的我写。诅咒,如果我不放任何东西,它会起作用,但我想要那样。

如果有验证类可以帮助我进行更多验证,例如检查地址是否存在或者linkedin是否实际上是linkedin的url,我正在徘徊?

这是我的代码

表格:

<?php

namespace AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

Class ModifierInfosType extends AbstractType
{

    public function buildForm(FormBuilderInterface $constructeur, array $options)
    {
        $constructeur
        ->add('travailFr', 'text', array('label'=>'Travail (Fr)'))
        ->add('travailEn', 'text', array('label'=>'Travail (En)'))
        ->add('lien', 'url', array('label'=>'Lien travail'))
        ->add('linkedin', 'url', array('label'=>'LinkedIn'))
        ->add('Modifier', 'submit');
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AdminBundle\Entity\Infos',
        ));
    }

    public function getName()
    {

        return 'portfolio_modifier_info';

    }

}

验证字段(PublicBundle\ressources\config\validation.yml):

PublicBundle\Entity\Infos:
    properties:
        lien:
            - Url:
        linkedin:
            - Url:

【问题讨论】:

标签: forms validation symfony


【解决方案1】:

我找到了一种方法来检查 url 是否存在和工作:

我做了一个自定义验证:

<?php

namespace AdminBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

Class ContrainteUrlExistValidator extends ConstraintValidator
{
    public function validate($url, Constraint $constraint)
    {
        //Vérifie si l'url peut être vide
        if(empty($url)&&$constraint->peutEtreVide)
        {
            return;
        }

        //Pattern pour trouver les url qui commence par http:// ou https://
        $pattern='/^(https?:\/\/)/';

        //Valide l'url et s'assure le preg_match a trouvé un match
        if(filter_var($url, FILTER_VALIDATE_URL)&&!empty(preg_match($pattern, $url, $matches)))
        {
            //Trouve l'host
            $hostname=parse_url($url, PHP_URL_HOST);

            //Tente de trouver l'adresse IP de l'host
            if (gethostbyname($hostname) !== $hostname)
            {
                //Cherche les données de l'entête
                $headers=get_headers($url);

                //Tente de trouver une erreur 404
                if(!strpos($headers[0], '404'))
                {
                    return;
                }
            }
        }

        //Crée une erreur
        $this->context->buildViolation($constraint->message)
                    ->setParameter('%string%', $url)
                    ->addViolation();
    }
}

【讨论】:

  • 谢谢!也许考虑在get_headers 之前添加ini_set('user_agent', 'Mozilla/5.0'); 之类的东西来欺骗您的用户代理并防止具有浏览器检测功能的网站(例如facebook)出现不同的结果。干杯!
猜你喜欢
  • 2015-12-25
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-18
相关资源
最近更新 更多