Jump to content

using arrays for errors questions


Eggzorcist

Recommended Posts

Hi, I have a few questions concerning controlling field validation using $error = array(); in a way to send back exactly which is false.

 

This is what I'm doing.

$error = array(NULL => '', NULL => '', NULL => '');

 

I check 3 fields: name, email, message and whether they are good or bad. As such

 

//if the value is BAD I do:
$error['message'] = "ERROR";

//if the value is good I do:
$error['message'] = NULL;

 

However, with this method I'm not sure how I can check whether any of these have an error...

 

Also, is it the good way to declare an array for this functioning?

 

Thank you very much!

Link to comment
https://forums.phpfreaks.com/topic/217963-using-arrays-for-errors-questions/
Share on other sites

Using an array to hold errors is pretty standard. Have you considered using a multidimensional array instead?

 

When I need to display errors to the user, I tend to use a multidimension array that might have a structure along the lines of:

$array['data'][] - data needed be displayed goes in this subarray.

$array['errors'][] - informative error messages are stored in this subarray and later looped through.

$array['success'] - true/false. Helps to determine styling such as a tick or a cross.

For user errors like "Please enter a username..", I tend to log them in the array, I wouldn't go as far as doing multidimentional arrays unless I am running a large scripts with different possibilities of errors/failures.

 

but for script errors

 

I try and keep most of my site's infrastructure outside of user's view, just by loggin most errors somewhere safe in a txt file.

 

 

I try and keep most of my site's infrastructure outside of user's view, just by loggin most errors somewhere safe in a txt file.

 

For user errors like "Please enter a username..", I tend to log them in the array, I wouldn't go as far as doing multidimentional arrays unless I am running a large scripts with different possibilities of errors/failures.

 

Given that you may want to return data to be displayed on the page as well as error messages, it seems logical to use multidimensional arrays.

 

I find that it's easier to stick with one structure even when I don't need to send data/error messages back to the user rather than possibly changing it later down the line.

Well I don't feel like I need a multi-dimensional array because most data will be already in the html and jquery... So all I want it to do is to store if its an error or not, so I can know which display error I should have slide open.

 

 

 

I'm just worried, since I don't think this line is working for me:

 

$error = array(NULL => '', NULL => '', NULL => '');

if(filter_var($name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[A-Za-z](?=[A-Za-z0-9_.]{4,24}$)[a-zA-Z0-9_]*\.?[a-zA-Z0-9_]*$/"))) === false){
$error['name'] = "ERROR";	
}

else{
$error['name'] = NULL;	
}



if ($error == NULL and $error != "ERROR"){
...
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.