2010-02-09 4 views
1

현재 Zend_Form 대신 확장 할 간단한 사용자 정의 레이어를 작성하려고합니다. 예를 들어, My_Form.Zend_Form이 사용자 정의 레이아웃의 요소 기본값을 대체합니다.

모든 양식이 동일하게 보이기를 원하므로 My_Form에 설정하고 싶습니다. 여기까지입니다.

class My_Form extends Zend_Form 
{ 
    protected $_elementDecorators = array(
     'ViewHelper', 
     'Errors', 
     array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')), 
     array('Label', array('tag' => 'td')), 
     array(array('row' => 'HtmlTag'), array('tag' => 'tr')), 
    ); 
} 

그리고 모든 양식이이를 연장합니다. 이제이 작업은 문제가 $ _elementDecorators 배열과 함께 제공됩니다. 레이블을 "td"에 래핑 할 때 Label Decorator는 "td"에 기본 "id"를 적용하지만 그 "td"에도 클래스를 추가하려고합니다.

이 배열을 사용하여이 작업을 수행 할 수 있습니까? 그렇지 않은 경우이 작업을 수행하는 더 좋은 방법이 있습니까? 또는 그렇다면 누군가이 어레이가 어떻게 작동하는지 설명해주십시오.

원하는 결과 :

<tr> 
    <td class='label_cell'> 
     <label /> 
    </td> 
    <td class='value_cell'> 
     <input /> 
    </td> 
</tr> 

감사합니다.

답변

1

나는 그것이 최선이라고 확신하지는 않지만 해결책을 찾았습니다.

여기에서는 사용자 정의 데코레이터를 만들고 대신로드하는 방법을 결정했습니다.

/** 
* Overide the default, empty, array of element decorators. 
* This allows us to apply the same look globally 
* 
* @var array 
*/ 
protected $_elementDecorators = array(
    'ViewHelper', 
    'Errors', 
    array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')), 
    array('CustomLabel', array('tag' => 'td')), 
    array(array('row' => 'HtmlTag'), array('tag' => 'tr')) 
); 

/** 
* Prefix paths to use when creating elements 
* @var array 
*/ 
protected $_elementPrefixPaths = array(
    'decorator' => array('My_Form_Decorator' => 'My/Form/Decorator/') 
); 

실내 장식이가 잘 작동하지만이 작업을 수행 할 수있는 간단한 방법이 있는지

class My_Form_Decorator_CustomLabel extends Zend_Form_Decorator_Label 
{ 
    public function render($content) 
    { 
     //... 
     /** 
     * Line 48 was added for the cutom class on the <td> that surrounds the label 
     */ 
     if (null !== $tag) { 
      require_once 'Zend/Form/Decorator/HtmlTag.php'; 
      $decorator = new Zend_Form_Decorator_HtmlTag(); 
      $decorator->setOptions(array('tag' => $tag, 
             'id' => $this->getElement()->getName() . '-label', 
             'class' => 'label_cell')); 

      $label = $decorator->render($label); 
     } 
     //... 
    } 
} 

, 나는 아직도 궁금합니다.

아이디어가 있으십니까?

0
같은 문제를 처리 할 때 내가 사용 결국

빠른 해킹 :

class My_Form extends Zend_Form 
{ 
    protected $_elementDecorators = array(
     'ViewHelper', 
     'Errors', 
     array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'value_cell')), 
     array('Label', array('tag' => 'th')), 
     array(array('row' => 'HtmlTag'), array('tag' => 'tr')), 
    ); 
} 

차이점은`배열 ('라벨', 배열 ('태그'=> '')),

"label"열에 TH 요소가 있고 요소 열에 TD 요소가 있습니다.

그런 다음 원하는대로 스타일을 지정할 수 있습니다.

관련 문제