当用户离开表单字段为空时,用正确的参数重新呈现视图的问题 [英] Problems re-rendering a view with correct params when a user leaves form fields blank
问题描述
我在<$ c $的< g:select>
标记中使用了 noSelection
c> Account 字段为我的加载
创建
视图。当用户没有做出选择并点击创建按钮时,我希望所有表单字段的当前值被传递给 save
闭包在我的加载
控制器,然后再调用 create
视图并使用前面提到的当前值填充该视图。这不会发生在我的两个表单字段,我不清楚为什么。
以下是 Load
< code $ /> c code> create 由 / truckingmanagement / load / create
控制器调用的视图看起来像:
如果用户没有在 所以我在这里有两个问题,第一个是 以下是用于帐户
字段中进行选择,然后继续单击创建按钮,则显然无法创建负载。以下是由 / truckingmanagement / load / save 调用的结果
Load
create
/ code>控制器看起来像:
Logged By $ c $第二个视图中的c>字段未使用在第一个视图中选择的值填充。此外,下拉框完全是空的,没有用户可以选择。第二个问题是
帐户
字段下拉框完全是空的,没有帐户可供选择。
记录的
和<$ c的< g:select>
create
视图中的$ c> Account 字段。
记录者
字段:
< g:select name =loggedBy.idfrom =$ {loggedByUsers}optionKey =idvalue =$ {loadInstance?.loggedBy?.id}/>
帐户
栏位:
< g:select id =accountname =account.idfrom =$ {accountsWithCargoDestinations}optionKey =idnoSelection =['':' - Select-']/>
以下是创建
和 save
在我的 LoadController
中关闭:
角色role2 = Role.findByAuthority(ROLE_OFFICE_PROFESSIONAL)
)
def loggedByUsers = UserRole.findAllByRoleInList([role1,role2,role3])。user
$ b $ def loadInstance = new Load()
loadInstance.properties = params
def accountsWithCargoDestinations = Account.findAllByUserInList(Address.findAll(from Address as addresses where addresses.cargoDestination = true)。user)
return [loadInstance:loadInstance,loggedByUsers:loggedByUsers, accountsWithCargoDestinations:accountsWithCargoDestinations]
}
$ b $ def save = {
def loadInstance = new Load(params)
if(loadInstance.save(flush:true)){
flash.message =$ { (code:'default.created.message',args:[message(code:'load.label',default:'Load'),loadInstance.id])}
redirect(action:show ,id:loadInstance.id)
}
else {
System.out.println params //尝试查看问题字段中的任何值是否丢失。
render(view:create,model:[loadInstance:loadInstance])
}
}
您会注意到 println
语句中的 else
部分 save
closure打印出 params
。当我这样做时,我发现在第一个视图中的 Logged By
字段中选择的值确实从第一个视图一路传递到了这一点,但我不知道不知道为什么当 Load
create
视图显示在下一行代码中时不显示它,或者至少为什么 (即 loggedByUsers
来自 truckingmanagement / load / create
控制器)渲染视图时不会填充 Logged By
字段。任何帮助将不胜感激,抱歉的冗长。
回答您在评论回复中提出的问题。您可能想要在控制器中使用 chain 进行调查。如果模型中尚未存在新的loadInstance,则需要稍微重新创建您的创建操作。但是,这应该会有诀窍。
I am using a noSelection
attribute in the <g:select>
tag of the Account
field for my Load
create
view. When a user does not make a selection and clicks the "Create" button I expect the current values of all form fields to be passed to the save
closure in my Load
controller, which in turn re-calls the create
view and populates that view with the previously mentioned current values. This does not happen for two of my form fields and I am unclear why.
Here is what the Load
create
view called by the /truckingmanagement/load/create
controller looks like:
If a user does not make a selection on in the Account
field and then proceeds to click the "Create" button, a load obviously cannot be created. Here is what the resulting Load
create
view called by the /truckingmanagement/load/save
controller looks like:
So I have two issues here, the first of which is that the Logged By
field in the second view is not populated with the value selected in the first view. Also, the drop down box is completely empty, there are no users to select from. The second issue is that the Account
field drop down box is completely empty, there are no accounts to select from.
Here are the <g:select>
tags used for the Logged By
and Account
fields in the Load
create
view.
Logged By
field:
<g:select name="loggedBy.id" from="${loggedByUsers}" optionKey="id" value="${loadInstance?.loggedBy?.id}" />
Account
field:
<g:select id="account" name="account.id" from="${accountsWithCargoDestinations}" optionKey="id" noSelection="['':'-Select-']" />
Here are the create
and save
closures in my LoadController
:
def create = {
Role role1 = Role.findByAuthority("ROLE_ADMIN")
Role role2 = Role.findByAuthority("ROLE_OFFICE_PROFESSIONAL")
Role role3 = Role.findByAuthority("ROLE_DRIVER")
def loggedByUsers = UserRole.findAllByRoleInList([role1, role2, role3]).user
def loadInstance = new Load()
loadInstance.properties = params
def accountsWithCargoDestinations = Account.findAllByUserInList(Address.findAll("from Address as addresses where addresses.cargoDestination=true").user)
return [loadInstance:loadInstance, loggedByUsers:loggedByUsers, accountsWithCargoDestinations:accountsWithCargoDestinations]
}
def save = {
def loadInstance = new Load(params)
if (loadInstance.save(flush: true)) {
flash.message = "${message(code: 'default.created.message', args: [message(code: 'load.label', default: 'Load'), loadInstance.id])}"
redirect(action: "show", id: loadInstance.id)
}
else {
System.out.println params //Trying to see if any values from the problem fields got lost.
render(view: "create", model: [loadInstance: loadInstance])
}
}
You will notice in the println
statement in the else
part of my save
closure which prints out the params
. When I did this I found that the value selected in the Logged By
field in the first view was indeed passed from the first view all the way to this point correctly, but I don't know why it is not displayed when the Load
create
view is rendered in the very next line of code, or at the least why something (i.e. loggedByUsers
from the truckingmanagement/load/create
controller) does not populate the Logged By
field when the view is rendered. Any help would be appreciated, and sorry for the verbosity.
To answer your question posed in the comment response. You might want to investigate using chain in your controller. You will need to re-work your create action a little to only instance a new loadInstance if not already present in the model. However, that should do the trick.
这篇关于当用户离开表单字段为空时,用正确的参数重新呈现视图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!