Symfony 添加和处理自定义表单字段
我使用 Symfony 和 propel 来生成一个名为 BaseMeetingMeetingsForm 的表单。
在 MeetingMeetingsForm.class.php 中,我有以下配置方法:
public function configure() {
$this->useFields(array('name', 'group_id', 'location', 'start', 'length'));
$this->widgetSchema['invited'] = new myWidgetFormTokenAutocompleter(array("url"=>"/user/json"));
}
在 MeetingMeetings.php 中,我的保存方法很简单:
public function save(PropelPDO $con = null) {
$this->setOwnerId(Meeting::getUserId());
return parent::save($con);
}
但是 propel 不知道我的自定义字段,因此不会对其执行任何操作。我在哪里以及如何放置一个可以处理此表单字段的特殊部分,请注意,这不仅仅是简单的保存到数据库,我需要在输入之前专门处理输入。
感谢您的时间和建议,
I am using Symfony with propel to generate a form called BaseMeetingMeetingsForm.
In MeetingMeetingsForm.class.php I have the following configure method:
public function configure() {
$this->useFields(array('name', 'group_id', 'location', 'start', 'length'));
$this->widgetSchema['invited'] = new myWidgetFormTokenAutocompleter(array("url"=>"/user/json"));
}
In MeetingMeetings.php my save method is simply:
public function save(PropelPDO $con = null) {
$this->setOwnerId(Meeting::getUserId());
return parent::save($con);
}
However propel doesn't know about my custom field and as such doesn't do anything with it. Where and how to I put in a special section that can deal with this form field, please be aware it is not just a simple save to database, I need to deal with the input specially before it is input.
Thanks for your time and advice,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须定义一个验证器(和/或创建您自己的验证器)。验证器
clean()
方法返回需要持久化的值。在 Doctrine 中(我不知道 Propel),表单然后调用表单上的
doUpdateObject()
,该表单又调用模型上的fromArray($arr)
函数。因此,如果它已经是您模型上的属性,您只需要创建验证器。如果它是一个更复杂的小部件,您需要向表单添加一些逻辑。
You have to define a validator (and/or create your own). The validator
clean()
method returns the value that needs to be persisted.In Doctrine (I don't know Propel) the form then calls the
doUpdateObject()
on the form, which in turns calls thefromArray($arr)
function on the model.So if it's already a property on your model you'll only need to create the validator. If it's a more complex widget, you'll need to add some logic to the form.