Symfony验证作为实体中的注释:什么是错误?

Symfony验证作为实体中的注释:什么是错误?,symfony,validation,Symfony,Validation,验证没有反应并阻止提交。 有人能告诉我遗漏了什么或者我做错了什么吗? 显然,当调用方法isValid时,验证器会阻止验证。 在实体中,此变量的验证如下所示: /** *@var字符串 *@Assert\NotBlank() */ 私人$pGaWeeks 提交会被阻止,无论是给变量pGaWeeks还是保留为空。 没有出现任何错误消息。当从代码中删除“@Assert\NotBlank()”时,提交工作正常 我还有一个DataHomeController.php public function new

验证没有反应并阻止提交。 有人能告诉我遗漏了什么或者我做错了什么吗? 显然,当调用方法isValid时,验证器会阻止验证。 在实体中,此变量的验证如下所示: /** *@var字符串 *@Assert\NotBlank() */ 私人$pGaWeeks

提交会被阻止,无论是给变量pGaWeeks还是保留为空。 没有出现任何错误消息。当从代码中删除“@Assert\NotBlank()”时,提交工作正常

我还有一个DataHomeController.php

public function newAction(Request $request){

        if(!$this->hasFuAccess() || !$this->isAdmin()){
            throw new AccessDeniedException("You do not have permission to access");
        }

        $newrecord =array();

        $dataA = new DataAPatient();
        array_push($newrecord,$dataA);
        $appendForm = $this->createNewForm($dataA);

        $appendForm->handleRequest($request);


        if($appendForm->isSubmitted()){//Save  
            if($appendForm->isValid()){    
             $tmp_pKpPatientid = $this->storeDataAPatient($dataA);
             $records = $this->getStepByStepSearch($dataA);
             return $this->render('GeneralCommonBundle:DataHome:show.html.twig', array(
                'records'=> $records,
                'isNew' => false,
                'newrecord' => $newrecord
                ));
         }
       //   }
        }

        return $this->render('DataLiveBundle:DataAPatient:new.html.twig', array(
            'form'   => $appendForm->createView(),
        ));


    }
在config.yaml中:

validation:      { enabled: true, enable_annotations: true }
和一个html.twig:

{% extends '::base.html.twig' %}

{% block body %}
{{ form_start(form, {attr: {novalidate: 'novalidate'}}) }}
{% form_theme form 'bootstrap_3_horizontal_layout.html.twig' %}

    <h1 style="margin-top:60px"><img src="{{ asset('public/images/icons/icon_data_gross.gif') }}"> Create a New Patient Dataset</h1>


          <div role="tabpanel" class="tab-pane active" id="PatientInfo">
              <div class="panel panel-default">
                  <div class="panel-body">
                     {% include 'DataLiveBundle:DataAPatient:form.html.twig' with {
                    'form':form, 'isNew': true  } only %} 
                  </div>
              </div>
          </div>

    {{ form_row(form._token) }}
    {{ form_end(form) }}



