当前位置: 首页>>代码示例>>PHP>>正文


PHP TypedDataManager::getValidationConstraintManager方法代码示例

本文整理汇总了PHP中Drupal\Core\TypedData\TypedDataManager::getValidationConstraintManager方法的典型用法代码示例。如果您正苦于以下问题:PHP TypedDataManager::getValidationConstraintManager方法的具体用法?PHP TypedDataManager::getValidationConstraintManager怎么用?PHP TypedDataManager::getValidationConstraintManager使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Drupal\Core\TypedData\TypedDataManager的用法示例。


在下文中一共展示了TypedDataManager::getValidationConstraintManager方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: testTypedDataValidation

 /**
  * Tests typed data validation.
  */
 public function testTypedDataValidation()
 {
     $definition = DataDefinition::create('integer')->setConstraints(array('Range' => array('min' => 5)));
     $violations = $this->typedDataManager->create($definition, 10)->validate();
     $this->assertEqual($violations->count(), 0);
     $integer = $this->typedDataManager->create($definition, 1);
     $violations = $integer->validate();
     $this->assertEqual($violations->count(), 1);
     // Test translating violation messages.
     $message = t('This value should be %limit or more.', array('%limit' => 5));
     $this->assertEqual($violations[0]->getMessage(), $message, 'Translated violation message retrieved.');
     $this->assertEqual($violations[0]->getPropertyPath(), '');
     $this->assertIdentical($violations[0]->getRoot(), $integer, 'Root object returned.');
     // Test translating violation messages when pluralization is used.
     $definition = DataDefinition::create('string')->setConstraints(array('Length' => array('min' => 10)));
     $violations = $this->typedDataManager->create($definition, "short")->validate();
     $this->assertEqual($violations->count(), 1);
     $message = t('This value is too short. It should have %limit characters or more.', array('%limit' => 10));
     $this->assertEqual($violations[0]->getMessage(), $message, 'Translated violation message retrieved.');
     // Test having multiple violations.
     $definition = DataDefinition::create('integer')->setConstraints(array('Range' => array('min' => 5), 'Null' => array()));
     $violations = $this->typedDataManager->create($definition, 10)->validate();
     $this->assertEqual($violations->count(), 1);
     $violations = $this->typedDataManager->create($definition, 1)->validate();
     $this->assertEqual($violations->count(), 2);
     // Test validating property containers and make sure the NotNull and Null
     // constraints work with typed data containers.
     $definition = BaseFieldDefinition::create('integer')->setConstraints(array('NotNull' => array()));
     $field_item = $this->typedDataManager->create($definition, array('value' => 10));
     $violations = $field_item->validate();
     $this->assertEqual($violations->count(), 0);
     $field_item = $this->typedDataManager->create($definition, array('value' => 'no integer'));
     $violations = $field_item->validate();
     $this->assertEqual($violations->count(), 1);
     $this->assertEqual($violations[0]->getPropertyPath(), '0.value');
     // Test that the field item may not be empty.
     $field_item = $this->typedDataManager->create($definition);
     $violations = $field_item->validate();
     $this->assertEqual($violations->count(), 1);
     // Test the Null constraint with typed data containers.
     $definition = BaseFieldDefinition::create('float')->setConstraints(array('Null' => array()));
     $field_item = $this->typedDataManager->create($definition, array('value' => 11.5));
     $violations = $field_item->validate();
     $this->assertEqual($violations->count(), 1);
     $field_item = $this->typedDataManager->create($definition);
     $violations = $field_item->validate();
     $this->assertEqual($violations->count(), 0);
     // Test getting constraint definitions by type.
     $definitions = $this->typedDataManager->getValidationConstraintManager()->getDefinitionsByType('entity');
     $this->assertTrue(isset($definitions['EntityType']), 'Constraint plugin found for type entity.');
     $this->assertTrue(isset($definitions['Null']), 'Constraint plugin found for type entity.');
     $this->assertTrue(isset($definitions['NotNull']), 'Constraint plugin found for type entity.');
     $definitions = $this->typedDataManager->getValidationConstraintManager()->getDefinitionsByType('string');
     $this->assertFalse(isset($definitions['EntityType']), 'Constraint plugin not found for type string.');
     $this->assertTrue(isset($definitions['Null']), 'Constraint plugin found for type string.');
     $this->assertTrue(isset($definitions['NotNull']), 'Constraint plugin found for type string.');
     // Test automatic 'required' validation.
     $definition = DataDefinition::create('integer')->setRequired(TRUE);
     $violations = $this->typedDataManager->create($definition)->validate();
     $this->assertEqual($violations->count(), 1);
     $violations = $this->typedDataManager->create($definition, 0)->validate();
     $this->assertEqual($violations->count(), 0);
     // Test validating a list of a values and make sure property paths starting
     // with "0" are created.
     $definition = BaseFieldDefinition::create('integer');
     $violations = $this->typedDataManager->create($definition, array(array('value' => 10)))->validate();
     $this->assertEqual($violations->count(), 0);
     $violations = $this->typedDataManager->create($definition, array(array('value' => 'string')))->validate();
     $this->assertEqual($violations->count(), 1);
     $this->assertEqual($violations[0]->getInvalidValue(), 'string');
     $this->assertIdentical($violations[0]->getPropertyPath(), '0.value');
 }
开发者ID:318io,项目名称:318-io,代码行数:75,代码来源:TypedDataTest.php


注:本文中的Drupal\Core\TypedData\TypedDataManager::getValidationConstraintManager方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。