Skip to content

Commit

Permalink
fixing empty search in validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ksierks committed Jan 10, 2025
1 parent 88a8d48 commit 73da5bb
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions app/validators/advanced_search_group_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@
class AdvancedSearchGroupValidator < ActiveModel::Validator
def validate(record)
groups_valid = true
return if empty_search?(record)

record.groups.each_with_index do |group, group_index|
validate_blank_fields(record, group, group_index)
validate_unique_fields(record, group, group_index)
groups_valid = false if record.groups[group_index].errors.any?
end

return if groups_valid

record.errors.add :base, I18n.t('validators.advanced_search_group_validator.base_error')
end

private

def validate_blank_fields(record, group, group_index)
if group.conditions.count == 1
validate_blank_fields_for_condition(record, group, group_index)
elsif group.conditions.any? do |condition|
condition.field.blank? || condition.operator.blank? || condition.value.blank?
end
record.groups[group_index].errors.add :base, I18n.t('validators.advanced_search_group_validator.blank_error')
def empty_search?(record) # rubocop:disable Metrics/AbcSize
if record.groups.length == 1 && record.groups[0].conditions.length == 1 &&
record.groups[0].conditions[0].field.blank? && record.groups[0].conditions[0].operator.blank? &&
record.groups[0].conditions[0].value.blank?
return true
end

false
end

def validate_blank_fields_for_condition(record, group, group_index)
condition = group.conditions.first
unless (condition.field.blank? && condition.operator.blank? && condition.value.blank?) ||
(condition.field.present? && condition.operator.present? && condition.value.present?)
def validate_blank_fields(record, group, group_index)
if group.conditions.any? do |condition|
condition.field.blank? || condition.operator.blank? || condition.value.blank?
end
record.groups[group_index].errors.add :base, I18n.t('validators.advanced_search_group_validator.blank_error')
end
end
Expand Down

0 comments on commit 73da5bb

Please sign in to comment.