2013-06-06 3 views
1

실패한 양식 유효성 검사 메시지에 대해 ZF2가 하나의 오류 메시지 만 표시하는 것처럼 보이지 않습니다.ZF2 - 양식에 단 하나의 오류 만 표시

oli.meffff' is not a valid hostname for the email address 
The input appears to be a DNS hostname but cannot match TLD against known list 
The input appears to be a local network name but local network names are not allowed 

내가 좀 더 친화적 인 뭔가를 보여 오류를 무시할 수 있습니다 방법 : 사용자가 오타를 만든 경우

예를 들어, EmailAddress를 검사기는 다음과 같은 7 개의 메시지를 다시 전달 일반적으로 표시 할 수 있습니다 예 : "올바른 이메일 주소를 입력하십시오."

+0

'$ form-> get ('email') -> setMessage ('틀린 전자 메일 친구!') '이외의 메시지를 설정해야하는 경우 ~에'' ' – Sam

+0

감사 샘. 보기 도우미가 여전히 빈 메시지에 대한 마크 업을 생성하지 않습니까? 여기에 기본 'li'요소를 스타일 지정하고 있습니다. 나는 사용자 정의 formElementErrors 뷰 도우미를 작성할 수 있다고 생각합니다. – MrNorm

+0

예, 두 번째 옵션을 사용하면 마크 업이 생성됩니다. 하지만 컨트롤러에서'setMessage()'를하면 커스텀 메시지 만 보여 질 것입니다 (iirc) – Sam

답변

2

정상적으로 해결할 수있는 솔루션입니다. 위에서 제시 한 샘 (Sam)과 같이 모든 유효성 검사기 오류에 대해 오류와 동일한 문자열을 사용하는 대신 요소에 대한 InputFilter의 오류 메시지를 재정의 한 다음 사용자 지정 양식 오류보기 도우미를 사용하여 첫 번째 메시지 만 표시했습니다.

<?php 
namespace Application\Form\View\Helper; 

use Traversable; 
use \Zend\Form\ElementInterface; 
use \Zend\Form\Exception; 

class FormElementSingleErrors extends \Zend\Form\View\Helper\FormElementErrors 
{ 
    /** 
    * Render validation errors for the provided $element 
    * 
    * @param ElementInterface $element 
    * @param array $attributes 
    * @throws Exception\DomainException 
    * @return string 
    */ 
    public function render(ElementInterface $element, array $attributes = array()) 
    { 
     $messages = $element->getMessages(); 
     if (empty($messages)) { 
      return ''; 
     } 
     if (!is_array($messages) && !$messages instanceof Traversable) { 
      throw new Exception\DomainException(sprintf(
       '%s expects that $element->getMessages() will return an array or Traversable; received "%s"', 
       __METHOD__, 
       (is_object($messages) ? get_class($messages) : gettype($messages)) 
      )); 
     } 

     // We only want a single message 
     $messages = array(current($messages)); 

     // Prepare attributes for opening tag 
     $attributes = array_merge($this->attributes, $attributes); 
     $attributes = $this->createAttributesString($attributes); 
     if (!empty($attributes)) { 
      $attributes = ' ' . $attributes; 
     } 

     // Flatten message array 
     $escapeHtml  = $this->getEscapeHtmlHelper(); 
     $messagesToPrint = array(); 
     array_walk_recursive($messages, function ($item) use (&$messagesToPrint, $escapeHtml) { 
      $messagesToPrint[] = $escapeHtml($item); 
     }); 

     if (empty($messagesToPrint)) { 
      return ''; 
     } 

     // Generate markup 
     $markup = sprintf($this->getMessageOpenFormat(), $attributes); 
     $markup .= implode($this->getMessageSeparatorString(), $messagesToPrint); 
     $markup .= $this->getMessageCloseString(); 

     return $markup; 
    } 

} 

그것은이 포함 오버라이드 (override) 렌더링 기능을 FormElementErrors의 단지 확장입니다 :

// We only want a single message 
$messages = array(current($messages)); 

그때 솔루션 I를 사용하여 내 응용 프로그램에 도우미를 삽입 여기

는 도우미 내 호 here에 게시했습니다.

관련 문제