我想通過靜態回調驗證我的實體。Symfony驗證回調
我能夠使它在Symfony guide之後工作,但有些事情對我而言並不清楚。
public static function validate($object, ExecutionContextInterface $context, $payload)
{
// somehow you have an array of "fake names"
$fakeNames = array(/* ... */);
// check if the name is actually a fake name
if (in_array($object->getFirstName(), $fakeNames)) {
$context->buildViolation('This name sounds totally fake!')
->atPath('firstName')
->addViolation()
;
}
}
當我填充我$fakeNames
數組,但如果我想使其「動態」它工作正常?比方說,我想從參數或數據庫或任何地方選擇該數組。 我該如何從構造函數不工作的時候將東西(比如容器或entityManager)傳遞給這個類,它必須是靜態的?
當然,我的方法可能是完全錯誤的,但我只是使用symfony示例以及在互聯網上發現的幾個其他類似問題,我試圖適應我的情況。