PHP Laminas DoctrineObjectInputFilter get value of other property in Callback input filter

338 views Asked by At

I am working with Laminas DoctrineObjectInputFilter and want to get value of other property in Callback input filter like this code is in init function of Filter class which extends DoctrineObjectInputFilter

// input filter whose value is required
$this->add([     
        'name' => 'name',
        'allow_empty' => false,
        'filters' => []
]);
// Input filter in which I want value of input name
$this->add([
        'name' => 'value',
        'allow_empty' => true,
        'filters' => [
            [
                'name' => 'Callback',
                'options' => [
                    'callback' => function ($value) {
                        $name = // want to get property name value here

                        if (key_exists($name, $this->applicationConfig) && gettype($value) === 'string') {
                            return trim(strip_tags($value));
                          }
                          else {
                              return trim($value);
                          }

                        return $value;
                    },
                ],
            ],
        ],
    ]);

have checked $this->getRawValues() but its returning null for all inputs.

1

There are 1 answers

1
Marcel On

a bit late but I guess you 're searching for $context. Since the value of name is in the same InputFilter instance, you can simply use $context in your callback function.

<?php
declare(strict_types=1);
namespace Marcel\InputFilter;

use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\Filter\ToNull;
use Laminas\InputFilter\InputFilter;
use Laminas\Validator\Callback;

class ExampleInputFilter extends InputFilter 
{
    public function init()
    {
        parent::init();

        $this->add([
            'name' => 'name',
            'required' => true,
            'allow_empty' => false,
            'filters' => [
                [ 'name' => StripTags::class ],
                [ 'name' => StringTrim::class ],
                [
                    'name' => ToNull::class,
                    'options' => [
                        'type' => ToNull::TYPE_STRING,
                    ],
                ],
            ],
        ]);

        $this->add([
            'name' => 'value',
            'required' => true,
            'allow_empty' => true,
            'filters' => [],
            'validators' => [
                [
                    'name' => Callback::class,
                    'options' => [
                        'callback' => function($value, $context) {
                            $name = $context['name'];
                            ...
                        },
                    ],
                ],
            ],
        ]);
    }
}