Skip to content

Add support for custom validation contexts in resource creation and update#3014

Open
goosys wants to merge 1 commit intothoughtbot:mainfrom
goosys:feature/validation-context
Open

Add support for custom validation contexts in resource creation and update#3014
goosys wants to merge 1 commit intothoughtbot:mainfrom
goosys:feature/validation-context

Conversation

@goosys
Copy link
Copy Markdown
Contributor

@goosys goosys commented Mar 24, 2026


This pull request introduces support for customizable validation contexts when creating or updating resources in the Administrate controllers. This allows developers to specify different validation scenarios based on user roles or other conditions, providing more flexibility for resource validation logic. Documentation and tests have been updated to reflect and verify this new functionality.

Controller enhancements for validation contexts:

  • Added validation_contexts_on_create and validation_contexts_on_update methods to Administrate::ApplicationController, allowing developers to override and specify custom validation contexts for resource creation and update. The default context is :create or :update based on whether the record is new. (app/controllers/administrate/application_controller.rb)

Documentation updates:

  • Added a new section in docs/customizing_controller_actions.md.

Testing improvements:

  • Added controller specs in spec/controllers/admin/application_controller_spec.rb.

Usage:

# Model
validates :name, presence: true, on: :save_by_regular_admin

# Controller
def validation_contexts_on_create(resource)
  if current_user.super_admin?
    super + [:save_by_super_admin]
  else
    super + [:save_by_regular_admin]
  end
end

def validation_contexts_on_update(resource)
  if current_user.super_admin?
    super + [:save_by_super_admin]
  else
    super + [:save_by_regular_admin]
  end
end

@goosys goosys force-pushed the feature/validation-context branch from bce9735 to 58219f1 Compare March 25, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant