=====================================================
在plugins/op插件名Plugin/lib/form/doctrine/目录下创建一个 VoteForm.class.php类文件,VoteForm名随意起
类文件中的'VoteForm'类必须继承'BaseForm'类而且在'configure()'方法里增加表单元素,增加表单元素的格式如下
<?php
class VoteForm extends BaseForm
{
public function configure()
{
$subjects = array('A'=>'Subject A', 'B'=>'Subject B', 'C'=>'Subject C');//=========用于select表单元素'A','B','C',将变成选项option的value属性值,如果数组不指定键值则默认是0,1,2...
//'Subject A','Subject B','Subject C'将变成option标签间要显示的内容
$this->setWidgets(array(//只能用一次,用此方法之前用setWidget与setWidgets创建的表单元素都将被此替代,但可以在下面用setWidget继续添加表单元素
'name' => new sfWidgetFormInputText(),//==============相应生成的表单元素前面的标题名和name,id属性值也为'name',但是开头用数字命名生成的表单元素id属性值会去掉以数字开头部分
//此创建表单元素时直接设定属性值,优先级大于$this->widgetSchema->setNameFormat('vote[%s]');
'email' => new sfWidgetFormInputText(array(), array('name'=>'emailuu', 'class' => '9emailuu', 'id' => '9emailuu', 'value' => 'Your Email Here')),
'subject' => new sfWidgetFormSelect(array('choices' => $subjects)),
'message' => new sfWidgetFormTextarea(),
));
//============也可通过下面两种方式设定指定表单元素的value属性默认值
//$this->setDefault('name', 'Your name Here');
//$this->setDefaults(array('name' => 'Your name Here', 'subject' => 'C'));//select则直接赋默认数组键值即可指定默认选项
/*
$this->setWidgets()也相当于new sfWidgetFormSchema()
$this->setWidgets(array(
'name' => new sfWidgetFormInputText(),
'email' => new sfWidgetFormInputText(array(), array('name'=>'emailuu', 'class' => '9emailuu', 'id' => '9emailuu', 'value' => 'Your Email Here')),
'subject' => new sfWidgetFormSelect(array('choices' => $subjects)),
'message' => new sfWidgetFormTextarea(),
));//和下等同
$this->widgetSchema = new sfWidgetFormSchema(array(
'name' => new sfWidgetFormInputText(),
'email' => new sfWidgetFormInputText(array(), array('name'=>'emailuu', 'class' => '9emailuu', 'id' => '9emailuu', 'value' => 'Your Email Here')),
'subject' => new sfWidgetFormSelect(array('choices' => $subjects)),
'message' => new sfWidgetFormTextarea(),
));//也和下等同
$this->setWidgetSchema(new sfWidgetFormSchema(array(
'name' => new sfWidgetFormInputText(),
'email' => new sfWidgetFormInputText(array(), array('name'=>'emailuu', 'class' => '9emailuu', 'id' => '9emailuu', 'value' => 'Your Email Here')),
'subject' => new sfWidgetFormSelect(array('choices' => $subjects)),
'message' => new sfWidgetFormTextarea(),
)));
*/
$this->setWidget(//可以用多次,单独创建一个表单元素
'55first_name1' , new sfWidgetFormInputText()
);
//==============只是修改对应表单元素前面的标题名,不涉及表单元素的name,id属性,属性值仍是name
$this->widgetSchema->setLabels(array(
'name' => 'Your name',
'email' => 'Your email address',
'subject' => 'Your subject',
'message' => 'Your message',
));
//===============单个修改某个表单元素前面标题
$this->widgetSchema->setLabel('55first_name1', '1Your email address1');
//将表单元素按列表方式显示
//$this->widgetSchema->setFormFormatterName('list');
//加上此句在前台生成的表单元素的name属性值变成name="vote[name]",name="vote[email]"。id属性值则变为id="vote_name",id="vote_email"。'vote'名是随意定的
//$this->widgetSchema->setNameFormat('vote[%s]');
}
}
?>
=====================================================则在任意动作里可以直接实例化
<?php
$this->form = new VoteForm();
//$this->form = new VoteForm(array('name' => 'Your name Here', 'subject' => 'C'));也可以通过这样创建一个表单组建并给相应表单元素赋默认值
?>
=====================================================前台页面则如下即可生成一个表单
<form action="<?php echo url_for('vote/submit') ?>" method="POST">
<table>
<?php echo $form ?>
<tr>
<td colspan="2">
<input type="submit" />
</td>
</tr>
</table>
</form>
==============也可通过下面方式依次输出创建的每个表单元素灵活性高
<table>
<form action="<?php echo url_for('vote/submit') ?>" method="POST">
<?php foreach ($form as $name => $field): ?><!--$name为创建表单元素时的标记名,不一定是表单元素name属性值-->
<?php if ($field->isHidden()) continue; ?><!--如果该表单元素是隐藏的则跳过此次循环-->
<tr>
<td><?php echo $field->renderLabel(); ?>:</td><td><?php echo $field->render(); ?></td>
</tr>
<?php endforeach; ?>
<?php //echo $form->renderHiddenFields();//如果表单提交不了则输出此语句 ?>
<tr><td colspan="2"><input type="submit" /></td></tr>
</form>
</table>
=====================================================像重定向的前台页面一次传递多个元素
<?php
public function executeSubmit($request)
{
$this->forward404Unless($request->isMethod('post'));
if($request->isMethod('post')){
$params = array(
'name' => $request->getParameter('name'),
'email' => $request->getParameter('email'),
'message' => $request->getParameter('message'),
);
$this->redirect('vote/thankYou?'.http_build_query($params));//可以使用http_build_query()把一个数组变成'键值=元素值&键值=元素值...'的字符串形式
}
}
public function executeThankyou()
{
}
?>
thankYou?name=asdgf&email=sdg&message=asdfg
//thankyouSuccess.php,被重定向的前台页面则可通过$sf_params->get('数组键值')来获取传递过来相应的值
<ul>
<li>Name: <?php echo $sf_params->get('name') ?></li>
<li>Email: <?php echo $sf_params->get('email') ?></li>
<li>Message: <?php echo $sf_params->get('message') ?></li>
</ul>