form validation & validateThis

59 views
Skip to first unread message

Ivan

unread,
Apr 22, 2020, 9:01:03 AM4/22/20
to framework-one
Hi everyone,
I'm looking for "the best way" to validate form. 
For now I have only found validateThis (let me know if there are other alternatives).

I wanted to know if someone has already used validateThis and if he can post me a mini tutorial of a few lines on how to best integrate it in fw/1

Thanks! 👋
Ivan

Dejan Karan

unread,
Apr 22, 2020, 9:43:46 PM4/22/20
to framework-one
 
Hi,

I have not tested this particular code and it might not be the best way but hopefully can give you idea how it works. I will test it when I get a chance and update it if it does not work.

Lets say we are creating TODO app and

So you will have following structure:

Views:
/views/todo/form.cfm

Controllers:
/controllers/todo.cfc

Beans and Services:
/model/beans/todo.cfc
/model/services/todoService.cfc

----------------

1. You will have the following in /views/todo/form.cfm is where you start. I'm using bootstrap.

<form method="POST" action="<cfoutput>#buildURL( action = 'todo.submittodo' )#</cfoutput>">

<!--- TODO Form --->
<div class="form-group row">
<label class="col-md-4 control-label" for="textinput"> TO DO </label>
<div class="col-md-6">
<input id="todo" name="todo" type="text" placeholder="What do you want to do?" class="form-control input-md <cfif structKeyExists(rc.todo.getErrors(), "todoerror")>alert-danger</cfif>" value="<cfoutput>#rc.todo.getTodo()#</cfoutput>">
<!--- So if validation in controller fails --->
<cfif structKeyExists(rc.todo.getErrors(), "todoerror")><font color="##FF0000" size="2"><cfoutput>#rc.todo.getErrors().todoerror#</cfoutput></font></cfif>
</div>
</div>

<!-- Submit -->
<div class="form-group row" >
<div class="col-md-4 col-sm-8 col-xs-4">
<input class="btn btn-primary" type="submit" value="Submit">
<span><a class="btn btn-secondary" href="<cfoutput>#buildURL( 'todo.list' )#</cfoutput>" role="button">Cancel</a> </span>
</div>
</div>

</form>

-----------------

2.
// TO DO CONTROLLER
// /controllers/todo.cfc

When you submit this form, fw/1 will call the function in the todo controller called submittodo, and here you can
do security checks, and populate the todoBean and save todo

component accessors="true" extends="base" {
 property todoService
;
 
public void function submittodo( struct rc ) {
 //writedump(rc);
 
if ( rc.action == 'submittodo') {

 
// Populate todoBean with data from the form structure
 getFramework
().populate( rc.todo, rc.fieldnames );

 // Validate form input
 
if ( !rc.todo.isValidTodo() ) {
 
// If false redirect back to the form but preserve user input.
 
// In this simple example it would not be helpful, redirect would suffice, but to give you idea how you can preserve user input in more complicated forms by adding the preserve argument
 getFramework
().redirect(action = 'todo.form', preserve='all');

 
} else {
 
// Save todo
 getTodoService
().saveTodo( rc.todo );
 
}

 } else {
 getFramework().redirect(action = 'todo.form');
 }
 }

}


---------------------
// TO DO SERVICE
// /model/services/todoService.cfc
// You would do heavy duty business logic here of processing todo
component accessors="true" output="true" {

 
public void function saveTodo( todo ) {
 
// get a sequence to use as FK
 
var qTodoid = queryExecute(
 "select todo_tbl.todo_seq.nextval as todo_id from dual"
 
, { }
 
, { datasource = application.dsn} //dsn is defined in Application.cfc or some config service
 );

 
// new query object
 
var foo = new Query();

 foo
.setSQL("
 INSERT INTO todo_tbl.todo_requests
 (todo_id, todo )
 VALUES
 (:todo_id, :todo)
 "
);
 foo.addParam(name="todo_id", CFSQLTYPE="cf_sql_integer",value="#arguments.qTodoid.todo_id#");
 foo
.addParam(name="todo", CFSQLTYPE="cf_sql_varchar",value="#arguments.todo.todo#"); //My Head is spining from all todo's :)
 
 foo
.setDatasource( getConfigService().getSetting('dsn') ).execute();
 
}

}


----------------------
// TODO BEAN
// /model/beans/todo.cfc
// You can expand on this if you have more complicated form
component accessors="true" output="true" {

 property name
="todo_id" type="numeric";
 property name
="todo" type="string";
 property name
="errors";

 
this.setErrors = structNew();

 
function isValidTodo( ){
 
var valid = true;

 
if ( !len(getTodo()) ) {
 variables.Errors.todoerror = "You cannot submit blank To do";
 valid
= false;
 }

return valid;
}

}




Ivan

unread,
Apr 23, 2020, 2:11:46 PM4/23/20
to framework-one
Hi Dejan Karan and thanks for your tip!! 

if possible, I'd like to know other people's opinion too! :)
Thank you!

Dejan Karan

unread,
Apr 23, 2020, 3:30:39 PM4/23/20
to framework-one
Absolutely!
Thanks
Reply all
Reply to author
Forward
0 new messages