我試圖使用外部yml規則集(不在Symfony項目中)使用Symfony2的驗證組件(如2.1分支)驗證導入的csv文件中的數據:使用Symfony驗證組件來驗證具有動態屬性的對象
use Symfony\Component\Validator\Validation;
$builder = Validation::createValidatorBuilder();
$builder->addYamlMapping('rules.yml');
$validator = $builder->getValidator();
$row = (object)array('name' => 'foo');
$violations = $validator->validate($row);
這是我的YML文件:
stdClass:
properties:
name:
- MinLength: 10
現在有,它似乎並不可能與Validator組件來驗證具有動態特性(如stdClass
或任何其他類對象的問題與魔術獲得者d setters)。
當我運行代碼,我得到一個消息說:
[Symfony\Component\Validator\Exception\ValidatorException]
Property forename does not exist in class stdClass
這是由於在突出顯示的行的PropertyMetaclass.php其中存在通過使用property_exists()
檢查這顯然因爲它是檢查對不工作類而不是對象。
有誰知道我如何讓Validator使用具有動態屬性的對象?
我更新了代碼以顯示我最新的symfony2.1工作。 – acme