2011-03-23 1 views

답변

2

나에 대해 단지 있었다 이 문제에 대해 제 자신의 해결책을 가지고 물어보십시오. 지역 사회를 위해 말하자면, 여러분이 제안에서 팝업에 질문하는 것을 보았을 때 말입니다.

내 간단하고 예쁜 우아한 솔루션은, 나 자신이라고 말하면 간단한 사용자 정의 데코레이터를 사용합니다. 수신하는 내용에는 아무런 영향을주지 않지만 요소를 변경합니다.

class App_Form_Decorator_ErrorClass 
    extends Zend_Form_Decorator_Abstract 
{ 

    protected $_placement = null; 

    protected $_options = array(
     'class' => 'error' 
    ); 

    public function render($content) 
    { 
     $element = $this->getElement(); 

     if($element->hasErrors()) 
     { 
      $errorClass = $this->getOption('class'); 
      $currentClass = $element->getAttrib('class'); 
      $element->setAttrib('class', (!empty($currentClass) ? $currentClass . ' ' . $errorClass : $errorClass)); 
     } 

     return $content; 
    } 
} 

사용법 :
당신이 전에 ViewHelper의 장식, 그리고 세트를 장식 를 추가하기 만하면됩니다.

public function init() 
{ 
    $elementDecorators = array(
     'ErrorClass', 
     'ViewHelper', 
     // etc.. 
    ); 

    // or: 
    $elementDecorators = array(
     array(
      'ErrorClass', 
      array('class' => 'custom-class') // defaults to 'error' 
     ), 
     'ViewHelper', 
     // etc.. 
    ); 

    // then just add the decorators to an element the way you usually do, for instance like so: 
    $someElement = new Zend_Form_Element_Text('someElement'); 
    $someElement->setDecorators($elementDecorators); 

    // etc... 

O, PS는 양식에 올바른 접두사 경로를 추가해야합니다 .: :

$this->addPrefixPath('App_Form', 'App/Form'); // or your own namespace 
+0

위대한 답변 fireeyedboy 주셔서 감사합니다. Abstract 클래스 대신 Decorator_ViewHelper를 확장하여 조금 다른 방식으로 구현했습니다. 이 방법은 해당하는 prefixPath가있는 모든 양식의 표준입니다. –

0

가장 간단한 방법은 내가이 해당 양식의 isValid() 메소드를 확장되었다 할 발견 사용중인 클래스. 라이브러리의 My_Standard_Form 클래스로 설정하는 것이 좋습니다. 솔루션에 대한

public function isValid($data) 
{ 
    $valid = parent::isValid($data); 

    foreach ($this->getElements() as $element) { 
     if ($element->hasErrors()) { 
      $oldClass = $element->getAttrib('class'); 
      if (!empty($oldClass)) { 
       $element->setAttrib('class', $oldClass . ' error'); 
      } else { 
       $element->setAttrib('class', 'error'); 
      } 
     } 
    } 

    return $valid; 
} 

크레딧은 웹 사이트 요인 블로그 (http://www.websitefactors.co.uk/zend-framework/2011/06/error-class-on-form-field-errors-using-zend-form/)로 이동합니다.

관련 문제