簡體   English   中英

Zend框架表單裝飾器

[英]Zend framework form Decorators

我正在嘗試使用裝飾器獲得以下布局:

    <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'));           

如何將標簽放置在標簽上方?

謝謝!

我准備了一個示例,該示例應有助於解決您的問題。 這里是:

    $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注入表單的任何函數。 在此示例中,我做了一個匿名函數,並將其分配給$makeImg變量(為此,您需要PHP 5.3,但在較早版本的PHP中,您也可以這樣做,但可以使用例如create_function函數)。 這個$makeImg變量是我的回調。 可以看出,該函數會生成您的img html標簽。

希望這對您有幫助。

您可以使用表單裝飾器

在此處添加元素集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'))));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM