PHP code example of thant / input-filter

1. Go to this page and download the library: Download thant/input-filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thant / input-filter example snippets



use Thant\Helpers\InputFilter;

$inputs = array(
            'field1' => ' 100 ',
            'field2' => '[email protected]',
            'field3' => '4400-129-2210'
          );
                          
$sanitizer = new InputFilter();
$sanitizer->setInputs($inputs)
          ->setRequired('field1')
          ->filter('field1', array(
              FILTER_VALIDATE_INT
          ))
          ->setRequired('field2')
          ->filter('field2', array(
              FILTER_VALIDATE_EMAIL
          ), 'Valid email ->getErrors(); //TODO do anything you want with error list failed for validation and filtering
}
else
{
  $data = $santizer->getClean(); //get the cleaned data
  $field1 = $data['field1'];
  $field2 = $data['field2'];
  $field3 = $data['field3'];
}



use Thant\Helpers\InputFilter\IFilterCallback;
class MyCustomFilterCallback implements IFilterCallback
{
	
	public function filter($val)
	{
		//return false if validation or filtering fails.
		//Otherwise you can return a sanitized value back.
	}

}



use Thant\Helpers\InputFilter;
use Thant\Helpers\InputFilter\DateTimeFilterCallback;

$inputs = array(
              'dateOfBirth' => '01/01/1990',
              'appointmentDate' => '06/01/2017'
            );
                            
$sanitizer = new InputFilter();
$sanitizer->setInputs($inputs)
          ->setRequired('dateOfBirth', 'Date of birth is han today date')
          ->setRequired('appointmentDate')
          ->filter('appointmentDate', array(
            FILTER_CALLBACK => new DateTimeFilterCallback([
              'minDate' => date('m/d/Y'), //appointment date should be today or in the future
              'format' => 'Y-m-d'
            ])
          ), 'Appointment date must be a valid date starting from today')
          ->sanitize();
          
if($santizer->hasErrors())
{
  $errors = $santizer->getErrors(); // do input validation error handling 
}
else
{
  $data = $sanitizer->getClean();
  $dateOfBirth = $data['dateOfBirth']; //should be '1990-01-01'
  $appointmentDate = $data['appointmentDate']; //should be '2017-06-01'
}

$sanitizer = new InputFilter();
$sanitizer->filter('field1', array(
	FILTER_CALLBACK => [
		'options' => function($val)
		{
			//TODO check the $val, return false if filtering/validation fails. Otherwise, return the sanitized value
		}
	]
));