當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。