=====================================================
在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属性值则变为>$sf_params->get('message') ?></li>
</ul>
相关文章: