Warning: Undefined array key "HTTPS" in include() (line 29 of /opt/www/fdv2/drupal.stage/web/sites/default/settings.local.php).
include('/opt/www/fdv2/drupal.stage/web/sites/default/settings.local.php') (Line: 4)
require('/opt/www/fdv2/drupal.stage/web/sites/drupal001.net/settings.php') (Line: 145)
Drupal\Core\Site\Settings::initialize('/opt/www/fdv2/drupal.stage/web', 'sites/drupal001.net', Object) (Line: 1099)
Drupal\Core\DrupalKernel->initializeSettings(Object) (Line: 701)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Drupal 8里面如何给Entity的field增加一个自定义的validate? - Drupal大学 - Drupal问答论坛
首页 / 帖子
Drupal 8里面如何给Entity的field增加一个自定义的validate?

想增加一个validation去验证字段,如何处理?

1个答案
老葛
发布于:2018-03-02 18:13

这个验证叫Constraint,有两种添加验证的时机,一个是在创建的时候,一个是已经创建了。
比如:

$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setDescription(t('The name of the feed (or the name of the website providing the feed).'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -5,
))
->setDisplayConfigurable('form', TRUE)
->addConstraint('FeedTitle', []);

如果要自定义Constraint的时候,要定义个plugin,通过注解的方式声明,添加到

src/Plugin/Validation/Constraint

目录下,命名空间用如下方式:

Drupal\your_module\Plugin\Validation\Constraint

具体参考:
https://www.drupalwatchdog.com/volume-5/issue-2/introducing-drupal-8s-entity-validation-api