php实现访问前验证,php-两次访问自定义验证器

我在Symfony2项目上有一个自定义验证器.

验证工作正常,但是以某种方式两次访问了该方法.

问题是下一个:

如您所见,错误消息显示两次.当我尝试在validate方法中进行var dump时,vardump也显示两次.知道为什么两次调用验证吗?当我使用$form-> bind($request);在我的控制器中.

编辑

这是树枝模板:

{% extends 'MerrinMainBundle::layout.html.twig' %}

{% block page_title %}

MDPI Conversion system (Merrin) 3.0 - New Conversion

{% endblock %}

{% block main %}

{% for flashMessage in app.session.flashbag.get('user-notice') %}

{% autoescape false %}

{{ flashMessage }}

{% endautoescape %}

{% endfor %}

Create New Manuscript

{% if valid == false %}

{{ form_errors(form) }}

{{ form_errors(form.doi) }}

{{ form_errors(form.publisher) }}

{{ form_errors(form.file) }}

{% endif %}

{% endblock %}

和控制器调用

public function createAction()

{

$em_scipub = $this->getDoctrine()->getManager();

$em_mdpipub = $this->getDoctrine()->getManager('mdpipub');

$enquiry = new Manuscript();

$formType = new NewManuscriptType();

$form = $this->createForm($formType, $enquiry);

$request = $this->getRequest();

$valid = true;

$error = '';

if ($request->isMethod('POST')) {

$form->bind($request);

if ($form->isValid()) {

... do something ...

$em_scipub->persist($enquiry);

$em_scipub->flush();

$flash_message = "New Manuscript sucessfully created.";

$this->get('session')->getFlashBag()->set('user-notice', $flash_message);

return $this->redirect($this->generateUrl('MerrinMainBundle_new'));

}

else

$valid = false;

}

$path = $this->generateUrl('MerrinMainBundle_new');

return $this->render('MerrinMainBundle:Pages:new_conversion.html.twig.twig', array(

'valid' => $valid,

'path' => $path,

'form' => $form->createView(),

) );

}

编辑2:

验证功能:

public function validate($value, Constraint $constraint)

{

$doi = $value->getDoi();

preg_match('/[^\/]+/i', $doi, $publisherDoiAbbr);

if($publisherDoiAbbr[0] !== $value->getPublisher()->getDoiAbbreviation()) {

$this->context->addViolation($constraint->message_publisher_DOI);

}

else {

preg_match("/[a-z]+/",$doi, $journalDoiAbbr);

$em_mdpipub = $this->entityManager;

$journal = $em_mdpipub->getRepository('MerrinMdpiPubBundle:Journal')->findOneBy(array('doi_abbreviation' => $journalDoiAbbr));

if($journal == null) {

$this->context->addViolation($constraint->message_journal_DOI);

}

}

preg_match('/\d*$/i', $doi, $doiNumericPart);

if(strlen($doiNumericPart[0]) < 8) {

$this->context->addViolation($constraint->message_volume_issue_firstpage_DOI);

}

}

和树枝模板:

{% extends 'MerrinMainBundle::layout.html.twig' %}

{% block page_title %}

MDPI Conversion system (Merrin) 3.0 - New Conversion

{% endblock %}

{% block main %}

{% for flashMessage in app.session.flashbag.get('user-notice') %}

{% autoescape false %}

{{ flashMessage }}

{% endautoescape %}

{% endfor %}

Create New Manuscript

{% if valid == false %}

{{ form_errors(form) }}

{{ form_errors(form.doi) }}

{{ form_errors(form.publisher) }}

{{ form_errors(form.file) }}

{% endif %}

{{ form_label(form.doi) }}

{{ form_widget(form.doi, { 'attr': {'size': 40} }) }}

{{ form_label(form.publisher) }}

{{ form_widget(form.publisher) }}

{{ form_label(form.file) }}

{{ form_widget(form.file) }}

Cancel

{{ form_rest(form) }}

{% endblock %}

编辑3:

这是我将验证器应用于实体的方式:

/**

* Manuscript

*

* @IsDOI()

* @ORM\Table(name="manuscripts")

* @ORM\Entity(repositoryClass="Merrin\MainBundle\Repository\ManuscriptRepository")

* @ORM\HasLifecycleCallbacks

*

*/

class Manuscript

{

....

}

编辑4:

当我尝试推销

$form->getErrors();

我得到一个带有两个值的数组:

array(2) {

[0]=>

object(Symfony\Component\Form\FormError)#507 (4) {

["message":"Symfony\Component\Form\FormError":private]=>

string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"

["messageTemplate":protected]=>

string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"

["messageParameters":protected]=>

array(0) {

}

["messagePluralization":protected]=>

NULL

}

[1]=>

object(Symfony\Component\Form\FormError)#542 (4) {

["message":"Symfony\Component\Form\FormError":private]=>

string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"

["messageTemplate":protected]=>

string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"

["messageParameters":protected]=>

array(0) {

}

["messagePluralization":protected]=>

NULL

}

}

解决方法:

如果您正在使用validation groups并将验证器应用于多个组,则可以这样做. @IsDOI()注释是什么意思?如果可能应用验证,则可以先在validation.yml中添加验证器,然后再通过此自定义注释添加验证器.

标签:symfony,dependency-injection,customvalidator,php

来源: https://codeday.me/bug/20191030/1969543.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值