Last active
September 30, 2015 10:57
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// `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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// `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"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- `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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- `views/customers/new.cfm` ---> | |
<cfoutput> | |
#startFormTag(route="customer")# | |
#includePartial("form")# | |
#endFormTag()# | |
</cfoutput> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- `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