PHP Laminas DoctrineObjectInputFilter Gets the value of other properties in the callback input filter
P粉883278265
P粉883278265 2024-03-28 17:17:08
0
1
457

I am using Laminas DoctrineObjectInputFilter and want to get the value of other properties in the callback input filter like this code is in the init function of the 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;
                    },
                ],
            ],
        ],
    ]);

Checked $this->getRawValues() but it returns null for all inputs.

P粉883278265
P粉883278265

reply all(1)
P粉842215006

A bit late, but I guess you are searching for $context. Since the value of name is in the same InputFilter instance, you only need to use $context in the callback function.

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'];
                            ...
                        },
                    ],
                ],
            ],
        ]);
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template