0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-09-19 15:21:11 +02:00

Add custom field validators

Dieser Commit ist enthalten in:
Angus McLeod 2018-08-19 10:34:20 +10:00
Ursprung ac9ed0ef2f
Commit b6c26b43a7

Datei anzeigen

@ -25,6 +25,19 @@ class CustomWizard::Builder
@sorted_handlers.sort_by! { |h| -h[:priority] }
end
def self.sorted_field_validators
@sorted_field_validators ||= []
end
def self.field_validators
sorted_field_validators.map { |h| { type: h[:type], block: h[:block] } }
end
def self.add_field_validator(priority = 0, type, &block)
sorted_field_validators << { priority: priority, type: type, block: block }
@sorted_field_validators.sort_by! { |h| -h[:priority] }
end
USER_FIELDS = ['name', 'username', 'email', 'date_of_birth', 'title', 'locale']
PROFILE_FIELDS = ['location', 'website', 'bio_raw', 'profile_background', 'card_background']
@ -60,7 +73,7 @@ class CustomWizard::Builder
if step_template['fields'] && step_template['fields'].length
step_template['fields'].each do |field|
validate_field(field, updater) if field['type'] != 'text-only'
validate_field(field, updater, step_template) if field['type'] != 'text-only'
end
end
@ -209,7 +222,7 @@ class CustomWizard::Builder
end
end
def validate_field(field, updater)
def validate_field(field, updater, step_template)
value = updater.fields[field['id']]
min_length = field['min_length']
@ -222,6 +235,12 @@ class CustomWizard::Builder
if field['type'] === 'checkbox'
updater.fields[field['id']] = standardise_boolean(value)
end
CustomWizard::Builder.field_validators.each do |validator|
if field['type'] === validator[:type]
validator[:block].call(field, updater, step_template)
end
end
end
def standardise_boolean(value)