Skip to content

Support overriding Superform::Rails::Form namespace #52

@mvkampen

Description

@mvkampen

With the namespaces you can easily support a different name, but not so much the case with a Form. In the Form initialize method it uses the key method that looks up the param_key of the model by default, but there is no way to override this.

A use case for this is single table inheritance, when I want to create a form that supports either type, this is how the code already was.
In Rails you can easily override the namespace, and most FormBuilders seem to. I think this would make a great addition here as well,
for now it can only be solvedt by overriding the key method of my custom form.

# Rails
form_for(model, as: :node)
form_with(model:, scope: :node)

Here you can see how scope precedes the model param_key lookup.
https://github.com/rails/rails/blob/1d1790b04d3930d65c2436bbe1d381af821d0ef4/actionview/lib/action_view/helpers/form_helper.rb#L771

I make the suggestion to add a paramter to give control over this from external scope, since its becomes the key of the root namespace and is a core concept i chose to keep it :namespace. But I could imagine others would prefer :root, or maybe looking for familiarity with rails :scope.

Superform::Rails::Form.new(model, namespace: :node)
class Node < ApplicationRecord; end
class Leaf < Node; end

class NodeController < ApplicationController
  def node_params   
    params.require('node').permit(...)
  end
end

In this case one model param_key will give 'node' and the other 'leaf', but in the controller i want to depend on 'node' being the static namespace when dealing with parameters.

Related code of current Form implementation:

@namespace = Namespace.root(key, object: model, field_class: self.class::Field)

def key
  @model.model_name.param_key
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions