我正在使用Symfony PHP。我有一个复选框,根据它是否被选中,我想在表单中显示一个组合框。如果选中复选框,我希望显示带有所有选项的组合,但如果未选中复选框,我希望禁用组合,但是小部件在保存表单时必须具有默认值或硬编码值。我不知道该怎么做,因为当我将组合框设置为禁用时,表单将小部件保存为空,而我无法设置预定义的值。有人能帮我吗?
可以在bind()方法中修改表单提交的数据。如果您想根据用户选择/提交的内容更改表单的行为,请使用此选项。
如果您想在表单数据验证并准备持久化到数据库之后更改对象的更改方式,那么请查看updateObject()方法。
class myForm
{
public function configure()
{
// configuration
}
public function bind($taintedValues = array(), $taintedFiles = array())
{
// I can alter the behaviour of the form here, depending on the data submitted
if (isset($taintedValues['do_not_store_my_personal_details'])) {
// Change the value of a form field. If the form doesn't pass validation, the user will see any information entered into the phone_number fields has been deleted
$taintedValues['phone_number'] = null;
}
return parent::bind($taintedValues, $taintedFiles);
}
public function updateObject($values = null)
{
if ($values === null) {
$values = $this->getValues();
}
// Override the data stored with the object
if ($values['do_not_store_my_personal_details'] == true) {
$values['phone_number'] = null;
}
return parent::updateObject($values);
}