2011-03-11 82 views
2

我试图让使用装饰布局如下:Zend框架的形式装饰

<form action="/index/login" method="post" id="login_form">    
     <div class="input_row"> 
      <img src="/images/user_icon.png" class="login_icon" alt=""/>     
      <label for="username" class="login_label">Username:</label>     
      <input type="text" name="username" value="" id="username" class="login_input" />        
     </div> 
     <div class="input_row"> 
      <img src="/images/password_icon.png" class="login_icon" alt=""/> 
      <label for="password" class="login_label">Password:</label>     
      <input type="password" name="password" value="" id="password" class="login_input" />      
     </div> 
     <div class="input_row"> 
      <input type="submit" name="login_submit" value="Login" class="login_submit" /> 
     </div> 
    </form> 

我有这个至今:

$form = new Zend_Form; 

    $form->setAction('/index/login') 
     ->setMethod('post') 
     ->setAttrib('id', 'login_form'); 

    $username = $form->createElement('text', 'username'); 
    $username->addValidator('alnum') 
      ->setRequired(TRUE) 
      ->setLabel('Username') 
      ->setAttrib('class', 'login_input'); 


    $username->setDecorators(array(
      'ViewHelper', 
      'Errors', 
      array('Label',array('class' => 'login_label')), 
      array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'input_row') 
     )); 


    $form->addElement($username) 
     ->addElement('submit', 'login', array('label' => 'Login'));   

我怎样在标签上面的标签?

谢谢!

+0

您当前的输出是什么?好吧,你已经发布了你的预期输出 – emaillenin 2011-03-11 18:45:09

回答

2

我准备这应该是解决你的问题有帮助的例子。那就是:

$form = new Zend_Form; 

    $form->removeDecorator('htmlTag'); 

    $form->setAction('/index/login') 
      ->setMethod('post') 
      ->setAttrib('id', 'login_form'); 

    $username = $form->createElement('text', 'username'); 

    $username->addValidator('alnum') 
      ->setRequired(TRUE) 
      ->setLabel('Username') 
      ->setAttrib('class', 'login_input'); 


    // anonymous function that will generate your image tag 
    $makeImg = function($content, $element, array $options) { 
       return '<img src="/images/' . $options['img'] . '" class="' . $options['class'] . ' " alt=""/> '; 
      }; 


    $username->setDecorators(array(
     'ViewHelper', 
     'Errors', 
     array('Label', array('class' => 'login_label')), 
     array('Callback', 
      array(
       'callback' => $makeImg, 
       'img' => 'user_icon.png', 
       'class' => 'login_icon',      
       'placement' => 'PREPEND' 
      ) 
     ), 
     array('HtmlTag', array('tag' => null, 'class' => 'input_row')), 
    )); 

    $form->addElement($username); 


    $submit = $form->createElement('submit', 'login', array(
       'label' => 'Login', 
       'class' => 'login_submit' 
        ) 
    ); 


    $submit->setDecorators(array(
     'ViewHelper', 
     'Errors', 
     array('HtmlTag', array('tag' => null, 'class' => 'input_row')), 
    )); 

    $form->addElement($submit); 

形式生成下面的HTML(我没有生成的密码字段作为您的Zend_Form的代码不包含它):

<form id="login_form" enctype="application/x-www-form-urlencoded" action="/index/login" method="post"> 
     <div class="input_row"> 
     <img src="/images/user_icon.png" class="login_icon " alt=""> 
     <label for="username" class="login_label required">Username</label> 
     <input type="text" name="username" id="username" value="" class="login_input"> 
     </div> 
     <div class="input_row"> 
     <input type="submit" name="login" id="login" value="Login" class="login_submit"> 
     </div> 
    </form> 

值得一提的是,我用Callback装饰。有了这个装饰器,你可以调用任何可以用来将自定义html注入到表单中的函数。在这个例子中,我创建了一个anonymous函数,我将其分配给$makeImg变量(为此,您需要PHP 5.3,但在旧版本的PHP中,您也可以这样做,但使用例如create_function函数)。这$makeImg变量是我的回调。可以看出该函数生成您的img html标签。

希望这会对你有所帮助。

+0

我正在运行一个老版本的PHP和zend框架1.5.3。从你的例子我得到这个: $ makeImg = create_function('$ content,$ element,array $ options','return“\"Image\"”'); – Michael 2011-03-14 10:02:45

+0

,但我得到一个错误:Warning:substr_compare()[function.substr-compare]:起始位置不能超过Zend/Form.php在2207 – Michael 2011-03-14 10:11:54

+0

@Michael上的初始字符串长度。我有ZF 1.11。所以如果你使用1.5这可能是一个问题。不幸的是,我不知道如何使它在ZF 1.5中工作。 – Marcin 2011-03-14 10:35:55

0

您可以使用表单装饰

添加元素集setDecorators这里之后,你婉告诉你可以写HTML。

$this->addElements(array( 
      $id, 
      $group_id, 
      $content_name, 
      $title, 
      $content, 
      $tags, 
      $status, 
      $Publish 
     )); 

     $this->setDecorators(array(array('viewScript', array('viewScript' => 'admin/articleFormDecorator.phtml'))));