Jump to content

Recommended Posts

Hello all,

 

When I run this code and a second error occurs, the array will be overwritten. Can someone tell me how to show both errors?

 

if($this->formValidator->isEmpty($username) || $this->formValidator->isEmpty($password)){
$this->message->storeMessage(array($error['a'] = 'U heeft niet alle velden ingevuld.'));
}
if($this->formValidator->isInvalid($username) || $this->formValidator->isInvalid($password)){
$this->message->storeMessage(array($error['b'] = 'U heeft ongeldige karakters ingevuld.'));
}
if($this->message->messages == true){
$this->message->showMessage();
}

 

class Message{

public $messages = array();

public function storeMessage(array $messages){
	$this->messages = $messages;
}
public function showMessage(){
	echo '<ul>';
		foreach($this->messages as $msg){
		echo '<li>'. $msg .'</li>';
		}
	echo '</ul>';
}
}

 

Link to comment
https://forums.phpfreaks.com/topic/233617-message-object-arrays/
Share on other sites

That's because you're assigning an array.

 

More to the point, your foreach is simply outputting each inner array as a whole, not their contents.  I'm wondering why you're even passing an array into your object since you never seem to do anything with its indices.  'a' and 'b' are hardly descriptive.

I'm confused now ;P

 

This is what I want. But then the oop way.

 

Where have I gone wrong and how should it be done?

 

$error = array();

$error['empty'] = "Empty input.";
$error['invalid'] = "Invalid characters.";

if(count($error)>0){
	echo '<u>Errors:</u>';
	echo '<ul>';
		foreach($error as $msg){
			echo '<li>'. $msg .'</li>';
		}
	echo '</ul>';
}

 

Thanks so far.

class Errors
{
   private $errorMsgs = array();

   public function add($type, $message)
   {
      if ($type != null && $message != null)
      {
         $this->errorMsgs[$type] = $message;
      }
      else
      {
         // error
      }
   }

   public function show() // <-- not the best design, as it's best to separate processing from display
   {
      foreach ($this->errorMsgs as $msg)
      {
         // echo errors
      }
   }
}

$errors = new Errors();

$errors->add("empty", "Empty input");
$errors->add("invalid", "Invalid characters");

$errors->show();

 

This is essentially a 1:1 translation of what you want into a class.  Like I say above, it's not the best idea, from a design standpoint, to have a class like this directly output its contents.  It muddies up what the class is used for (storing errors and displaying them?).  Instead, I'd simply return $errorMsgs from the object and iterate over them in your display code.

Thankyou so much!

 

I would have never figured that out lol. How can I learn these things? I mean, I can't find this in the php manual. I have also read alot of tutorials but still I can't figure this out myself.

 

I know that echoing in a class is not the best thing to do, but I need something to output my own generated errors. I see no other option because Exceptions are used for other kind of errors.

 

If you have any suggestions to learn this, please tell me.

Uh... there's a whole section in the manual devoted to OOP: http://www.php.net/manual/en/language.oop5.basic.php

 

And, like I said before, simply return the array of errors and let your display code handle it:

 

class Errors
{
   private $errorMsgs = array();

   public function add($type, $message)
   {
      if ($type != null && $message != null)
      {
         $this->errorMsgs[$type] = $message;
      }
      else
      {
         // error
      }
   }

   public function getAll()
   {
      return $this->errorMsgs;
   }
}

$errors = new Errors();

$errors->add("empty", "Empty input");
$errors->add("invalid", "Invalid characters");

foreach ($errors->getAll() as $error)
{
   // echo $error
}

 

Keep in mind that this class is very bare-bones.  You could add functionality to return the count of multiple errors of the same type, for example.  And there's no error handling, or data normalization (error types should probably all pass through strtolower).  This was just some code to show the basic idea.  It's not ready for production.

 

Exceptions aren't a good idea for something like this.  They're used to encapsulate application errors, such as being unable to connect to a db.  They also contain technical details that you wouldn't want to show the user.  User errors don't qualify, and require better messaging so the user can remedy the problem.

Keep in mind that this class is very bare-bones.  You could add functionality to return the count of multiple errors of the same type, for example.  And there's no error handling, or data normalization (error types should probably all pass through strtolower).  This was just some code to show the basic idea.  It's not ready for production.

 

Why is this not ready for production? Because there is no error handling?

 

And what do you mean with data normalization?

 

I am writing my own login system and I'd like to implement this code to produce messages when they deliver bad input. Nothing special right?

Keep in mind that this class is very bare-bones.  You could add functionality to return the count of multiple errors of the same type, for example.  And there's no error handling, or data normalization (error types should probably all pass through strtolower).  This was just some code to show the basic idea.  It's not ready for production.

 

Why is this not ready for production? Because there is no error handling?

 

Yes, and for the other reasons I specified.  The class, currently, is merely a sack of errors.  What would happen if the same type of error is triggered twice, like, say, the form has two empty fields?  How would you want to handle it?  Also, would you want to log these errors?  Like I said, the class is incomplete.  It's meant as a teaching tool, nothing more.

 

And what do you mean with data normalization?

 

Well, like I said above, you most likely want each type of error to be formatted in the same way, so you don't have "empty", "Empty", "EMPTY", etc. when trying to add them to the collection. 

 

I am writing my own login system and I'd like to implement this code to produce messages when they deliver bad input. Nothing special right?

 

Nothing special, but not something entirely trivial, either.  Displaying an error message to the user is the obvious step.  What about the error itself?  Does the error matter?  Would you want to track them to see if there's an issue with your form design?  How would multiple errors on the same field work?  Would an empty field also be considered one with invalid characters?  How would multiple errors of the same type be handled?

About the error handling. I could use exceptions for that right?

 

I can't think of a situation when this could go wrong.

 

Yeah, I'd use exceptions for those errors.

 

Examples of errors would be if add() was invoked without one of the parameters, or if an illegal error type (if you feel like creating a list of acceptable errors) was passed in.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.