symfony表單將基本的用法定義在sfForm裡面
例如
public function executeContact($request)
{
$this->form = new sfForm();
$this->form->setWidgets(array(
'name' => new sfWidgetFormInputText(),
'email' => new sfWidgetFormInputText(array('default' => 'me@example.com')),
'subject' => new sfWidgetFormChoice(array('choices' => array('Subject A', 'Subject B', 'Subject C'))),
'message' => new sfWidgetFormTextarea(),
));
}
除了一次大量設定外 也可以一個一個設定
// Text input
$form->setWidget('full_name', new sfWidgetFormInput(array('default' => 'John Doe')));
<label for="full_name">Full Name</label>
<input type="text" name="full_name" id="full_name" value="John Doe" />
// Textarea
$form->setWidget('address', new sfWidgetFormTextarea(array('default' => 'Enter your address here'), array('cols' => 20, 'rows' => 5)));
<label for="address">Address</label>
<textarea name="address" id="address" cols="20" rows="5">Enter your address here</textarea>
// Password input
// Note that 'password' type widgets don't take a 'default' parameter for security reasons
$form->setWidget('pwd', new sfWidgetFormInputPassword());
<label for="pwd">Pwd</label>
<input type="password" name="pwd" id="pwd" />
// Hidden input
$form->setWidget('id', new sfWidgetFormInputHidden(array('default' => 1234)));
<input type="hidden" name="id" id="id" value="1234" />
// Checkbox
$form->setWidget('single', new sfWidgetFormInputCheckbox(array('value_attribute_value' => 'single', 'default' => true)));
<label for="single">Single</label>
<input type="checkbox" name="single" id="single" value="true" checked="checked" />
叫出form的方法
render() (for widget)
renderLabel()
renderHelp
renderError
renderRow()
設定選項按鍵
multiple=flase(default) multiple=true
expanded=false(default) 下拉列表(<select>) 可多选下拉列表
expanded=true 一组单选按钮 一组多选按钮
設定時間選項
// Date
$years = range(1950, 1990);
$form->setWidget('dob', new sfWidgetFormDate(array(
'label' => 'Date of birth',
'default' => '01/01/1950', // can be a timestamp or a string understandable by strtotime()
'years' => array_combine($years, $years)
)));
// symfony renders the widget in HTML as
<label for="dob">Date of birth</label>
<select id="dob_month" name="dob[month]">
<option value=""/>
<option selected="selected" value="1">01</option>
<option value="2">02</option>
...
<option value="12">12</option>
</select> /
<select id="dob_day" name="dob[day]">
<option value=""/>
<option selected="selected" value="1">01</option>
<option value="2">02</option>
...
<option value="31">31</option>
</select> /
<select id="dob_year" name="dob[year]">
<option value=""/>
<option selected="selected" value="1950">1950</option>
<option value="1951">1951</option>
...
<option value="1990">1990</option>
</select>
// Time
$form->setWidget('start', new sfWidgetFormTime(array('default' => '12:00')));
// symfony renders the widget in HTML as
<label for="start">Start</label>
<select id="start_hour" name="start[hour]">
<option value=""/>
<option value="0">00</option>
...
<option selected="selected" value="12">12</option>
...
<option value="23">23</option>
</select> :
<select id="start_minute" name="start[minute]">
<option value=""/>
<option selected="selected" value="0">00</option>
<option value="1">01</option>
...
<option value="59">59</option>
</select>
// Date and time
$form->setWidget('end', new sfWidgetFormDateTime(array('default' => '01/01/2008 12:00')));
// symfony为月,日,年,时,分来显示五个下拉列表。
其中 array_combine($a,$b)代表將$a的鍵與#b的值結合成新array
<?php
$a = array('green','red','yellow');
$b = array('avocado','apple','banana');
$c = array_combine($a, $b);
print_r($c);
/* Outputs:
Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)
*/
?>
当然,你可以定义日期格式,使用欧洲标准代替国际标准
(%day%/%month%/%year% 代替 %month%/%day%/%year%)
表单数据验证的处理
事实上,除了仅从用户的输入中取得值表单处理还有很多的事情要做。对于大多数的表单提交来说,应用程序控制器需要作以下几件事:- 1.检查数据是否符合一组预先定义好的规则(必添的字段,email的格式等)
- 2.有选择性地转换一些输入数据从而使之更容易理解(消除空格,转换成PHP格式的日期等)
- 3.如果数据是无效的,重新显示带有错误信息的表单
- 4.如果数据是正确的,做一些处理并跳转到另一个动作
訂製驗證器
$this->form->setValidators(array(
'name' => new sfValidatorString(),
'email' => new sfValidatorEmail(array('required' => false)),
'subject' => new sfValidatorString(),
'message' => new sfValidatorString(array('min_length' => 4))
));
使用
sfValidatorAnd來使用多個驗證器
$this->form->setValidators(array(
'name' => new sfValidatorString(),
'email' => new sfValidatorAnd(array(
new sfValidatorEmail(),
new sfValidatorString(array('min_length' => 4)),
), array('required' => false)),
'subject' => new sfValidatorString(),
'message' => new sfValidatorString(array('min_length' => 4))
));
數個字段使用同一個驗證器
// in modules/foo/actions/actions.class.php
// 定义表单
$this->form = new sfForm();
$this->form->setWidgets(array(
'login' => new sfWidgetFormInputText(),
'password1' => new sfWidgetFormInputText(),
'password2' => new sfWidgetFormInputText()
);
$this->form->setValidators(array(
'login' => new sfValidatorString(), // login is required
'password1' => new sfValidatorString(), // password1 is required
'password2' => new sfValidatorString(), // password2 is required
));
$this->form->setPostValidators(new sfValidatorSchemaCompare('password1', '==', 'password2'));
底下列舉一些常用驗證器
// 字符串验证器
$form->setValidator('message', new sfValidatorString(array(
'min_length' => 4,
'max_length' => 50,
),
array(
'min_length' => 'Please post a longer message',
'max_length' => 'Please be less verbose',
)));
// 数值验证器
$form->setValidator('age', new sfValidatorNumber(array( // 如果你想验证整型值可使用'sfValidatorInteger'来代替。
'min' => 18,
'max' => 99.99,
),
array(
'min' => 'You must be 18 or more to use this service',
'max' => 'Are you kidding me? People over 30 can\'t even use the Internet',
)));
//邮件地址验证器
$form->setValidator('email', new sfValidatorEmail());
// URL 验证器
$form->setValidator('website', new sfValidatorUrl());
//正则表达式验证器
$form->setValidator('IP', new sfValidatorRegex(array(
'pattern' => '^[0-9]{3}\.[0-9]{3}\.[0-9]{2}\.[0-9]{3}$'
)));
沒有留言:
張貼留言