Azarian Posted June 12, 2011 Share Posted June 12, 2011 I have a class I wrote that handles the back end processing for registering/login/change pass/pass recovery. What I am trying to do is basically pass the $_POST var to a function than run through the values to see if they are set. I am trying to compare the $_POST array to a preset array with valid form field names with also the names the user read on the page, so it can be returned to the user which field or fields they messed up. I can't seem to wrap my head around how to make this work. Anyone have any thoughts or suggestions what I can do? private $required_fields = array( 'user_name' =>'Username', 'email_address' => 'Email Address', 'password' => 'Password', 'password2' => 'Confirm Password', 'oldpassword' => 'Old Password', 'newpassword' => 'New Password', 'newpassword2' => 'Confirm New Password' ); public function form_field_validation($form_array){ foreach ($form_array as $key){ $field = $form_array[$key]; if(in_array($field, $this->required_fields)){ $field_name = $this->required_fields[$field]; if(!$field){ array_push($this->validation_fail_msg, " $field_name is a required field."); echo $this->validation_fail_msg; } } } } Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted June 12, 2011 Share Posted June 12, 2011 If i understand you correctly, you want to array keys in $_POST to resemble the keys in your preset array. if so you can do something like $postkeys = array_keys($_POST); $presetkeys = array_keys($required_fields); //now we can compare them if ($postkeys == $presetkeys){ //all keys in post are set the only problem is this assumes the keys are in the same order in both arrays. You can use array_intersects however, with some clever logic. Assume we have already defined $postkeys and $presetkeys like above $intersection = array_intersect($presetkeys, $postkeys); //now the $intersection variable will have all the values that are in both //if the length of intersection is the same as $preset, we know all the keys are there //even if they arent in the same order if (sizeOf($intersection) == sizeOf($presetkeys)){ //they have same values! } now if we wanted to determine what keys werent in our $_POST array, we could use array diff, for example $notPresent = array_diff($presetkeys, $postkeys); //now we can do whatever //for example foreach($notPresent as $value){ echo "You missed $notPresent! <br />"; } Hope this helps Quote Link to comment Share on other sites More sharing options...
Azarian Posted June 12, 2011 Author Share Posted June 12, 2011 Your kinda on the right track. Your code only gets me half way there though. Let me break this down psuedo style and try to explain a little better. $_post = contains the form field names => with the user submited value $my_preset_var = List of all possible form field names => What the user read on the form for example a form field maybe named email_address but on UI side the user sees Email Address. Not all the possible field names will ever be passed to the function at the same time. Lets say at some point else where in my code I want to check if an email field was filled in by a user. I pass it to the function. It sees email_address is a valid field to be passing to the function. Than it checks if the user filled in the field. If the user didn't fill in the field it returns back Email Address is a required field. I hope this break down is more clear. Quote Link to comment Share on other sites More sharing options...
Azarian Posted June 12, 2011 Author Share Posted June 12, 2011 I went back and looked at your code trying to figure out if I could make some combo of your code and mine work. I totally missed the fact you used array_keys() on both arrays. While Looking up to make sure that function did what I thought it did, I realized the in_array() I used didn't do what I thought it did. With a little bit of tweakin I got the code to work. Thanks for giving me the nudge I needed to figure this out! Here is the code that works. public function form_field_validation($form_array){ $postkeys = array_keys($form_array); foreach ($postkeys as $key){ if(array_key_exists($key, $this->required_fields)){ $field_ui_name = $this->required_fields[$key]; $user_input = $form_array[$key]; if(!$user_input){ array_push($this->validation_fail_msg, " $field_ui_name is a required field."); } } } } Quote Link to comment 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.