New topic: 

Validate fields on save

<http://forums.realsoftware.com/viewtopic.php?t=47704>

         Page 1 of 1
   [ 9 posts ]                 Previous topic | Next topic          Author  
Message        swilson          Post subject: Validate fields on savePosted: 
Thu Apr 25, 2013 1:30 pm                         
Joined: Fri Aug 10, 2012 9:59 am
Posts: 12                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                anis2505          Post subject: 
Re: Validate fields on savePosted: Fri Apr 26, 2013 11:36 am                    
     
Joined: Mon Apr 15, 2013 3:13 am
Posts: 7                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                taylor-design          Post 
subject: Re: Validate fields on savePosted: Fri Apr 26, 2013 12:28 pm           
              
Joined: Wed Mar 22, 2006 11:15 am
Posts: 656
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                swilson          Post subject: 
Re: Validate fields on savePosted: Fri Apr 26, 2013 12:53 pm                    
     
Joined: Fri Aug 10, 2012 9:59 am
Posts: 12                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                silverpie          Post 
subject: Re: Validate fields on savePosted: Fri Apr 26, 2013 2:11 pm            
             
Joined: Sat Oct 01, 2005 9:55 am
Posts: 520                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                taylor-design          Post 
subject: Re: Validate fields on savePosted: Fri Apr 26, 2013 2:17 pm            
             
Joined: Wed Mar 22, 2006 11:15 am
Posts: 656
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                swilson          Post subject: 
Re: Validate fields on savePosted: Fri Apr 26, 2013 3:32 pm                     
    
Joined: Fri Aug 10, 2012 9:59 am
Posts: 12                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                taylor-design          Post 
subject: Re: Validate fields on savePosted: Fri Apr 26, 2013 4:54 pm            
             
Joined: Wed Mar 22, 2006 11:15 am
Posts: 656
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                swilson          Post subject: 
Re: Validate fields on savePosted: Sat Apr 27, 2013 2:24 pm                     
    
Joined: Fri Aug 10, 2012 9:59 am
Posts: 12                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             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 9 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to