Eggzorcist Posted November 6, 2010 Share Posted November 6, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/217963-using-arrays-for-errors-questions/ Share on other sites More sharing options...
Yucky Posted November 7, 2010 Share Posted November 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217963-using-arrays-for-errors-questions/#findComment-1131254 Share on other sites More sharing options...
phpSensei Posted November 7, 2010 Share Posted November 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217963-using-arrays-for-errors-questions/#findComment-1131283 Share on other sites More sharing options...
Yucky Posted November 7, 2010 Share Posted November 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217963-using-arrays-for-errors-questions/#findComment-1131284 Share on other sites More sharing options...
Eggzorcist Posted November 7, 2010 Author Share Posted November 7, 2010 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"){ ... } Quote Link to comment https://forums.phpfreaks.com/topic/217963-using-arrays-for-errors-questions/#findComment-1131395 Share on other sites More sharing options...
Eggzorcist Posted November 7, 2010 Author Share Posted November 7, 2010 What is the proper methodology for declaring an empty assoc array? And checking if its still empty or not? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/217963-using-arrays-for-errors-questions/#findComment-1131485 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.