问题描述
< form action =/ index / login方法我试图使用装饰器得到以下布局: =postid =login_form>
< div class =input_row>
< img src =/ images / user_icon.pngclass =login_iconalt =/>
< label for =usernameclass =login_label>用户名:< / label>
< input type =textname =usernamevalue =id =usernameclass =login_input/>
< / div>
< div class =input_row>
< img src =/ images / password_icon.pngclass =login_iconalt =/>
< label for =passwordclass =login_label>密码:< / label>
< input type =passwordname =passwordvalue =id =passwordclass =login_input/>
< / div>
< div class =input_row>
< input type =submitname =login_submitvalue =Loginclass =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');
$ b $ username-> setDecorators(array(
'ViewHelper',$ b $'Errors',
array('Label',array(' class'=>'login_label')),
array('row'=>'HtmlTag'),array('tag'=>'div','class'=>'input_row')
));
$ b $ 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');
//匿名函数将生成图片标记
$ makeImg = function($ content,$ element,array $ options){
return'< img src =/ images /'。$ options ['img']。'class ='。$ options ['class']。'alt =/> ;
};
$ b $ username-> setDecorators(array(
'ViewHelper',$ b $'Errors',
array('Label',array(' (''Callback',
array(
'callback'=> $ makeImg,
'img'=>'login_label')),
array 'user_icon.png',
'class'=>'login_icon',
'placement'=>'PREPEND'
)
),
array( 'HtmlTag',array('tag'=> null,'class'=>'input_row')),
));
$ form-> addElement($ username);
$ b $ submit = $ form-> createElement('submit','login',array(
'label'=>'Login',
'class'=>'login_submit'
)
);
$ b $ submit-> setDecorators(array($ b $'ViewHelper',$ b $'Errors',
array('HtmlTag',array(' tag'=> null,'class'=>'input_row')),
));
$ form-> addElement($ submit);
表单生成以下html(我没有生成密码字段,因为您的Zend_Form代码没有包含它):
< form id =login_formenctype =application / x-www-form-urlencodedaction = / index / loginmethod =post>
< div class =input_row>
< img src =/ images / user_icon.pngclass =login_iconalt =>
< label for =usernameclass =login_label required>用户名< / label>
< input type =textname =usernameid =usernamevalue =class =login_input>
< / div>
< div class =input_row>
< input type =submitname =loginid =loginvalue =Loginclass =login_submit>
< / div>
< / form>
值得一提的是我使用了修饰器。有了这个装饰器,你可以调用任何可以用来将自定义html注入到表单中的函数。在这个例子中,我将函数分配给 $ makeImg
变量(为此您需要PHP 5.3,但在旧版本的PHP中,您也可以这样做,但使用例如create_function函数)。这个 $ makeImg
变量是我的回调函数。可以看出该函数生成了 img
html标记。
希望这对您有所帮助。
i am trying to get the following layout using 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>
I have got this so far:
$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'));
How do i place the tag above the label?
Thanks!
I prepared an example that should be helpful in solving your problem. Here it is:
$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);
The form generates the following html (I did not generated password field as your Zend_Form code does not contain it):
<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>
What's worth mentioning is that I used Callback decorator. With this decorator you can call any function that can be used to inject custom html into your forms. In this example I made an anonymous function that I assigned to $makeImg
variable (for this you need PHP 5.3 but in older versions of PHP you could do it also, but using e.g. create_function function). This $makeImg
variable is my callback. As can be seen the function generates your img
html tag.
Hope this will be helpful to you.
这篇关于Zend框架表单装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!