FutureShock wrote:
Quote:
I am using a registration class to process a registration form and need
some opinions on returning error messages.
>
I am self referring the page on submit.
>
I would like to send each form field to the class for processing
(validating, sanitizing, etc..) So far no problem.
>
Now I am throwing ideas around on how best to check for error messages
to the users.
>
One idea was to expect and array from the class as a return value, set
the error key in the function if needed and check if the error key is
set for each call.
>
public function validateEmail($post) {
//if no error after validating
$result['result'] = $post;
return $result
//if error
$result['error'] = 'Please input a valid email';
return $result;
}
>
>
$result = $register->validateEmail($_POST['email']);
if(isset($result['error'])){
//process for display
} elseif(isset($result['result'])) {
//process for DB
}
>
|
That's one possibility.
Quote:
************************************************** ***************
>
Second idea was to use a PUBLIC variable in the class and test it.
>
public $error;
>
public function validateEmail($post) {
//if no error after validating
$result = $post
return $result;
>
//if error
$this->$error['email'] = "Please enter a valid email";
}
>
$result = $register->validateEmail($_POST['email']);
if(isset($error['email]){
//process for display
} else {
//process $result
}
>
|
Good OO design dictates you (almost) never have public variables. If
you want to do it this way, you should have the variable private and
have a get method to access it (maybe also a reset_errors method to
clear it, although that's generally not needed).
Quote:
************************************************** ****************
>
I have only tested the first idea and it has worked so far.
>
I am still learning OOP so the second idea may not even be good practice
or might not even work as predicted.
>
I know some of you out there have come across this and am curious what
has worked for you.
>
Thanks
Scotty
|
There isn't a "right" way - there is nothing wrong (other than the
public variable) with either case.
One way to think of it is that (among other things), OO is supposed to
limit code duplication. So if you use the same class in 2 (or 20 or
200) pages, are you going to always want the same error messages? If
so, then I would recommend keeping the messages in the class itself.
But if you're going to want different messages, then you would want to
keep them separate.
A third way would be to have a message class with all the messages your
class(es) use. This makes multilingual sites much easier.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================