Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Need opinion on form validation...
Old 08-17-2007, 12:58 PM Need opinion on form validation...
Extreme Talker

Posts: 182
Trades: 0
Do you guys think it is better to validate form fields ( or any other data ) WITHIN a particular class method that will insert it into a database, or validate before even sending the data to the class method?

That may be confusing so I will give examples.

Here is a class that will insert user data into a database:

PHP Code:
class insertdata {
    
    function 
insertdata $firstname$lastname ) {
   
         
// insert $firstname and $lastname into a database

    
}


To use that class i would obviously need to validate $firstname and $lastname before i call the insertdata method.

Should I do it like that, or like this below:

PHP Code:
require_once ( 'formValidator.class.php' ); // this is a class i have that validates data

class insertdata extends formValidator {

    function 
insertdata $firstname$lastname ) {
        
         
// if $this->checkdata ( $firstname, $lastname )

        // // $this->nowinsertdata ( $firstname, $lastname )

    
}

    function 
checkdata $firstname$lastname ) {
 
         
// validate $firstname and $lastname using formValidator

         // return true or false

    
}

    function 
nowinsertdata $firstname$lastname ) {
 
         
// insert $firstname and $lastname into a database

    
}


that way of doing things validates the data within the class.

I'm just looking for opinions on which way you all think is better.
bhgchris is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-17-2007, 02:07 PM Re: Need opinion on form validation...
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
http://www.htmlcenter.com/tutorials/...s.cfm/149/PHP/

This link should help you can you give me some talkupation for helping you?
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Old 08-17-2007, 02:13 PM Re: Need opinion on form validation...
Extreme Talker

Posts: 182
Trades: 0
lol Thanks for the link but that is not all what I was asking. You must've just skimmed and thought I was asking how to validate form fields.

I am just wanting an opinion about what I posted. Thank you for your post though, bro.
bhgchris is offline
Reply With Quote
View Public Profile
 
Old 08-17-2007, 02:41 PM Re: Need opinion on form validation...
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
I would merge the two. Have an insert data method but validate the data in that method (or use a sub procedure to do so). That way the programmer doesn't have to worry about any extra messy validation routines.

Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Old 08-17-2007, 02:54 PM Re: Need opinion on form validation...
Extreme Talker

Posts: 182
Trades: 0
To tell you the truth, that is what I'm doing now. I extend my formValidator to validate data inside the methods that are inserting to the database.

That seems to be a bit messy though.

The reason you gave for doing it that way is the very thing that made me think twice.
bhgchris is offline
Reply With Quote
View Public Profile
 
Old 08-17-2007, 06:08 PM Re: Need opinion on form validation...
Experienced Talker

Posts: 49
Trades: 0
Quote:
Originally Posted by JamieLewis View Post
I would merge the two. Have an insert data method but validate the data in that method (or use a sub procedure to do so). That way the programmer doesn't have to worry about any extra messy validation routines.

Jamie
yup that's what i would do as well...
__________________
Time to upgrade your website! Visit us at:
Please login or register to view this content. Registration is FREE
.
- Professionals Designs with Details
Cyrus is offline
Reply With Quote
View Public Profile
 
Old 08-20-2007, 01:31 AM Re: Need opinion on form validation...
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
The page I posted is a hole article on basic PHP validation it should help you.
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Old 08-20-2007, 09:34 AM Re: Need opinion on form validation...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Do you think he wants the TP?... of ocurse i never do that ...

Ok sorry no clue but what is a class? never come across them before

i know about how to make the functions just never seen calsses someone care to explain..
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-20-2007, 09:43 AM Re: Need opinion on form validation...
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
Quote:
Originally Posted by dansgalaxy View Post
Do you think he wants the TP?... of ocurse i never do that ...

Ok sorry no clue but what is a class? never come across them before

i know about how to make the functions just never seen calsses someone care to explain..

Classes are part of Object Orientated Programming, The basic idea behind them is that you program the way objects behave and interact instead of procedurally going through and changing everything e.g.

You an have a page object that stores all the markup information, a database object that controls databases and even a Bob object that controls "bob"

Code:
Class bob{
//variables for here

function bob(){

}

function doSomething(){

}


}

$bobObject = new bob()
$bobObject-> doSomething();
It is difficult to explain the entire concept in a short post like this so its best that you have a look at googling some tutorials. If you can get to grips with OO Programming then languages like Java and C++ become easy to learn.

Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Old 08-20-2007, 09:53 AM Re: Need opinion on form validation...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
0.o...

