Skip to content

Instantly share code, notes, and snippets.

@chrisdpeters
Last active March 18, 2019 01:56
Show Gist options
  • Save chrisdpeters/31ea934fc81f5c3fec01be6bae8ae47c to your computer and use it in GitHub Desktop.
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/
<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>
<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>
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 "));
}
}
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 "));
}
}
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");
}
}
}
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");
//...
}
//...
}
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