Returning error message from Classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FutureShock

    Returning error message from Classes

    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($resul t['error'])){
    //process for display
    } elseif(isset($r esult['result'])) {
    //process for DB
    }

    *************** *************** *************** *************** *****

    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
    }

    *************** *************** *************** *************** ******

    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
  • Jerry Stuckle

    #2
    Re: Returning error message from Classes

    FutureShock wrote:
    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($resul t['error'])){
    //process for display
    } elseif(isset($r esult['result'])) {
    //process for DB
    }
    >
    That's one possibility.
    *************** *************** *************** *************** *****
    >
    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).
    *************** *************** *************** *************** ******
    >
    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@attgl obal.net
    =============== ===

    Comment

    • FutureShock

      #3
      Re: Returning error message from Classes

      Jerry Stuckle wrote:
      FutureShock wrote:
      >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($resu lt['error'])){
      > //process for display
      >} elseif(isset($r esult['result'])) {
      > //process for DB
      >}
      >>
      >
      That's one possibility.
      >
      >************** *************** *************** *************** ******
      >>
      >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($erro r['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).
      >
      >************** *************** *************** *************** *******
      >>
      >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.
      >
      OK thanks for the input and insight.
      I will definitely need to implement the message class for varied output.

      Using the PUBLIC variable kind of set uneasy with me after most of the
      stuff I have read and since I was sitting on the fence with that, your
      input helps sway me off of it towards the array option.

      Thanks
      Scotty

      Comment

      Working...