im fine at the moment

no clue... ill look it up when i have a spare erm year
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-21-2007, 01:02 AM Re: Need opinion on form validation...
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
It is code written in such a way that it can be included using php include and it is simplify reusable code that can be used on many things.
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Old 08-21-2007, 08:02 AM Re: Need opinion on form validation...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
so whats the differance between class and function?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-21-2007, 08:23 AM Re: Need opinion on form validation...
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
Quote:
Originally Posted by dansgalaxy View Post
so whats the differance between class and function?
A function is simply a piece of code that can accept parameters and provide an output.

A Class is a collection of variables and functions that are all related in someway.

The good thing about classes is something known as inheritance....but that is another tutorial in itself (hell, there have been books written on it)

Quote:
It is code written in such a way that it can be included using php include and it is simplify reusable code that can be used on many things.
Um...no. That is not what a class is at all. That definition could be used to describe function libraries, config files etc.

Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Old 08-21-2007, 08:33 AM Re: Need opinion on form validation...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
thats what i thought... and i understand them basically

could you show a example of a simple class that could be used. because i just looked at php.net and it didnt really explain anything to me
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-21-2007, 08:40 AM Re: Need opinion on form validation...
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
Quote:
class page{
var $Title;
var $Content;

function page($btitle,$bcontent){
$Title = $btitle;
$Content = $bcontent;
}

function echoTitle(){
echo "<title>".$Title."</title>";
}

}
Hopefully you can see how it would work, I just threw that up now but normal classes are far more complex. for example my page class can handle modules (smaller template files) and uses a template file with placeholder variables (like <!Title!>)

Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Old 08-21-2007, 08:43 AM Re: Need opinion on form validation...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
oookay.. so how would you then use this in your pages?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-21-2007, 08:47 AM Re: Need opinion on form validation...
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
You would load the class as an object in your php file then manipulate it however you choose. It is difficult to explain


Take a look at my IdevsPage class in action:
Quote:
$Page = new IdevsPage("index.itf");

$Page->BuildReplacement("<!PageTitle!>",$PageTitle);
$Page->BuildReplacement("<!PageDesc!>",$PageDesc);
$Page->BuildReplacement("<!PageContent!>",$Content);
$Page->BuildReplacement("<!Sidebar!>",$Sidebar);
$Page->BuildReplacement("<!Footer!>",$Footer);

$Page->PopulatePage();
$Page->LoadPage();
Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Old 08-21-2007, 09:06 AM Re: Need opinion on form validation...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
what would be the advantage of that compared to like a file which just sets the vars or get it from a db and then using ereg_replace to replace the <!pagetitle!> with the $pagetitle var?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 08-21-2007, 09:13 AM Re: Need opinion on form validation...
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
Well with the system I have it means I can quickly build a site by grabbing the basic template file, adding as many vars as I want and then just adding a few BuildReplacement Lines.

The main index page is smaller due to everything being dealt with in prewritten class files which makes debugging easier.

It also means that the class is extensible so I can add as many new functions as I want and I know where everything is.

Once you have used objects and classes, trust me, you will never go back to do everything procedural. Objects and Classes make life far easier.

Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Old 08-23-2007, 03:20 PM Re: Need opinion on form validation...
Extreme Talker

Posts: 182
Trades: 0
whew, alrighty ...
For the record, I ended up doing the data validation in it's own class.

I build a controller class to assist in checking the user submitted data. Once the data had been verified clean, I sent it to the proper class to handle it. I believe this is the best way. Validating the data inside the class that was handling it was a stupid and messy idea I think.


Dan:
phpfreaks has some nice noob tutorials on OOP and classes:
http://www.phpfreaks.com/tutorial_ca...rogramming.php

If you are learning about oop and classes for the first time, I would recommend making sure you are well rested, and your brain is clear before you attempt to read any articles or tutorials It can be overwhelming for a beginner.

It is so true. Once you start, you will wonder how you ever did anything without using OOP techniques. Whatever you do, don't get overwhelmed and give up. It will be worth it in the end...baby steps


goheadtry:

I think you misunderstood what I was asking. I was not asking how to validate a form. I was asking opinions on what particular point in my app to validate form data.


Thanks again for the replies!!!!
bhgchris is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need opinion on form validation...

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.54903 seconds with 12 queries