@@ -1208,16 +1208,16 @@ Custom Field Options
12081208You may define custom options for the ``field`` macro function
12091209which extend its behavior at the your time model classes are loaded.
12101210
1211- As an example, we will define a ``:required `` option which will add a presence
1211+ As an example, we will define a ``:max_length `` option which will add a length
12121212validator for the field. First, declare the new field option in an initializer,
12131213specifiying its handler function as a block:
12141214
12151215.. code-block:: ruby
12161216
12171217 # in /config/initializers/mongoid_custom_fields.rb
12181218
1219- Mongoid::Fields.option :required do |model, field, value|
1220- model.validates_presence_of field if value
1219+ Mongoid::Fields.option :max_length do |model, field, value|
1220+ model.validates_length_of field.name, maximum: value
12211221 end
12221222
12231223Then, use it your model class:
@@ -1227,7 +1227,7 @@ Then, use it your model class:
12271227 class Person
12281228 include Mongoid::Document
12291229
1230- field :name, type: String, required: true
1230+ field :name, type: String, max_length: 10
12311231 end
12321232
12331233Note that the handler function will be invoked whenever the option is used
0 commit comments