1
0
Fork 0

validation framework code working

Dieser Commit ist enthalten in:
Faizaan Gagan 2021-01-30 23:16:04 +05:30
Ursprung 5d749fe426
Commit 79e2368b57
13 geänderte Dateien mit 67 neuen und 33 gelöschten Zeilen

Datei anzeigen

@ -1,5 +1,17 @@
import Component from "@ember/component"; import Component from "@ember/component";
Component.extend({ export default Component.extend({
init(){
this._super(...arguments);
if (!this.validations) return;
if (!this.field.validations) {
const validations = {};
this.validations.forEach((validation) => {
validations[validation] = {};
});
this.set('field.validations', EmberObject.create(validations));
}
}
}); });

Datei anzeigen

@ -31,8 +31,8 @@ export default Component.extend(UndoChanges, {
validations(type) { validations(type) {
const applicableToField = []; const applicableToField = [];
for(let validation in wizardSchema.field.validations) { for(let validation in wizardSchema.field.validations) {
if (wizardSchema.field.validations[validation].includes(type)) { if (wizardSchema.field.validations[validation]["type"] === type) {
applicableToField.push(validation); applicableToField.push(validation)
} }
} }

Datei anzeigen

@ -1,15 +1,15 @@
<div> <div>
<label>{{i18n 'admin.wizard.field.validations.header'}}</label> <label>{{i18n 'admin.wizard.field.validations.header'}}</label>
</div> </div>
{{#each-in field.validations as |name props|}}
{{#each validations as |validation|}}
<span class="setting-title"> <span class="setting-title">
{{i18n (concat 'admin.wizard.field.validations.' validation)}} {{input type="checkbox" }} {{i18n (concat 'admin.wizard.field.validations.' name)}}
{{input type="checkbox" checked=props.status }}
</span> </span>
<div> <div>
{{input type="radio"}} :before {{radio-button name=(concat name field.id) value="above" selection=props.position}} {{i18n 'admin.wizard.field.validations.above'}}
</div> </div>
<div> <div>
{{input type="radio"}} :after {{radio-button name=(concat name field.id) value="below" selection=props.position}} {{i18n 'admin.wizard.field.validations.below'}}
</div> </div>
{{/each}} {{/each-in}}

Datei anzeigen

@ -3,7 +3,7 @@ import { observes } from "discourse-common/utils/decorators";
export default Component.extend({ export default Component.extend({
actions:{ actions:{
perform() { perform() {
this.toggleProperty('performValidation'); this.appEvents.trigger('custom-wizard:validate');
}
} }
},
}); });

Datei anzeigen

@ -9,8 +9,11 @@ export default Component.extend({
isValid: null, isValid: null,
isInvalid: not('isValid'), isInvalid: not('isValid'),
layoutName: 'components/validator', // useful for sharing the template with extending components layoutName: 'components/validator', // useful for sharing the template with extending components
@observes('perform') didInsertElement() {
performValidation() { this.appEvents.on('custom-wizard:validate', this, this.validate);
this.validate();
}, },
willDestroyElement() {
this.appEvents.off('custom-wizard:validate', this, this.validate);
}
}); });

Datei anzeigen

@ -1,13 +1,13 @@
{{#if field.validations}} {{#if field.validations}}
{{#each field.validations.top as |validation|}} {{#each-in field.validations.above as |name validation|}}
{{validation.component field=field perform=perform}} {{component validation.component field=field}}
{{/each}} {{/each-in}}
{{yield (hash perform=(action 'perform')) }} {{yield (hash perform=(action 'perform'))}}
{{#each field.validations.bottom as |validation|}} {{#each-in field.validations.below as |name validation|}}
{{validation.component field=field perform=perform}} {{component validation.component field=field}}
{{/each}} {{/each-in}}
{{else}} {{else}}
{{yield}} {{yield}}
{{/if}} {{/if}}

Datei anzeigen

@ -179,6 +179,8 @@ en:
validations: validations:
header: "Realtime Validation Settings" header: "Realtime Validation Settings"
suggested_topics: "Suggested Topics" suggested_topics: "Suggested Topics"
above: "Above"
below: "Below"
type: type:
text: "Text" text: "Text"

Datei anzeigen

@ -105,7 +105,8 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController
:limit, :limit,
:property, :property,
prefill: mapped_params, prefill: mapped_params,
content: mapped_params content: mapped_params,
validations: {},
] ]
], ],
actions: [ actions: [

Datei anzeigen

@ -4,6 +4,7 @@ module CustomWizardFieldExtension
:description, :description,
:image, :image,
:key, :key,
:validations,
:min_length, :min_length,
:max_length, :max_length,
:char_counter, :char_counter,
@ -20,6 +21,7 @@ module CustomWizardFieldExtension
@description = attrs[:description] @description = attrs[:description]
@image = attrs[:image] @image = attrs[:image]
@key = attrs[:key] @key = attrs[:key]
@validations = attrs[:validations]
@min_length = attrs[:min_length] @min_length = attrs[:min_length]
@max_length = attrs[:max_length] @max_length = attrs[:max_length]
@char_counter = attrs[:char_counter] @char_counter = attrs[:char_counter]

Datei anzeigen

@ -158,6 +158,7 @@ class CustomWizard::Builder
params[:description] = field_template['description'] if field_template['description'] params[:description] = field_template['description'] if field_template['description']
params[:image] = field_template['image'] if field_template['image'] params[:image] = field_template['image'] if field_template['image']
params[:key] = field_template['key'] if field_template['key'] params[:key] = field_template['key'] if field_template['key']
params[:validations] = field_template['validations'] if field_template['validations']
params[:min_length] = field_template['min_length'] if field_template['min_length'] params[:min_length] = field_template['min_length'] if field_template['min_length']
params[:max_length] = field_template['max_length'] if field_template['max_length'] params[:max_length] = field_template['max_length'] if field_template['max_length']
params[:char_counter] = field_template['char_counter'] if field_template['char_counter'] params[:char_counter] = field_template['char_counter'] if field_template['char_counter']

Datei anzeigen

@ -5,7 +5,8 @@ class CustomWizard::Field
min_length: nil, min_length: nil,
max_length: nil, max_length: nil,
prefill: nil, prefill: nil,
char_counter: nil char_counter: nil,
validations: nil
}, },
textarea: { textarea: {
min_length: nil, min_length: nil,

Datei anzeigen

@ -1,7 +1,7 @@
class CustomWizard::RealtimeValidation class CustomWizard::RealtimeValidation
cattr_accessor :types cattr_accessor :types
@@types ||= { @@types ||= {
suggested_topics: [:text] suggested_topics: {type: :text, component: "alpha-validator"}
} }
end end

Datei anzeigen

@ -8,6 +8,7 @@ class CustomWizard::FieldSerializer < ::WizardFieldSerializer
:limit, :limit,
:property, :property,
:content, :content,
:validations,
:max_length, :max_length,
:char_counter, :char_counter,
:number :number
@ -58,6 +59,17 @@ class CustomWizard::FieldSerializer < ::WizardFieldSerializer
object.choices.present? object.choices.present?
end end
def validations
validations = {}
object.validations&.each do |name, props|
next unless props["status"]
validations[props["position"]] ||= {}
validations[props["position"]][name] = props.merge CustomWizard::RealtimeValidation.types[name.to_sym]
end
validations
end
def max_length def max_length
object.max_length object.max_length
end end