Skip to content

Instantly share code, notes, and snippets.

@chrisdpeters
Last active September 30, 2015 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdpeters/1775257 to your computer and use it in GitHub Desktop.
Save chrisdpeters/1775257 to your computer and use it in GitHub Desktop.
Loading "extra" queries for a form in CFWheels http://www.chrisdpeters.com/loading-extra-queries-for-form-cfwheels/
// `controllers/Customers.cfc`
component extends="Controller" {
function init() {
verifies(only="edit,update,show", params="key", paramsTypes="integer");
verifies(only="create,update", params="customer");
}
function new() {
customer = model("customer").new();
}
function create() {
customer = model("customer").new(params.customer);
if (customer.save()) {
redirectTo(route="customer", key=customer.key(), success="...");
}
else {
flashInsert(error="...");
renderPage(action="new");
}
}
function edit() {
customer = model("customer").findByKey(params.key);
}
function update() {
customer = model("customer").findByKey(params.key);
if (customer.update(params.customer)) {
redirectTo(route="customer", key=customer.key(), success="...");
}
else {
flashInsert(error="...");
renderPage(action="edit");
}
}
function show() {
customer = model("customer").findByKey(params.key);
}
private struct function form() {
local.data.states = model("state").findAll(order="name");
local.data.countries = model("countries").findAll(order="name");
return local.data;
}
}
// `controllers/Customers.cfc`
component extends="Controller" {
function init() {
verifies(only="edit,update,show", params="key", paramsTypes="integer");
verifies(only="create,update", params="customer");
filters(only="new,create,edit,update", through="$setStates,$setCountries");
}
function new() {
customer = model("customer").new();
}
function create() {
customer = model("customer").new(params.customer);
if (customer.save()) {
redirectTo(route="customer", key=customer.key(), success="...");
}
else {
flashInsert(error="...");
renderPage(action="new");
}
}
function edit() {
customer = model("customer").findByKey(params.key);
}
function update() {
customer = model("customer").findByKey(params.key);
if (customer.update(params.customer)) {
redirectTo(route="customer", key=customer.key(), success="...");
}
else {
flashInsert(error="...");
renderPage(action="edit");
}
}
function show() {
customer = model("customer").findByKey(params.key);
}
private function $setStates() {
states = model("state").findAll(order="name");
}
private function $setCountries() {
countries = model("countries").findAll(order="name");
}
}
<!--- `views/customers/_form.cfm` --->
<cfparam name="customer">
<cfparam name="arguments.countries" type="query">
<cfparam name="arguments.states" type="query">
<cfoutput>
#textField(objectName="customer", property="name")#
#textField(objectName="customer", property="email")#
#select(objectName="customer", property="countryId", options=arguments.countries)#
#select(objectName="customer", property="stateId", options=arguments.states)#
</cfoutput>
<!--- `views/customers/new.cfm` --->
<cfoutput>
#startFormTag(route="customer")#
#includePartial("form")#
#endFormTag()#
</cfoutput>
<!--- `views/customers/new.cfm` --->
<cfparam name="customer">
<cfparam name="countries" type="query">
<cfparam name="states" type="query">
<cfoutput>
#startFormTag(route="customer")#
#textField(objectName="customer", property="name")#
#textField(objectName="customer", property="email")#
#select(objectName="customer", property="countryId", options=countries)#
#select(objectName="customer", property="stateId", options=states)#
#endFormTag()#
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment