对象的表单参数
我接管了一个遗留网站,我的蜘蛛侠感觉代码气味对整个网站使用的表单参数变得疯狂。
例如,我们有一个表单,允许您添加新联系人,或根据 URL 中存在的 iContactId 编辑现有联系人。所以 cfm 文件顶部的代码是。
<cfparam name="form.name" default="">
<cfparam name="form.age" default="">
<cfparam name="form.surname" default="">
<--- More cfparams for every form field--->
<cfif isDefined("URL.iContactId")>
<cfset VARIABLES.contact = contactService.getContact("URL.iContactId")/>
<cfset FORM.name = contact.getName() />
<cfset FORM.age= contact.getAge() />
<cfset FORM.surname = contact.getSurname() />
</cfif>
因此,本质上我们将所有表单字段默认为空,然后如果 iContactId 位于 URL 中则填充它们。 cfm 文件底部的表单使用这些变量,如下所示。
<form>
<input name="name" value="#FORM.name#">
现在,对我来说,这些技术的问题是,此页面上有大约 30 个表单字段,因此我对所有表单字段进行参数化,然后如果 URL.iContactId 变量存在,则将 30 个表单字段设置为联系人对象值。我正在尝试想办法消除这种重复 - 也许通过将表单直接映射到对象?
有什么想法吗?
谢谢
I have taken over a legacy site, and my spidey sense code smell is going crazy over the form paramming used throughout the site.
For example we have a form that allows you to add a new contact, or edit an existing one dependant on the iContactId being present in the URL. So the code in the top part of the cfm file is.
<cfparam name="form.name" default="">
<cfparam name="form.age" default="">
<cfparam name="form.surname" default="">
<--- More cfparams for every form field--->
<cfif isDefined("URL.iContactId")>
<cfset VARIABLES.contact = contactService.getContact("URL.iContactId")/>
<cfset FORM.name = contact.getName() />
<cfset FORM.age= contact.getAge() />
<cfset FORM.surname = contact.getSurname() />
</cfif>
So essentially we are defaulting all form fields to be empty and then populating them if the iContactId is in the URL. The form at the bottom part of the cfm file uses these variables like this.
<form>
<input name="name" value="#FORM.name#">
Now, for me, the issue with these technique, is that there are around 30 form fields on this page, so I am parramming all form fields, and then setting 30 form fields to the contact objects values if the URL.iContactId variable exists. Am trying to think of a way to remove this duplication - maybe by mapping the form directly to the object?
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不明白为什么不。
获取 getContact() 以返回联系人 ID 0 的“空”对象,并从空联系人对象填充表单。我所说的“空”是指为每个字段设置默认值的对象。
例如。
然后在表格中。
唯一需要注意的是;如果您正在进行服务器端验证并返回到此表单,那么您将需要进行管理,确保您有一个包含有效数据的联系人对象来填充您的表单,以便可以更正无效字段。
Don't see why not.
Get getContact() to return an "empty" object for contact ID 0 and populate the form from the empty contact object. By "empty" I mean an object with default values set for each field.
eg.
and then in the form.
Only thing to be aware of; If you are doing server-side validation that returns you to this form then you will need to manage making sure that you have a contact object with the valid data in it to populate your form, so that the invalid fields can be corrected.