可以通过mergeForm()方法将一个表单与另一个表单合并,或用embedForm()方法进行表单嵌套:
$this->mergeForm(new AnotherForm());
$this->embedForm('name', new AnotherForm());
2. 取消表单显示字段:
默认情况下,Propel表单显示的字段对应全部数据表字段,我们要清除(unset)其中不可以由终端用户编辑的字段:ex
class JobeetJobForm extends BaseJobeetJobForm
{
public function configure()
{
unset( $this['created_at'], $this['updated_at'], $this['expires_at'], $this['is_activated']
); }
}
3. sfWidgetFormChoice控件
sfWidgetFormChoice表示一个选择控件,它可以根据配置选项(通过expanded 和 multiple参数不同搭配)的不同,被显示为不同的控件:
* 生成下拉列表<select> array(’multiple’ => false, ‘expanded’ => false)
* 生成下拉框<select multiple=”multiple”> array(’multiple’ => true, ‘expanded’ => false)
* 生成单选框<input type=radio> array(’multiple’ => false, ‘expanded’ => true)
* 生成复选框<input type=checkbox> array(’multiple’ => true, ‘expanded’ => true)
4. 自定义表单样式
<?php echo $form ?>默认会以表格形式显示表单。
多少情况下,你可能需要自己定制表单布局。表单对象为定制提供许多有用的方法:
Method | Description |
---|---|
render() | 显示表单(相当于echo $form) |
renderHiddenFields() | 显示隐藏的字段 |
hasErrors() | 如果表单有错误,返回true |
hasGlobalErrors() | 如果标有全局错误,返回true |
getGlobalErrors() | 返回全局错误数组 |
renderGlobalErrors() | 显示全局错误 |
表单对象就像一个字段数组。你可以用$form['company']访问company字段,返回的对象提供显示这个字段每一个元素的方法:
Method | Description |
---|---|
renderRow() | 显示表单域行。(包括label,error,field tag等全部) |
render() | 显示字段控件 |
renderLabel() | 显示字段标签 |
renderError() | 如果有子段错误则显示 |
renderHelp() | 显示字段帮助信息 |
echo $form语句相当于:
<?php foreach ($form as $widget): ?> <?php echo $widget->renderRow() ?> <?php endforeach(); ?>