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

Channel Form: Unexpected default value behavior as placeholder #154

Closed
oxzi opened this issue Jan 12, 2024 · 6 comments · Fixed by #175
Closed

Channel Form: Unexpected default value behavior as placeholder #154

oxzi opened this issue Jan 12, 2024 · 6 comments · Fixed by #175
Assignees
Labels
enhancement New feature or improvement
Milestone

Comments

@oxzi
Copy link
Member

oxzi commented Jan 12, 2024

Describing a channel holding a configuration option with a non empty default field results in this default being rendered as the input's placeholder (<input placeholder="…">). However, when creating or editing this channel through web, this placeholder value will not be reflected in the database and is not used as a default.

As a concrete example, let's start with the following Go snippet:

func (ch *Webhook) GetInfo() *plugin.Info {
	elements := []*plugin.ConfigOption{
		// [ . . . ]
		{
			Name: "request_body_template",
			Type: "string",
			Label: map[string]string{
				"en_US": "Request Body Template",
				"de_DE": "Anfragedaten-Template",
			},
			Help: map[string]string{
				"en_US": "Go template applied to the current plugin.NotificationRequest to create an request body.",
				"de_DE": "Go-Template über das zu verarbeitende plugin.NotificationRequest zum Erzeugen der mitgesendeten Anfragedaten.",
			},
			// Default as a minimal working example to send a JSON object.
			Default: `{"id": {{.Incident.Id}}, "type": "{{.Event.Type}}", "name": "{{.Object.Name}}", "severity": "{{.Incident.Severity}}", "url": "{{.Incident.Url}}"}`,
		},
	}

	// [ . . . ]
}

This results in the following available_channel_type entry:

notifications=# select config_attrs from available_channel_type where type = 'webhook';
-[ RECORD 1 ]-
config_attrs | [{…},{"name":"request_body_template","type":"string","label":{"de_DE":"Anfragedaten-Template","en_US":"Request Body Template"},"help":{"de_DE":"Go-Template über das zu verarbeitende plugin.NotificationRequest zum Erzeugen der mitgesendeten Anfragedaten.","en_US":"Go template applied to the current plugin.NotificationRequest to create an request body."},"default":"{\"id\": {{.Incident.Id}}, \"type\": \"{{.Event.Type}}\", \"name\": \"{{.Object.Name}}\", \"severity\": \"{{.Incident.Severity}}\", \"url\": \"{{.Incident.Url}}\"}","min":null,"max":null}]

Within the web, the following output will be generated:
2024-01-12-100008_871x316_scrot

<div class="control-group">
  <div class="control-label-group">
    <label>Request Body Template</label>
  </div>
  <input placeholder="{&quot;id&quot;: {{.Incident.Id}}, &quot;type&quot;: &quot;{{.Event.Type}}&quot;, &quot;name&quot;: &quot;{{.Object.Name}}&quot;, &quot;severity&quot;: &quot;{{.Incident.Severity}}&quot;, &quot;url&quot;: &quot;{{.Incident.Url}}&quot;}" name="config[request_body_template]" type="text">
  <i class="icon fa-info-circle control-info fa" role="image" title="Go template applied to the current plugin.NotificationRequest to create an request body."></i>
</div>

However, when now saving this entry with the placeholder value, it will be written as null to the database:

notifications=# select * from channel;
-[ RECORD 1 ]-------------------------------------------------------------------------------------------------------
id     | 1
name   | web
type   | webhook
config | {"method":"POST","url_template":"http:\/\/http-echo:8000\/{{.Incident.Id}}\/","request_body_template":null}

I would expect a default named field to create a default value which will be used when no other value was entered.

@nilmerg
Copy link
Member

nilmerg commented Jan 12, 2024

The reason for this behavior is that a default may change. If a default is stored that way, it never changes automatically. If the default is not applied by the channel plugin, this needs to be fixed.

@oxzi
Copy link
Member Author

oxzi commented Jan 12, 2024

The reason for this behavior is that a default may change. If a default is stored that way, it never changes automatically. If the default is not applied by the channel plugin, this needs to be fixed.

Fair enough. But then it should be reflected in the database that the default value should be used. This would also bring up the question how to distinct an empty or null value from the wish to use the default.

@nilmerg
Copy link
Member

nilmerg commented Jan 12, 2024

Usually, a non existing value for a config option means to use the default. In this case, NULL. But yeah, it is currently not possible to store e.g. the empty string. We could of course change this, so that the default is not a placeholder, but is just not stored if not adjusted.

@nilmerg nilmerg added the enhancement New feature or improvement label Apr 9, 2024
@nilmerg nilmerg linked a pull request Apr 12, 2024 that will close this issue
@nilmerg nilmerg added this to the Beta milestone Apr 12, 2024
@oxzi
Copy link
Member Author

oxzi commented Apr 18, 2024

As I just stumbled about this again: configuring a Default results in populating the input's placeholder field, but one has to manually type the value again in the web dialog when Required: true.

This is a bit frustrating, as I am unable to catch this on the backend by replacing an empty value to the configured default because the web dialog forbids submitting an empty form. However, when I starts typing, the shown placeholder disappears.

2024-04-18-124433_1033x414_scrot

@nilmerg
Copy link
Member

nilmerg commented Apr 18, 2024

Have you seen Icinga/icinga-notifications#172?

@oxzi
Copy link
Member Author

oxzi commented Apr 18, 2024

Have you seen Icinga/icinga-notifications#172?

Now I've seen it. It kinda addresses this very issue.

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

Successfully merging a pull request may close this issue.

3 participants