정상적으로 해결할 수있는 솔루션입니다. 위에서 제시 한 샘 (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에 게시했습니다.
'$ form-> get ('email') -> setMessage ('틀린 전자 메일 친구!') '이외의 메시지를 설정해야하는 경우 ~에'' ' – Sam
감사 샘. 보기 도우미가 여전히 빈 메시지에 대한 마크 업을 생성하지 않습니까? 여기에 기본 'li'요소를 스타일 지정하고 있습니다. 나는 사용자 정의 formElementErrors 뷰 도우미를 작성할 수 있다고 생각합니다. – MrNorm
예, 두 번째 옵션을 사용하면 마크 업이 생성됩니다. 하지만 컨트롤러에서'setMessage()'를하면 커스텀 메시지 만 보여 질 것입니다 (iirc) – Sam