Jump to content

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"){
...
}

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.