views:

90

answers:

1

This question will undoubtedly be difficult to answer and ask in a way that makes sense but I'll try my best:

I have a form which uses PHP to display certain sections of the form such as:

<?php if ($_SESSION['EnrType'] == "Individual") { display only form information for individual enrollment } ?>

and

<?php if ($_SESSION['Num_Enrs'] > 6) { display only form information for 7 total members enrollment } ?>

In each form piece, unique information is collected about each enrollee but the basic criteria for each enrollee is the same, i.e. All enrollee's must use have a value in the FirstName field. Each field is named according to the enrollee number, i.e. Num1FirstName; Num2FirstName.

I have a PHP validation script which is absolutely fantastic and am not looking to change it but the issue I am running into is duplication of the script in order to validate ALL fields in one swoop.

On submission, all POSTED items are run through my validation script and based on the rules set return an error if they do not equal true.

Sample code:

if (isset($_POST['submit']))
{
  // import the validation library
  require("validation.php");

  $rules = array(); // stores the validation rules

  //All Enrollee Rules
  $rules[] = "required,Num1FirstName,Num2FirstName,The First Name field is required.";

The script above does the following, $rules[] ="REQUIREMENT,fieldname,error message" where requirement gives criteria (in this case, simply that a value is passed), fieldname is the name of the field being validated, and error message returns the error used.

My Goal is to use the same formula above and have $rules[] run through ALL firstnames and return the error posted ONLY if they exist (i.e. dont check for member #7's first name if it doesnt exist on the screen).

If I simply put a comma between the 'fieldnames' this only checks for the first, then second, and so on so this wont work.

Any ideas?

A: 
Alec
@Alec - I think you are probably on the right track. I am having a bit of a hard time following but will try this. I think based on what you describe though i still have to create custom error messages for all first name types. The fields are defined as F1FirstName and F2FirstName currently. I may have to simply build out a TON of code to have this checked. Thank you!
JM4
What is "F1" and "F2" exactly? If they're both of the type "firstname", and both should return the same error message when they don't pass your validation, just make sure that you find out what type of field it is. Not too pretty but certainly possible: `if (strstr($fieldname,'FirstName')) { // do a firstname validation }`.
Alec
F1 and F2 are indicators for which athlete the FirstName belongs to and 'must' be attached to the field name. i'm not sure how to separate F1 from Firstname, then validate, then put them back together in the end. Perhaps you can assist with the javascript bit i put in my comment below the original post. I think this will help me discover the right answer.
JM4
I edited my answer and added a few things. Posting how that validator works might clear a few things up if it still doesn't work as you intended.
Alec
Alec - I think it does but ultimately adding the second line (as you mentioned) is exactly what I am trying to avoid. In one form I have over 200 variables so listing the rules line by line 200 times is what I was trying to avoid but just may be what I am forced to do!
JM4
Why avoid it? Isn't that exactly what the validator requires?
Alec
I want to avoid it because I then have to create hundreds of line of code when the goal of my original post was to consolidate the code. The answer you gave is exactly what I am already doing.
JM4
Then I don't really get what you're trying to do :) If you have a page with say 100 people and thus possibly 100 first names, and you want to check each name for its validity after an update, you'll need to check each of those 100 names one way or the other. Either call on the validator 100 times, or change the validator in such a way that it'll accept an array (so you only have to call it once) and it returns an array with the fields that didn't pass the validation.
Alec
@Alec - reviewing this again, you have been a great help!
JM4