auraphp / Aura.Filter

Validate and sanitize arrays and objects.
MIT License
159 stars 33 forks source link

Need help on how to use this? #142

Closed rusrazvan closed 2 years ago

rusrazvan commented 3 years ago

Hello,

Excuse my ignorance, I'm not advanced in PHP. I do not understand how after validating the data you can return the sanitized data to use them in queries like inserting data. For example:

$ok = $filter->validate($username, 'alnum')
   && ! $filter->validate($username, 'int')
   && $filter->validate($username, 'strlenBetween', 6, 10)
   && $filter->sanitize($username, 'string');
if (! $ok) {
    echo "The username is not valid.";
} else {
// insert sanitized data in database
// how to get sanitized values like array or objects properties?
// $username = $sanitized_value['username'];
// or $user->username = $sanitized_value['username'];

$pdo->prepare('INSERT QUERY');
$pdo->execute($username);

}

Thank you in advance!

harikt commented 2 years ago

How you do in PDO is a different subject.

This is how you can apply rules : https://auraphp.com/packages/2.x/Filter/subject-filter.html#4-7-3-2

For the sake of record I am creating a sample code.

<?php
require __DIR__ . '/vendor/autoload.php';

use Aura\Filter\FilterFactory;

$filter_factory = new FilterFactory();

$filter = $filter_factory->newSubjectFilter();

$filter->sanitize('field')->to('alpha');

$subject = [
    'field' => '123abcd'
];

$filter->apply($subject);

var_dump($subject);

You can use the value from the $subject array which will be modified when you apply the filter.