gpt4 book ai didi

php - 自定义验证器被访问两次

转载 作者:可可西里 更新时间:2023-11-01 01:03:15 30 4
gpt4 key购买 nike

我的 Symfony2 项目上有一个自定义验证器。验证工作正常,但该方法以某种方式被访问了两次。

这是我的自定义验证器:My other resolved question

问题是下一个:enter image description here

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

编辑

这是 Twig 模板:

{% 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') %}
<div class="flash-notice">
{% autoescape false %}
{{ flashMessage }}
{% endautoescape %}
</div>
{% endfor %}
<h1>Create New Manuscript</h1>
{% if valid == false %}
<div class="error">
{{ form_errors(form) }}
{{ form_errors(form.doi) }}
{{ form_errors(form.publisher) }}
{{ form_errors(form.file) }}
</div>
{% endif %}

<form action="{{ path }}" method="POST" {{ form_enctype(form) }}>
</form>

{% endblock %}

Controller 调用

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 = "<a href='edit/".$enquiry->getId()."'>New Manuscript</a> 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);
}
}

和 Twig 模板:

{% 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') %}
<div class="flash-notice">
{% autoescape false %}
{{ flashMessage }}
{% endautoescape %}
</div>
{% endfor %}
<h1>Create New Manuscript</h1>
{% if valid == false %}
<div class="error">
{{ form_errors(form) }}
{{ form_errors(form.doi) }}
{{ form_errors(form.publisher) }}
{{ form_errors(form.file) }}
</div>
{% endif %}

<form action="{{ path }}" method="POST" {{ form_enctype(form) }}>
<div style="float:left;">
<table width="700">
<tr>
<td>
{{ form_label(form.doi) }}
</td>
<td>
{{ form_widget(form.doi, { 'attr': {'size': 40} }) }}
</td>
</tr>
<tr>
<td>
{{ form_label(form.publisher) }}
</td>
<td>
{{ form_widget(form.publisher) }}
</td>
</tr>
<tr>
<td>
{{ form_label(form.file) }}
</td>
<td>
{{ form_widget(form.file) }}
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
<td>
<input class="submit-confirm-button" type="submit" name="update-text" value="submit" />
<a class="cancel-link" href="{{ path('MerrinMainBundle_homepage' ) }}">Cancel</a>
</td>
</tr>
</table>
</div>
{{ form_rest(form) }}
</form>
{% endblock %}

编辑 3:

下面是我如何将验证器应用于实体:

/**
* Manuscript
*
* @IsDOI()
* @ORM\Table(name="manuscripts")
* @ORM\Entity(repositoryClass="Merrin\MainBundle\Repository\ManuscriptRepository")
* @ORM\HasLifecycleCallbacks
*
*/
class Manuscript
{
....
}

编辑 4:

当我尝试 vardump 时

$form->getErrors();

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

array(2) {
[0]=&gt;
object(Symfony\Component\Form\FormError)#507 (4) {
["message":"Symfony\Component\Form\FormError":private]=&gt;
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageTemplate":protected]=&gt;
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageParameters":protected]=&gt;
array(0) {
}
["messagePluralization":protected]=&gt;
NULL
}
[1]=&gt;
object(Symfony\Component\Form\FormError)#542 (4) {
["message":"Symfony\Component\Form\FormError":private]=&gt;
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageTemplate":protected]=&gt;
string(77) "The Publisher DOI abbreviation does not correspond to the DOI you filled in !"
["messageParameters":protected]=&gt;
array(0) {
}
["messagePluralization":protected]=&gt;
NULL
}
}

最佳答案

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

关于php - 自定义验证器被访问两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17672870/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com