Skip to content

Commit 0423ea0

Browse files
committed
validation
1 parent b9aff1c commit 0423ea0

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

docs/admin/templates/extending-templates/dynamic-parameters.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ Using these in conjunction, administrators can build intuitive, reactive forms f
298298

299299
<div class="tabs">
300300

301-
## Hide/show options
301+
### Hide/show options
302302

303303
Using Terraform conditionals and the `count` block, we can allow a checkbox to expose or hide a subsequent parameter.
304304

@@ -333,7 +333,7 @@ data "coder_parameter" "cpu_cores" {
333333
}
334334
```
335335

336-
## Dynamic Defaults
336+
### Dynamic Defaults
337337

338338
For a given parameter, we can influence which option is selected by default based on the selection of another. This allows us to suggest an option dynamically without strict enforcement.
339339

@@ -387,9 +387,57 @@ data "coder_parameter" "ide_selector" {
387387
}
388388
```
389389

390+
## Dynamic Validation
390391

392+
Parameters' validation block can also leverage inputs from other parameters.
391393

392-
## Dynamic Validation
394+
[Try dynamic validation in the Parameter Playground](https://playground.coder.app/parameters/sdbzXxagJ4).
395+
396+
397+
```terraform
398+
data "coder_parameter" "git_repo" {
399+
name = "git_repo"
400+
display_name = "Git repo"
401+
description = "Select a git repo to work on."
402+
order = 1
403+
mutable = true
404+
type = "string"
405+
form_type = "dropdown"
406+
407+
option {
408+
# A Go-heavy repository
409+
name = "coder/coder"
410+
value = "coder/coder"
411+
}
412+
413+
option {
414+
# A python-heavy repository
415+
name = "coder/mlkit"
416+
value = "coder/mlkit"
417+
}
418+
}
419+
420+
data "coder_parameter" "cpu_cores" {
421+
# Only show this parameter if the previous box is selected.
422+
count = data.coder_parameter.show_cpu_cores.value ? 1 : 0
423+
424+
name = "cpu_cores"
425+
display_name = "CPU Cores"
426+
type = "number"
427+
form_type = "slider"
428+
order = 2
429+
430+
# Dynamically set default
431+
default = try(data.coder_parameter.git_repo.value, "") == "coder/mlkit" ? 12 : 6
432+
433+
validation {
434+
min = 1
435+
436+
# Dynamically set max validation
437+
max = try(data.coder_parameter.git_repo.value, "") == "coder/mlkit" ? 16 : 8
438+
}
439+
}
440+
```
393441

394442
## Daisy Chaining
395443

0 commit comments

Comments
 (0)