Last active
March 18, 2019 01:56
-
-
Save chrisdpeters/31ea934fc81f5c3fec01be6bae8ae47c to your computer and use it in GitHub Desktop.
Search forms with tableless models in CFWheels http://blog.chrisdpeters.com/search-forms-tableless-models-cfwheels/
This file contains 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
<cfoutput> | |
#startFormTag(route="invoices", method="get")# | |
#textFieldTag(name="startDate", value=params.startDate)# | |
#textFieldTag(name="endDate", value=params.endDate)# | |
#submitTag(value="Filter Invoices")# | |
#endFormTag()# | |
<table> | |
<thead> | |
<tr> | |
<th>Invoice</th> | |
<th>Date</th> | |
<th>Amount</th> | |
</tr> | |
</thead> | |
<tbody> | |
<cfloop query="invoices"> | |
<tr> | |
<td>#h(id)#</td> | |
<td>#DateFormat(createdAt)#</td> | |
<td>#DollarFormat(amount)#</td> | |
</tr> | |
</cfloop> | |
</tbody> | |
</table> | |
</cfoutput> |
This file contains 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
<cfoutput> | |
#startFormTag(route="invoices", method="get")# | |
#textField(objectName="search", property="startDate")# | |
#errorMessageOn(objectName="search", property="startDate")# | |
#textField(objectName="search", property="endDate")# | |
#errorMessageOn(objectName="search", property="endDate")# | |
#submitTag(value="Filter Invoices")# | |
#endFormTag()# | |
<table> | |
<thead> | |
<tr> | |
<th>Invoice</th> | |
<th>Date</th> | |
<th>Amount</th> | |
</tr> | |
</thead> | |
<tbody> | |
<cfloop query="search.results"> | |
<tr> | |
<td>#h(id)#</td> | |
<td>#DateFormat(createdAt)#</td> | |
<td>#DollarFormat(amount)#</td> | |
</tr> | |
</cfloop> | |
</tbody> | |
</table> | |
</cfoutput> |
This file contains 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
component extends="Controller" { | |
function index() { | |
param name="params.startDate" default=""; | |
param name="params.endDate" default=""; | |
local.where = []; | |
if (IsDate(params.startDate)) { | |
ArrayAppend(local.where, "createdAt >= '#params.startDate#'"); | |
} | |
if (IsDate(params.endDate)) { | |
local.nextDay = DateAdd("d", 1, params.endDate); | |
local.nextDay = DateFormat(local.nextDay, "m/d/yyyy"); | |
ArrayAppend(local.where, "createdAt < '#local.nextDay#'"); | |
} | |
invoices = model("invoice").findAll(where=ArrayToList(local.where, " AND ")); | |
} | |
} |
This file contains 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
component extends="Controller" { | |
function index() { | |
param name="params.startDate" default=""; | |
param name="params.endDate" default=""; | |
local.where = []; | |
// Let's make sure the start date and end date jive. | |
if (IsDate(params.startDate) && IsDate(params.endDate) && params.startDate > params.endDate) { | |
flashInsert(error="The start date must be on or before the end date."); | |
} | |
if (IsDate(params.startDate)) { | |
ArrayAppend(local.where, "createdAt >= '#params.startDate#'"); | |
} | |
if (IsDate(params.endDate)) { | |
local.nextDay = DateAdd("d", 1, params.endDate); | |
local.nextDay = DateFormat(local.nextDay, "m/d/yyyy"); | |
ArrayAppend(local.where, "createdAt < '#local.nextDay#'"); | |
} | |
invoices = model("invoice").findAll(where=ArrayToList(local.where, " AND ")); | |
} | |
} |
This file contains 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
component extends="Controller" { | |
function index() { | |
// Note that moving this into an object named `search` will change the | |
// `params` struct slightly. | |
param name="params.search.startDate" default=""; | |
param name="params.search.endDate" default=""; | |
// We pass the `params.search` struct in as properties on the search form | |
// object. | |
search = model("invoiceSearchForm").new(argumentCollection=params.search); | |
// This runs the search and adds an error message if validation fails. | |
if (!search.run()) { | |
flashInsert(error="There was an error with your search filters"); | |
} | |
} | |
} |
This file contains 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
component extends="Model" { | |
function init() { | |
table(false); | |
// Set property labels for form fields and related error messages. | |
property(name="startDate", label="Start"); | |
property(name="endDate", label="End"); | |
//... | |
} | |
//... | |
} |
This file contains 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
component extends="Model" { | |
function init() { | |
// Make it tableless | |
table(false); | |
// Validations | |
validatesFormatOf(properties="startDate,endDate", type="date", allowBlank=true); | |
validate("startDateBeforeEndDateValidation"); | |
} | |
boolean function run() { | |
// Run validations and abort if failed. | |
if (!this.valid()) { | |
this.results = QueryNew(""); | |
return false; | |
} | |
// Continue with query if validation passed. | |
local.where = []; | |
if (IsDate(this.startDate)) { | |
ArrayAppend(local.where, "createdAt >= '#this.startDate#'"); | |
} | |
if (IsDate(this.endDate)) { | |
local.nextDay = DateAdd("d", 1, this.endDate); | |
local.nextDay = DateFormat(local.nextDay, "m/d/yyyy"); | |
ArrayAppend(local.where, "createdAt < '#local.nextDay#'"); | |
} | |
this.results = model("invoice").findAll(where=ArrayToList(local.where, " AND ")); | |
return true; | |
} | |
private function startDateBeforeEndDateValidation() { | |
if (IsDate(this.startDate) && IsDate(this.endDate) && this.startDate > this.endDate) { | |
this.addError("startDate", "Start Date must be on or before End Date"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment