Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce option validation for all backpex fields #661

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

pehbehbeh
Copy link
Member

@pehbehbeh pehbehbeh commented Nov 8, 2024

  • change usage of Backpex.Field when building custom fields
  • add config schemas for all core backpex fields
  • validate config schema of each field defined in live resources

@pehbehbeh pehbehbeh added the feature New feature label Nov 8, 2024
Base automatically changed from feature/refactor-liveresource-using to develop November 14, 2024 17:23
demo/lib/demo_web/live/short_link_live.ex Show resolved Hide resolved
type: {:fun, 1}
],
custom_alias: [
doc: "A custom alias for the field.",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Flo0807 Feel free to suggest further information here (and for other options).

select: dynamic([user: u], fragment("concat(?, ' ', ?)", u.first_name, u.last_name)),
}
""",
type: {:struct, Ecto.Query.DynamicExpr}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the following warning when generating the docs:

Generating docs...
     warning: documentation references module "Ecto.Query.DynamicExpr" but it is hidden
     │
 134 │   @callback render_index_form(assigns :: map()) :: %Phoenix.LiveView.Rendered{}
     │   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     │
     └─ lib/backpex/field.ex:134: Backpex.Field (module)

But maybe thats not a problem?

Copy link
Collaborator

@Flo0807 Flo0807 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also noticed that the placeholder is not used anymore (?) I guess, its because of this MR: #658

],
prompt: [
doc: "The text to be displayed when no option is selected or function that receives the assigns.",
type: :string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prompt may also be a function with arity 1.

@config_schema [
options: [
doc: "List of options or function that receives the assigns.",
type: {:or, [:keyword_list, {:fun, 1}]},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options may also be a list of tuples, e.g. [{"Admin", "admin"}]. I think a list of strings can also be used. In the future, we may also have to support groups (like described here: https://hexdocs.pm/phoenix_html/Phoenix.HTML.Form.html#options_for_select/2)

Maybe we just use {:list, :any} instead of :keyword_list for now?

Otherwise, we should allow all options that are allowed for options_for_select/2. See https://github.com/phoenixframework/phoenix_html/blob/v4.1.1/lib/phoenix_html/form.ex#L303

@@ -1,17 +1,33 @@
# credo:disable-for-this-file Credo.Check.Design.DuplicatedCode
defmodule Backpex.Fields.DateTime do
@default_format "%Y-%m-%d %I:%M %p"
@config_schema [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly (boolean or function of arity 1) is missing

@@ -1,17 +1,33 @@
# credo:disable-for-this-file Credo.Check.Design.DuplicatedCode
defmodule Backpex.Fields.Date do
@default_format "%Y-%m-%d"
@config_schema [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly (boolean or function of arity 1) is missing

@@ -1,15 +1,29 @@
defmodule Backpex.Fields.Number do
@config_schema [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly (boolean or function of arity 1) is missing

Define wether this field should be editable on the index view. Also see the
[index edit](/guides/authorization/index-edit.md) guide.
""",
type: :boolean
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index_editable can also be a function of arity 1

],
prompt: [
doc: "The text to be displayed when no option is selected or function that receives the assigns.",
type: :string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prompt can also be a function


The default value is `"Select options..."`.
""",
type: :string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prompt can also be a function with arity 1

@config_schema [
options: [
doc: "List of options or function that receives the assigns.",
type: {:or, [:keyword_list, {:fun, 1}]},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issues as with Backpex.Fields.Select options

],
prompt: [
doc: "The text to be displayed when no option is selected or function that receives the assigns.",
type: :string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prompt can also be a function of arity 1

@Flo0807
Copy link
Collaborator

Flo0807 commented Nov 19, 2024

Before this MR, we allowed throttle to be a string. We should either add string type to throttle or adapt the throttle function in Backpex.Field accordingly. If we stop supporting it, it might be a good idea to add it to the upgrade guide.

],
accept: [
doc: "Required filetypes that will be accepted.",
type: {:list, :string}
Copy link
Collaborator

@Flo0807 Flo0807 Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc: "Required filetypes that will be accepted.",
type: {:list, :string}
],
max_entries: [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a default of 1 for max_entries?

* `:meta` - The upload meta.
* `:entry` - The upload entry.
@config_schema [
upload_key: [
Copy link
Collaborator

@Flo0807 Flo0807 Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LiveView also allows upload_key (the name passed to allow_upload/3) to be of type binary, but I guess this change would break some functionality in Backpex.

See https://github.com/phoenixframework/phoenix_live_view/blob/v0.20.17/lib/phoenix_live_view/upload.ex#L12

@Flo0807
Copy link
Collaborator

Flo0807 commented Nov 19, 2024

We don't use validated fields for:

  • Edit view (see apply_action(socket, :edit) in live_resource.ex)
  • Resource actions (see apply_action(socket, :resource_action) in form_component.ex)
  • Item Actions (see assign_fields(%{assigns: %{action_to_confirm: action_to_confirm}} = socket) in form_component.ex)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants