2012-03-26 3 views
2

내가에서 발견 사용자 지정 양식 데코레이터 사용하고 있습니다 : 파일 (라인 70)의 하단에 http://code.google.com/p/digitalus-cms/source/browse/trunk/library/Digitalus/Form/Decorator/Composite.php?r=767젠드 프레임 워크 클래스 정의에 양식 데코레이터가

것은 :

$output = '<div class="form_element">' 
       . $label 
       . $input 
       . $errors 
       . $desc 
       . '</div>'; 

나는를하고 싶다 DIV 클래스 동적 및 컨트롤러에서 요소를 만들 때 전달됩니다. 내가 사용하는 모든 내장 ZEND 함수는 LABEL 또는 INPUT 만 수정합니다. 다음은 요소 작성의 예입니다.

$decorator = new Composite(); 

     $this->addElement('text', 'start', array(
      'label'  => 'Start Number', 
      'required' => true, 
      'filters' => array('StringTrim'), 
      'validators' => array(
       'alnum', 
      ), 
      'decorators' => array($decorator) 
     )); 

모든 아이디어는 대단히 감사하겠습니다. 시간을내어 주셔서 감사합니다!

답변

2

그냥 렌더링() 메소드 수정이 현재의 장식을 변경할 수 있습니다 경우 모든 CSS 클래스가 하드 코딩하는 이유를 지금 확인하십시오

class Digitalus_Form_Decorator_Composite 
{ 
    /* ... */ 
    public function render($content) 
    { 
     $element = $this->getElement(); 
     if (!$element instanceof Zend_Form_Element) { 
      return $content; 
     } 
     if (null === $element->getView()) { 
      return $content; 
     } 

     $separator = $this->getSeparator(); 
     $placement = $this->getPlacement(); 
     $label  = $this->buildLabel(); 
     $input  = $this->buildInput(); 
     $errors = $this->buildErrors(); 
     $desc  = $this->buildDescription(); 

     $output = '<div class="'.$this->getOption('class').'">' 
       . $label 
       . $input 
       . $errors 
       . $desc 
       . '</div>'; 

     switch ($placement) { 
      case (self::PREPEND): 
       return $output . $separator . $content; 
      case (self::APPEND): 
      default: 
       return $content . $separator . $output; 
     } 
    } 
    /* ... */ 
} 

그리고 요소 생성시 :

$element->setDecorators(array(
    /* ... */ 
    array(array('div'=>'Composite'), array('class' => 'my_class_name')) 
    /* ... */ 
))); 

하는 경우를 기존의 데코레이터를 편집하고 싶지 않고 render() 메소드를 오버라이드하면된다. ...

+0

+1 멋진 답변과 나에게 이길 수있다 :) 나는 getOptions '그리고 생성자. – drew010

+0

덕분에, 대개 zf를 도와 주었고, 여기는 더 조용합니다 :) –

+0

위대한 - 매력처럼 일했습니다! 내 양식을 초기화하는 동안 addPrefixPath를 정의하면 모든 것이 훌륭하게 작동했습니다. 정말 고마워. – user1199981