Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Wed Aug 15, 2018 2:06 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Validate fields on save
PostPosted: Thu Apr 25, 2013 1:30 pm 
Offline

Joined: Fri Aug 10, 2012 9:59 am
Posts: 15
I would like to check all fields for null values before saving to my database. I can seem to find a built-in collection of controls that could be used to iterate to make this easy. Any sample code or recommendations?

Thanks!
Steve


Top
 Profile  
Reply with quote  
 Post subject: Re: Validate fields on save
PostPosted: Fri Apr 26, 2013 11:36 am 
Offline

Joined: Mon Apr 15, 2013 3:13 am
Posts: 11
Hi,

No Idea if there is a solution to do it or not.

I create a property ValuesArray on my web page( It could be an array or a class) to store the values that should be stored on the Database
I create a method on mywebpage that do two thing
First collect the form field values and put it on my property ValuesArray
and validate the values and return a boolean indcating if it's valid or not


Top
 Profile  
Reply with quote  
 Post subject: Re: Validate fields on save
PostPosted: Fri Apr 26, 2013 12:28 pm 
Offline

Joined: Wed Mar 22, 2006 11:15 am
Posts: 712
Location: Southern California
swilson wrote:
I would like to check all fields for null values before saving to my database. I can seem to find a built-in collection of controls that could be used to iterate to make this easy. Any sample code or recommendations?


Are you trying to...

* Validate controls on the page before copying their data in a DatabaseRecord or RecordSet?

* Validate fields in a DatabaseRecord before insert, or a RecordSet before update?

_________________
Daniel L. Taylor
Custom Controls for Real Studio WE!
Visit: http://www.webcustomcontrols.com/


Top
 Profile  
Reply with quote  
 Post subject: Re: Validate fields on save
PostPosted: Fri Apr 26, 2013 12:53 pm 
Offline

Joined: Fri Aug 10, 2012 9:59 am
Posts: 15
taylor-design wrote:
swilson wrote:
I would like to check all fields for null values before saving to my database. I can seem to find a built-in collection of controls that could be used to iterate to make this easy. Any sample code or recommendations?


Are you trying to...

* Validate controls on the page before copying their data in a DatabaseRecord or RecordSet?

* Validate fields in a DatabaseRecord before insert, or a RecordSet before update?



The first option - I want to validate the values in controls on my page before inserting them as a new record in my table. Way back in my VB days, there were collections of forms, controls, etc. that I used to iterate through and I'm looking for the RB equivalent.

Steve


Top
 Profile  
Reply with quote  
 Post subject: Re: Validate fields on save
PostPosted: Fri Apr 26, 2013 2:11 pm 
Offline

Joined: Sat Oct 01, 2005 9:55 am
Posts: 527
If the verification depends only on the kind of control, you can loop through the Control(x) array, use IsA to check the type, and apply the appropriate rules. But if it's more complex, you'll probably have to handle the controls separately.


Top
 Profile  
Reply with quote  
 Post subject: Re: Validate fields on save
PostPosted: Fri Apr 26, 2013 2:17 pm 
Offline

Joined: Wed Mar 22, 2006 11:15 am
Posts: 712
Location: Southern California
swilson wrote:
The first option - I want to validate the values in controls on my page before inserting them as a new record in my table. Way back in my VB days, there were collections of forms, controls, etc. that I used to iterate through and I'm looking for the RB equivalent.


You can iterate through the controls on a page using WebPage.ControlCount and WebPage.ControlAtIndex. You'll have to test to know which controls map to the database and which serve other functions (i.e. labels).

Note that WebContainer has the same methods. So you could put all of the controls which map to the database on a container to make it easy to get an array of the controls you need to test.

_________________
Daniel L. Taylor
Custom Controls for Real Studio WE!
Visit: http://www.webcustomcontrols.com/


Top
 Profile  
Reply with quote  
 Post subject: Re: Validate fields on save
PostPosted: Fri Apr 26, 2013 3:32 pm 
Offline

Joined: Fri Aug 10, 2012 9:59 am
Posts: 15
taylor-design wrote:
swilson wrote:
The first option - I want to validate the values in controls on my page before inserting them as a new record in my table. Way back in my VB days, there were collections of forms, controls, etc. that I used to iterate through and I'm looking for the RB equivalent.


You can iterate through the controls on a page using WebPage.ControlCount and WebPage.ControlAtIndex. You'll have to test to know which controls map to the database and which serve other functions (i.e. labels).

Note that WebContainer has the same methods. So you could put all of the controls which map to the database on a container to make it easy to get an array of the controls you need to test.


Is there a code sample or example somewhere perhaps? This would appear to be reasonably easy, but it's kick my tail late on a Friday. Thanks gang!


Top
 Profile  
Reply with quote  
 Post subject: Re: Validate fields on save
PostPosted: Fri Apr 26, 2013 4:54 pm 
Offline

Joined: Wed Mar 22, 2006 11:15 am
Posts: 712
Location: Southern California
swilson wrote:
Is there a code sample or example somewhere perhaps? This would appear to be reasonably easy, but it's kick my tail late on a Friday. Thanks gang!


Here's how you would step through the controls. Note that Self is referring to the page, i.e. this is code that would sit in a page method or control event.

Dim c As Integer = Self.ControlCount - 1
Dim i As Integer
Dim objControl As WebObject

For i = 0 To c Step 1
objControl = Self.ControlAtIndex(i)
Next


Beyond that you have to test to see if the control is one to be checked, and cast it to the correct type to then get the value. The code for that will depend on your page.

Let's assume you have a page of controls where all of the WebTextFields have database input, and there are no WebTextFields with something else, and no other control types related to the database. And you need to make sure all the WebTextFields contain some value, i.e. not empty strings:

Dim c As Integer = Self.ControlCount - 1
Dim i As Integer
Dim objField As WebTextField

For i = 0 To c Step 1
objControl = Self.ControlAtIndex(i)
If objControl IsA WebTextField Then
objField = WebTextField(objControl)
If objField.Text.Trim = "" Then
MsgBox("Invalid input!")
End If
End If
Next

_________________
Daniel L. Taylor
Custom Controls for Real Studio WE!
Visit: http://www.webcustomcontrols.com/


Top
 Profile  
Reply with quote  
 Post subject: Re: Validate fields on save
PostPosted: Sat Apr 27, 2013 2:24 pm 
Offline

Joined: Fri Aug 10, 2012 9:59 am
Posts: 15
Awesome - thanks Daniel! You saved me many brain cells with that code. It's so hard to 'unlearn' a previous platform when you don't use it everyday!

Using your code as the base, I wrote a function that includes a test for a WebPopUp (though I still call 'em comboboxes), and figured out how to highlight the text box with a color via WebStyle to indicate missing fields.

Steve


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group