Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I ended up using a custom solution inspired by the answer of @VolkerK. I should have stated clearer what I wanted. I've update my question with this information.</p> <p>I'm still looking for an easier solution.</p> <p>First, I made some result arrays:</p> <pre><code>// errors to display $errors = array(); // fields to ignore in the validation step $ignore_fields = array(); // return values after validation $returnvalues = array(); </code></pre> <p>I first filtered for required fields:</p> <pre><code>function required($string){ return trim($string) == "" ? false : $string; } $required_errors = array( "firstname" =&gt; "Je moet je voornaam invullen.", "lastname" =&gt; "Je moet je achternaam invullen.", "email" =&gt; "Je moet je e-mailadres invullen." ); $required_filter = array(); foreach($required_errors as $requiredfieldname =&gt; $error){ $required_filter[$requiredfieldname] = array( 'filter' =&gt; FILTER_CALLBACK, 'options' =&gt; "required", 'flags' =&gt; FILTER_NULL_ON_FAILURE ); } $required_filtered = filter_input_array(INPUT_GET | INPUT_POST, $required_filter); foreach($required_errors as $required =&gt; $error){ if(!isset($required_filtered[$required])){ $errors[$required] = $required_errors[$required]; $returnvalues[$required] = ""; $ignore_fields[$required] = "ignored"; // value doesn't matter } } </code></pre> <p>Then check for the empty fields and load them with a standard value</p> <pre><code>// ignore the other fields if they are empty; // the actual form has about 10 values here $maybeempty = array("from", "to", "phonenumber"); $optional_defaultvalue = array( "from" =&gt; 0, "to" =&gt; 0, "phonenumber" =&gt; "", ); $optional_filter = array(); foreach($maybeempty as $field){ $required_filter[$requiredfieldname] = array( 'filter' =&gt; FILTER_CALLBACK, 'options' =&gt; "required", 'flags' =&gt; FILTER_NULL_ON_FAILURE ); } $optional_filtered = filter_input_array(INPUT_GET | INPUT_POST, $required_filter); foreach($maybeempty as $field){ if(!isset($optional_filtered[$field])){ $ignore_fields[$field] = "ignored"; // value doesn't matter $returnvalue[$field] = $optional_defaultvalue[$field]; } } </code></pre> <p>In the final step, I will build a validation array, custom error messages and ignore the ignore fields, to skip empty fields or fields that already had an error:</p> <pre><code> $validity_filter = array( 'email' =&gt; array( 'filter' =&gt; FILTER_VALIDATE_EMAIL, 'flags' =&gt; FILTER_REQUIRE_SCALAR ), 'from' =&gt; array( 'filter' =&gt; FILTER_VALIDATE_INT, 'flags' =&gt; FILTER_REQUIRE_SCALAR, 'options' =&gt; array('min_range' =&gt; 1964, 'max_range' =&gt; 2009)), 'to' =&gt; array( 'filter' =&gt; FILTER_VALIDATE_INT, 'flags' =&gt; FILTER_REQUIRE_SCALAR, 'options' =&gt; array('min_range' =&gt; 1964, 'max_range' =&gt; 2009)) ); // filter all ignored fields $validity_filter = array_diff_key($validity_filter, $ignore_fields); $validity_filtered = filter_input_array(INPUT_GET | INPUT_POST, $required_filter); foreach($validity_filter as $field =&gt; $value){ if($value === false){ // NULL values are checked in a previous step $errors[$field] = $validity_errors[$field]; // show error $returnvalues[$field] = $_REQUEST[$field]; // return original value } else { $returnvalues[$field] = $value; // return filtered value } } // process possible errors and response values </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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