Validation

Validating for correct inputs is a crucial part of setting up an experiment. This is necessary for automated data analysis or to conduct sessions without problems. How to validate given data depends on the requirements for the inputs and this is why there is no general solution for data validation in forms.

To stick with the example given in the section above the requirements for a correct input are valid integers between 1 and 10, including the boundaries. The validation is processed in the process tab of the step. By using the request API the submitted parameters are accessed and saved into an array. Afterwards each input is validated individually by checking, whether the input is not empty and still including anything else but numbers. In addition we check, if the input is smaller than 0 or larger than 10 and we make sure that the input is not empty

$inputArray = array();
$view = $api->get('view');

for ($i = 1; $i <= 11; $i++)
{
  $inputArray[] = $requestApi->getParam('nr' .  $i, '');
}

$variableApi->setPSL('array', $inputArray);

foreach ($inputArray as $input)
{
  if((!empty($input) && !ctype_digit($input)) || $input < 0 || $input > 10 || empty($input))
  {
    $view->set('message', 'At least one of your ratings has not been correct.');
    return false;
  }
}

return true;

Furthermore, the information of a wrong input is displayed to a participant, whenever a validation fails. To accomplish that the view API is used, to set the ‘message’ variable to the required error message. The return value false as a result guarantees that the participant is not able to proceed without valid inputs.

<?php if (!empty($message)): ?>
    <div class="alert alert-danger">
      <?= $message; ?>
    </div>
<?php endif; ?>

Wrong Input