PHP Laminas DoctrineObjectInputFilter Mendapat nilai sifat lain dalam penapis input panggil balik
P粉883278265
P粉883278265 2024-03-28 17:17:08
0
1
453

Saya menggunakan Laminas DoctrineObjectInputFilter 并希望获取回调输入过滤器中其他属性的值,就像此代码位于 Filter 类的 init 函数中,该类扩展了 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;
                    },
                ],
            ],
        ],
    ]);

Disemak $this->getRawValues() tetapi ia mengembalikan batal untuk semua input.

P粉883278265
P粉883278265

membalas semua(1)
P粉842215006

Agak lewat, tetapi saya rasa anda sedang mencari $context。由于 name 的值位于同一个 InputFilter 实例中,因此您只需在回调函数中使用 $context Itu sahaja.

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'];
                            ...
                        },
                    ],
                ],
            ],
        ]);
    }
}
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan