Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand what you're trying to do correctly:</p> <p><strong>1. Define types</strong><br> First, you need to define what type of field requires what kinds of rules, e.g.: <code><pre>$type['FirstName']['requirement'] = 'required'; $type['FirstName']['error_message'] = 'The First Name field is required.';<br> // just random example: $type['MobilePhone']['requirement'] = 'optional'; $type['MobilePhone']['error_message'] = 'Only enter digits.'; </pre></code></p> <p><strong>2. Go through each posted value</strong><br> Second, check for each posted value what type of field it is. Now a lot of stuff might be included in the <code>$_POST</code> array, and you only need to check certain fields. It might make it a lot easier to use names like <code>checkthis;1234;FirstName</code> for your input fields. <code><pre>foreach ($_POST as $key => $value) { // split the key, so you know what's what: $data = explode(';',$key);<br> // now $data[0] tells you what kind of field it is // $data[1] is the ID of the enrollee (your Num1, Num2) // $data[2] is the type of field<br> // only do checks for posted fields that start with "checkthis" if ($data[0]=='checkthis') { // now you can fill the $rule array: $rule[] = $type[$data[2]]['requirement'] . ',' . $key . ',' . $type[$data[2]]['error_message']; } }</pre></code> This way it doesn't matter what you include in your form. As long as you've defined the type of field, a rule will be added to the rule array if it's included in the $_POST values, and your validation script will do the rest.</p> <hr /> <p><strong>Edit</strong><br> If you structure your input fields like this:</p> <pre><code> &lt;input type="text" name="F1Firstname" value="" /&gt; &lt;input type="text" name="F2Firstname" value="" /&gt; etc..</code></pre> <p>..and you submit the form, the $_POST array will e.g. look like this:</p> <pre><code>//print_r($_POST) gives: Array ( [F1FirstName] => John [F2FirstName] => Jane )</code></pre> <p>..the easiest thing to do is to loop through each of those with <code>foreach</code>:</p> <pre><code>foreach ($_POST as $key => $value) { // first $key will be "F1FirstName" // first $value will be "John" // second $key will be "F2FirstName" // second $value will be "Jane" // etc }</code></pre> <p>Now, in that $_POST will also be other types of fields, like e.g. <code>F1MobilePhone</code> or whatever, that require different validations and different messages. So for each of those fields, you need to find out what type it is, to determine what kind of message to enter into your validation function. One option would be (this goes within the <code>foreach</code> statement):</p> <pre><code>if (strstr($key,'FirstName')) { // add a validation rule for the FirstName type } if (strstr($key,'Somethingelse')) { // etc }</code></pre> <p>I don't know how your validation script works, but I'm guessing you're doing something like this:</p> <pre><code>// import the validation library require("validation.php"); $rules = array(); // stores the validation rules // All Enrollee Rules $rules[] = "required,Num1FirstName,The First Name field is required."; // Call on the validation function $result = validate($rules);</code></pre> <p>Then you'll probably receive back whether, in this case, "Num1FirstName" was valid or not. Since you use an array <code>$rules</code>, the validator can probably handle multiple lines at once. Simply add more values to the array, but don't add another variable. If you do this:</p> <p><pre><code>$rules[] = "required,Num1FirstName,Num2FirstName,The First Name field is required.";</pre></code></p> <p>..the validator will think that the "Num2FirstName" is the error message, since that's the 3rd value you enter, and it won't know what to do with the actual message which is now 4th value. Try this instead: </p> <p><pre><code>$rules[] = "required,Num1FirstName,The First Name field is required."; $rules[] = "required,Num2FirstName,The First Name field is required.";</pre></code></p> <p>So bringing that all together gives you this:</p> <pre><code>foreach ($_POST as $key => $value) { // first $key will be "F1FirstName" // first $value will be "John" // second $key will be "F2FirstName" // second $value will be "Jane" if (strstr($key,'FirstName')) { $rules[] = "required,$key,The First Name field is required."; } if (strstr($key,'Somethingelse')) { $rules[] = "required,$key,Some other message for this type of field."; } } // call on your validate function and feed it the $rules array you've created</code></pre> <p>Adding JavaScript to (pre-)validate is a whole different story. In the end you still need to validate it in PHP because, like you said, the JS check can easily be bypassed. So I guess fixing this first might be best.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload