# 如何在插件中使用我的验证器？

**URL:** https://meta.discourse.org/t/how-do-i-use-my-validator-in-the-plugin/140839
**Category:** Development
**Created:** [2020年二月6日 10:20 UTC](https://meta.discourse.org/t/how-do-i-use-my-validator-in-the-plugin/140839 "2020-02-06T10:20:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Dev\_Work](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dev_work/32/124200_2.png) [@Dev\_Work](https://meta.discourse.org/u/Dev_Work)
#### Post date: [2020年二月6日 10:20 UTC](https://meta.discourse.org/t/how-do-i-use-my-validator-in-the-plugin/140839/1 "2020-02-06T10:20:16Z")

</div>

请各位帮帮我  
如何在插件中使用我的验证器？

我在插件中添加了一个文件

```plaintext
lib/validators/plugin_ldap_setting_validator.rb

```

在 settings.yml 中使用

```plaintext
  forum_ldap_validation:
    default: 'system'
    validator: 'PluginLdapSettingValidator'

```

在 plugin.rb 中加载文件

```plaintext
load File.expand_path('../lib/validators/plugin_ldap_setting_validator.rb', __FILE__ )

```

出现错误

```plaintext
! Unable to load application: NoMethodError: undefined method `lazy' for nil:NilClass
bundler: failed to load command: puma (/home/smart/.rbenv/versions/2.6.5/bin/puma)
NoMethodError: undefined method `lazy' for nil:NilClass
  /home/smart/discourse/lib/plugin_initialization_guard.rb:9:in `rescue in plugin_initialization_guard'
  /home/smart/discourse/lib/plugin_initialization_guard.rb:3:in `plugin_initialization_guard'
  /home/smart/discourse/config/application.rb:269:in `<class:Application>'
  /home/smart/discourse/config/application.rb:59:in `<module:Discourse>'
  /home/smart/discourse/config/application.rb:58:in `<top (required)>'
  /home/smart/discourse/config/environment.rb:4:in `require'
  /home/smart/discourse/config/environment.rb:4:in `<top (required)>'
  config.ru:6:in `require'
  config.ru:6:in `block in <main>'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.8/lib/rack/builder.rb:55:in `instance_eval'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.8/lib/rack/builder.rb:55:in `initialize'
  config.ru:in `new'
  config.ru:in `<main>'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.8/lib/rack/builder.rb:49:in `eval'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.8/lib/rack/builder.rb:49:in `new_from_string'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rack-2.0.8/lib/rack/builder.rb:40:in `parse_file'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/configuration.rb:321:in `load_rackup'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/configuration.rb:246:in `app'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/runner.rb:155:in `load_and_bind'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/single.rb:98:in `run'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-4.3.1/lib/puma/launcher.rb:172:in `run'
  /home/smart/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/puma-

```

---

<div class="post-metadata">

### Author: ![Dev\_Work](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dev_work/32/124200_2.png) [@Dev\_Work](https://meta.discourse.org/u/Dev_Work)
#### Post date: [2020年二月6日 10:20 UTC](https://meta.discourse.org/t/how-do-i-use-my-validator-in-the-plugin/140839/2 "2020-02-06T10:20:37Z")

</div>

> [@Dev\_Work](#):
>
> ```plaintext
> 
> ```

那里将 key 设为 ‘validator’

甚至这样的示例也能生效：

```plaintext
plugins:
  my_custom_username_validation:
    default: 'system'
    validator: 'UsernameSettingValidator'

```

> [@Developing Discourse Plugins - Part 3 - Add custom site settings](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-3-custom-settings/31115/23):
>
> I was looking how to create a site setting that validates a username, but found there’s a username type to do this. Here the list of site setting types we have today: email username integer regex string list enum Also, you can do custom validations for your plugin settings, it accepts a validator field that expects a Ruby class. You use it like this. plugins: my\_custom\_username\_validation: default: 'system' validator: 'UsernameSettingValidator' You can check the [UsernameSettingVal…](https://github.com/discourse/discourse/blob/master/lib/validators/username_setting_validator.rb)

> <https://github.com/discourse/discourse/blob/main/lib/validators/username_setting_validator.rb>

---

<div class="post-metadata">

### Author: ![Dev\_Work](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dev_work/32/124200_2.png) [@Dev\_Work](https://meta.discourse.org/u/Dev_Work)
#### Post date: [2020年二月8日 14:35 UTC](https://meta.discourse.org/t/how-do-i-use-my-validator-in-the-plugin/140839/3 "2020-02-08T14:35:45Z")

</div>

将文件放置在 `discourse/lib/validators/ldap_setting_validator.rb` 后，问题已解决：

```plaintext
 forum_ldap_validation:
    default: 'system'
    validator: 'LdapSettingValidator'   

```

 ![2020-02-08_173514](https://global.discourse-cdn.com/meta/original/3X/8/b/8becf761bd7776f0e6b415a62c9c1dcfa8690677.png)

如何使该代码在插件中生效？

---

<div class="post-metadata">

### Author: ![Dev\_Work](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dev_work/32/124200_2.png) [@Dev\_Work](https://meta.discourse.org/u/Dev_Work)
#### Post date: [2020年二月8日 15:27 UTC](https://meta.discourse.org/t/how-do-i-use-my-validator-in-the-plugin/140839/4 "2020-02-08T15:27:29Z")

</div>

太好了，成功了

plugin.rb 中的代码

```plaintext
def forum_require(path)
  require Rails.root.join('plugins', 'forum', 'app', path).to_s
end

after_initialize do

  forum_require 'lib/ldap_setting_validator'
//代码
end

```

ldap\_setting\_validator.rb → 路径 → myplugin/app/lib/ldap\_setting\_validator.rb

感谢提供的示例 → [babble/plugin.rb at master · gdpelican/babble · GitHub](https://github.com/gdpelican/babble/blob/master/plugin.rb#L17-L52)

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2020年六月5日 20:41 UTC](https://meta.discourse.org/t/how-do-i-use-my-validator-in-the-plugin/140839/5 "2020-06-05T20:41:53Z")

</div>

> [@Dev\_Work](#):
>
> 我如何在插件中使用我的验证器？

我昨天一直在寻找如何操作的示例。我在 Discourse 仓库中找到的示例位于此处：[https://github.com/discourse/discourse-chat-integration。你会看到，在该插件的](https://github.com/discourse/discourse-chat-integration%E3%80%82%E4%BD%A0%E4%BC%9A%E7%9C%8B%E5%88%B0%EF%BC%8C%E5%9C%A8%E8%AF%A5%E6%8F%92%E4%BB%B6%E7%9A%84) `plugin.rb` 文件中，验证器类是通过以下命令引入的：

```plaintext
require_relative "lib/discourse_chat/provider/slack/slack_enabled_setting_validator"

```

为了让 Discourse 找到该验证器，它需要在调用 `after_initialize` 之前被包含进来。