{% endblock %}
{% block body %}

            <div class="row" style='margin-bottom: 5px'>
                <div style='white-space: nowrap'>
                  <div class='col-xs-5'>
                    <div class='col-xs-5'>{{ form_label(form.pSnnid, 'SNN-ID (if known)', {'label_attr':{'style':'margin-top:3px'}})}}</div>
                   <div class='col-xs-2'>{{ form_widget(form.pSnnid, {'attr':{'style':'width:105px'}})}}</div>
                  </div>
                    <div class='col-xs-5'>
                  <div class='col-xs-5'>{{ form_label(form.pPlaceOfBirth, 'Place of birth', {'label_attr':{'style':'margin-top:3px' }})}}</div>
                  <div class='col-xs-2'>{{ form_widget(form.pPlaceOfBirth , {'attr': {'style':'width:200px' }})}}</div>
                  </div>
                </div>   
            </div>


            <div class="row" style='margin-bottom: 5px'>
                <div style='white-space: nowrap'>
                  <div class='col-xs-5'>
                    <div class='col-xs-5'>{{ form_label(form.pDateOfBirth, 'Date of birth', {'label_attr':{'style':'margin-top:3px'}})}}</div>
                    <div class='col-xs-2'> {{ form_widget(form.pDateOfBirth, {'id': 'P_Date_of_birth','attr':{'style':'width:100px', }})}}</div>
                  </div>
                  <div class='col-xs-5'>
                    <div class='col-xs-5'>{{ form_label(form.pPatientNo, 'Patient ID / Nr. NEO', {'label_attr':{'style':'margin-top:5px'}})}}</div>
                    <div class='col-xs-2'>{{ form_widget(form.pPatientNo, {'attr':{'style':'width:105px' }})}}</div>
                  </div>
                </div>         
            </div>

            <div class="row" style='margin-bottom: 5px'>
                <div style='white-space: nowrap'>
                  <div class='col-xs-5'>
                    <div class='col-xs-5'>{{ form_label(form.pGaWeeks, 'Gestational age', {'label_attr':{'style':'margin-top:3px'}})}}</div>
                    <div class='col-xs-2'> <div class="input-group">{{ form_widget(form.pGaWeeks, {'id': 'P_GA_weeks', 'attr':{'style':'width:45px'}})}}<div class="input-group-addon">w</div></div>
                <div class="input-group">{{ form_widget(form.pGaDays, {'id': 'P_GA_days','attr':{'style':'width:45px' }})}}<div class="input-group-addon">d</div></div></div>
                  </div>
                  <div class='col-xs-5'>
                    <div class='col-xs-5'>{{ form_label(form.pBirthWeight, 'Birth weight', {'label_attr':{'style':'margin-top:3px'}})}}</div>
                    <div class='col-xs-2'><div class="input-group">{{ form_widget(form.pBirthWeight, {'attr':{'style':'width:60px'}})}}<div class="input-group-addon">g</div></div></div>
                  </div>
                </div> 
            </div>

            <div class="row" style='margin-bottom: 5px'>
                <div style='white-space:nowrap'>
                  <div class='col-xs-5'>
                    <div class='col-xs-5'>{{ form_label(form.pSex, 'Sex/Gender', {'label_attr':{'style':'margin-top:3px'}})}}</div>
                    <div class='col-xs-2' style='margin-top: 5px'>{{ form_widget(form.pSex, {'attr':{'style':'text-indent:10px'}})}}</div>
                  </div>
                  <div class='col-xs-5'>
                    <div class='col-xs-5'>{{ form_label(form.pNumberOfInfants, 'No. of infants in birth', {'label_attr':{'style':'margin-top:5px'}})}}</div>
                    <div class='col-xs-2'>{{ form_widget(form.pNumberOfInfants, {'attr':{'style':'width:35px'}})}}</div>
                  </div>
                </div> 
            </div>

            <div class="row" style='margin-bottom: 5px'>
                <div style='white-space: nowrap'>
                  <div class='col-xs-5'>
                   {#<div class='col-xs-5'>{{ form_label(form.pEpoStudy, 'Child in EPO study')}}</div>#}
                   {# <div class='col-xs-2'>{{ form_widget(form.pEpoStudy, {'attr':{'style':'text-indent:10px'}})}}</div>#}
                  </div>
                  <div class='col-xs-5'>
                    <div class='col-xs-5'>{{ form_label(form.pRank, 'Rank (if >1)', {'label_attr':{'style':'margin-top:5px'}})}}</div>
                    <div class='col-xs-2'>{{ form_widget(form.pRank, {'attr':{'style':'width:35px'}})}}</div>
                    <div style='padding-right: 80px; margin-top: 8px'> (A/B/C/D..)</div>
                  </div>
                </div> 
                <div  style= 'padding-left:40px ; margin-top:50px;'> (<font color=red>*</font> = input necessary)</div>
                {% if isNew == true %}
                  <div class='col-xs-2'>{{ form_widget(form.submit_new, {'attr':{'style':'width:160px ; color:red; margin-left:900px'}})}}</div>      
                {% else %} 
                <div style="padding-left:1000px"> <a class='btn btn-default btnNext'><font color=red>Continue »</font></a></div>
                {% endif %}       
            </div>   

{% endblock %}
下面是methode stroeDataAPatient:

private function storeDataAPatient($dataA){

   if (!$dataA) {
        throw $this->createNotFoundException('Unable to find DataEFu2 entity.');
    }

    $em = $this->getDoctrine()->getManager();
    $session = $this->getRequest()->getSession();
    $em->persist($dataA);
    $em->flush();
    $tmp_pKpPatientid= $dataA->getpKpPatientid();

}
private function storeDataAPatient($dataA){

   if (!$dataA) {
        throw $this->createNotFoundException('Unable to find DataEFu2 entity.');
    }

    $em = $this->getDoctrine()->getManager();
    $session = $this->getRequest()->getSession();
    $em->persist($dataA);
    $em->flush();
    $tmp_pKpPatientid= $dataA->getpKpPatientid();

}
StoreDataaClient:

private function storeDataAPatient($dataA){

   if (!$dataA) {
        throw $this->createNotFoundException('Unable to find DataEFu2 entity.');
    }

    $em = $this->getDoctrine()->getManager();
    $session = $this->getRequest()->getSession();
    $em->persist($dataA);
    $em->flush();
    $tmp_pKpPatientid= $dataA->getpKpPatientid();

}
private function storeDataAPatient($dataA){

   if (!$dataA) {
        throw $this->createNotFoundException('Unable to find DataEFu2 entity.');
    }

    $em = $this->getDoctrine()->getManager();
    $session = $this->getRequest()->getSession();
    $em->persist($dataA);
    $em->flush();
    $tmp_pKpPatientid= $dataA->getpKpPatientid();

}

我可以告诉你,你可以把你的条件,如果有效和提交,为更好的表现:

 $form = $this->createForm(yourPathType, yourObject);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();
        }

是一个简短的代码

能否请您展示一下
DataLiveBundle:dataapient:form.html.twig
createNewForm
方法的代码。如果
dump($appendForm->getData()),将显示什么?您的
pGaWeeks
是否为空?错误(“pGaWeeks不应该是空的”)没有显示,因为我想您没有渲染它…什么是
storedataapacient
做的?“pGaWeeks”总是空的,是的。“storedataapacient”将用户输入保存在数据库中。我不完全确定,但我认为
storedataapacient()
将刷新。你的答案是猜测。您没有解释对原始代码所做的更改,甚至没有重命名变量(
$form
->
$appendForm
),请参见更新后的问题。你的答案不能回答这个问题。