Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The problem with filtering nested properties #343

Open
tuner7777 opened this issue May 26, 2023 · 0 comments
Open

The problem with filtering nested properties #343

tuner7777 opened this issue May 26, 2023 · 0 comments

Comments

@tuner7777
Copy link

tuner7777 commented May 26, 2023

The methods filter and filter_rules do not work with nested properties. For example, the code below, the filter_rules method will ignore filters for nested properties.

$data = [
    'id'       => '1',
    'username' => ' testuser123',
    'email'    => ' mail@example.com',
    'person'   => [
        'name'      => ' <script>log();</script> Test',
        'promocode' => 'test_CODE'
    ]
];

$gump           = new GUMP();
$gump->validation_rules( [
    'id'               => [ 'required', 'integer' ],
    'username'         => [ 'required', 'alpha_numeric' ],
    'email'            => [ 'required', 'valid_email' ],
    'person.name'      => [ 'required' ],
    'person.promocode' => [ 'required', 'alpha_numeric_dash' ],
] );
$gump->filter_rules( [
    'id'               => [ 'whole_number' ],
    'username'         => [ 'trim' ],
    'email'            => [ 'trim' ],
    'person.name'      => [ 'trim', 'sanitize_string' ],
    'person.promocode' => [ 'trim', 'sanitize_string', 'upper_case' ],
] );

$valid_data = $gump->run( $data );

if ( $gump->errors() ) {
    var_dump( $gump->get_errors_array() );
} else {
    var_dump( $valid_data );
}

Result:

array(4) {
  ["id"]=>
  int(1)
  ["username"]=>
  string(11) "testuser123"
  ["email"]=>
  string(16) "mail@example.com"
  ["person"]=>
  array(2) {
    ["name"]=>
    string(29) " <script>log();</script> Test"
    ["promocode"]=>
    string(9) "test_CODE"
  }
}

The expected behavior is that the nested property person.promocode should be transformed to uppercase, and in the person.name property, all spaces and <script> tags should be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant