Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-22 17:30:29 +01:00
Merge pull request #100 from paviliondev/step-conditionality-multiple
FIX: allow setting multiple conditions for entering a step
Dieser Commit ist enthalten in:
Commit
7d38337c84
3 geänderte Dateien mit 92 neuen und 16 gelöschten Zeilen
|
@ -192,11 +192,16 @@ class CustomWizard::Builder
|
||||||
|
|
||||||
def check_condition(template)
|
def check_condition(template)
|
||||||
if template['condition'].present?
|
if template['condition'].present?
|
||||||
CustomWizard::Mapper.new(
|
result = CustomWizard::Mapper.new(
|
||||||
inputs: template['condition'],
|
inputs: template['condition'],
|
||||||
user: @wizard.user,
|
user: @wizard.user,
|
||||||
data: @wizard.current_submission
|
data: @wizard.current_submission,
|
||||||
|
opts: {
|
||||||
|
multiple: true
|
||||||
|
}
|
||||||
).perform
|
).perform
|
||||||
|
|
||||||
|
result.any?
|
||||||
else
|
else
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
|
@ -130,21 +130,63 @@ describe CustomWizard::Mapper do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "validates valid data" do
|
context "conditional validation" do
|
||||||
expect(CustomWizard::Mapper.new(
|
it "validates valid data" do
|
||||||
inputs: inputs['validation'],
|
expect(CustomWizard::Mapper.new(
|
||||||
data: data,
|
inputs: inputs['validation'],
|
||||||
user: user1
|
data: data,
|
||||||
).perform).to eq(true)
|
user: user1
|
||||||
end
|
).perform).to eq(true)
|
||||||
|
end
|
||||||
|
|
||||||
it "does not validate invalid data" do
|
it "does not validate invalid data" do
|
||||||
data["input_2"] = "value 3"
|
data["input_2"] = "value 3"
|
||||||
expect(CustomWizard::Mapper.new(
|
expect(CustomWizard::Mapper.new(
|
||||||
inputs: inputs['validation'],
|
inputs: inputs['validation'],
|
||||||
data: data,
|
data: data,
|
||||||
user: user1
|
user: user1
|
||||||
).perform).to eq(false)
|
).perform).to eq(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "using or condition" do
|
||||||
|
it "validates the data when all of the conditions are met" do
|
||||||
|
expect(CustomWizard::Mapper.new(
|
||||||
|
inputs: inputs['validation_multiple_pairs'],
|
||||||
|
data: data,
|
||||||
|
user: user1,
|
||||||
|
opts: {
|
||||||
|
multiple: true
|
||||||
|
}
|
||||||
|
).perform.any?).to eq(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "validates the data when one of the conditions are met" do
|
||||||
|
custom_data = data.dup
|
||||||
|
custom_data['input_1'] = 'value 3'
|
||||||
|
expect(CustomWizard::Mapper.new(
|
||||||
|
inputs: inputs['validation_multiple_pairs'],
|
||||||
|
data: custom_data,
|
||||||
|
user: user1,
|
||||||
|
opts: {
|
||||||
|
multiple: true
|
||||||
|
}
|
||||||
|
).perform.any?).to eq(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "doesn't validate the data when none of the conditions are met" do
|
||||||
|
custom_data = data.dup
|
||||||
|
custom_data['input_1'] = 'value 3'
|
||||||
|
custom_data['input_2'] = 'value 4'
|
||||||
|
expect(CustomWizard::Mapper.new(
|
||||||
|
inputs: inputs['validation_multiple_pairs'],
|
||||||
|
data: custom_data,
|
||||||
|
user: user1,
|
||||||
|
opts: {
|
||||||
|
multiple: true
|
||||||
|
}
|
||||||
|
).perform.any?).to eq(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "maps text fields" do
|
it "maps text fields" do
|
||||||
|
|
29
spec/fixtures/mapper/inputs.json
gevendort
29
spec/fixtures/mapper/inputs.json
gevendort
|
@ -260,5 +260,34 @@
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"validation_multiple_pairs": [
|
||||||
|
{
|
||||||
|
"type": "validation",
|
||||||
|
"pairs": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"key": "input_1",
|
||||||
|
"key_type": "wizard_field",
|
||||||
|
"value": "value 1",
|
||||||
|
"value_type": "text",
|
||||||
|
"connector": "equal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "validation",
|
||||||
|
"connector": "or",
|
||||||
|
"pairs": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"key": "input_2",
|
||||||
|
"key_type": "wizard_field",
|
||||||
|
"value": "value 2",
|
||||||
|
"value_type": "text",
|
||||||
|
"connector": "equal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
Laden …
In neuem Issue referenzieren