> For the complete documentation index, see [llms.txt](https://docs.devolutions.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.devolutions.net/powershell-universal/it/app/components/inputs/form.md).

# Form

Crei form nelle app di PowerShell Universal che raccolgono, convalidano e inviano dati da caselle di testo, caselle di controllo, elenchi di selezione e altri controlli di input.

{% embed url="<https://youtu.be/o8Bl1NsCc2Y>" %}

I form offrono un modo per raccogliere dati dagli utenti.

I form possono includere qualsiasi tipo di controllo desideri. Questo le consente di personalizzare l'aspetto e di utilizzare qualsiasi controllo di input.

I dati inseriti tramite i controlli di input verranno inviati al blocco di script `OnSubmit` quando il form viene inviato. All'interno del gestore di eventi `OnSubmit`, avrà accesso alla variabile `$EventData` che conterrà proprietà per ciascuno dei campi del form.

Per esempio, se ha due campi, avrà due proprietà su `$EventData`.

```powershell
New-UDForm -Content {
    New-UDTextbox -Id 'txtTextField'
    New-UDCheckbox -Id 'chkCheckbox'
} -OnSubmit {
    Show-UDToast -Message $EventData.txtTextField
    Show-UDToast -Message $EventData.chkCheckbox
}
```

## Controlli supportati

I seguenti controlli di input si integrano automaticamente con un form. I valori impostati all'interno di questi controlli verranno inviati durante la convalida e nel gestore di eventi `OnSubmit`.

* [Autocomplete](/powershell-universal/it/app/components/inputs/automcomplete.md)
* [Checkbox](/powershell-universal/it/app/components/inputs/checkbox.md)
* [Date Picker](/powershell-universal/it/app/components/inputs/date-picker.md)
* [Radio](/powershell-universal/it/app/components/inputs/radio.md)
* [Select](/powershell-universal/it/app/components/inputs/select.md)
* [Slider](/powershell-universal/it/app/components/inputs/slider.md)
* [Switch](/powershell-universal/it/app/components/inputs/switch.md)
* [Textbox](/powershell-universal/it/app/components/inputs/textbox.md)
* [Time Picker](/powershell-universal/it/app/components/inputs/time-picker.md)
* [Transfer List](/powershell-universal/it/app/components/inputs/transfer-list.md)
* [Upload](/powershell-universal/it/app/components/inputs/upload.md)

## Form semplice

I form semplici possono utilizzare input come caselle di testo e caselle di controllo.

```powershell
New-UDForm -Content {
    New-UDTextbox -Id 'txtTextfield'
    New-UDCheckbox -Id 'chkCheckbox'
} -OnSubmit {
    Show-UDToast -Message $EventData.txtTextfield
    Show-UDToast -Message $EventData.chkCheckbox
}
```

## Formattazione di un form

Poiché i form possono utilizzare qualsiasi componente, può usare i componenti di formattazione standard all'interno del form.

```powershell
New-UDForm -Content {

    New-UDRow -Columns {
        New-UDColumn -SmallSize 6 -LargeSize 6 -Content {
            New-UDTextbox -Id 'txtFirstName' -Label 'First Name' 
        }
        New-UDColumn -SmallSize 6 -LargeSize 6 -Content {
            New-UDTextbox -Id 'txtLastName' -Label 'Last Name'
        }
    }

    New-UDTextbox -Id 'txtAddress' -Label 'Address'

    New-UDRow -Columns {
        New-UDColumn -SmallSize 6 -LargeSize 6  -Content {
            New-UDTextbox -Id 'txtState' -Label 'State'
        }
        New-UDColumn -SmallSize 6 -LargeSize 6  -Content {
            New-UDTextbox -Id 'txtZipCode' -Label 'ZIP Code'
        }
    }

} -OnSubmit {
    Show-UDToast -Message $EventData.txtFirstName
    Show-UDToast -Message $EventData.txtLastName
}
```

## Restituire componenti

Quando un form viene inviato, può facoltativamente restituire un altro componente per sostituire il form nella pagina. Può restituire qualsiasi componente di Universal Dashboard. Tutto ciò che deve fare è assicurarsi che il componente venga scritto nella pipeline all'interno del gestore di eventi `OnSubmit`.

```powershell
New-UDForm -Content {
    New-UDTextbox -Id 'txtTextfield'
} -OnSubmit {
    New-UDTypography -Text $EventData.txtTextfield
}
```

## Convalida di un form

La convalida del form può essere effettuata utilizzando il parametro del blocco di script OnValidate.

```powershell
New-UDForm -Content {
    New-UDTextbox -Id 'txtValidateForm'
} -OnValidate {
    $FormContent = $EventData

    if ($FormContent.txtValidateForm -eq $null -or $FormContent.txtValidateForm -eq '') {
        New-UDFormValidationResult -ValidationError "txtValidateForm is required"
    } else {
        New-UDFormValidationResult -Valid
    }
} -OnSubmit {
    Show-UDToast -Message $Body
}
```

## Annullamento di un form

Può definire un gestore di eventi `-OnCancel` da richiamare quando viene premuto il pulsante di annullamento. Questo può essere utilizzato per eseguire azioni come chiudere una finestra modale.

```powershell
New-UDButton -Text 'On Form' -OnClick {
    Show-UDModal -Content {
        New-UDForm -Content {
            New-UDTextbox -Label 'Hello'
        } -OnSubmit {
            Show-UDToast -Message 'Submitted!'
            Hide-UDModal
        } -OnCancel {
            Hide-UDModal
        }
    }
}
```

## Visualizzare l'output senza sostituire il form

Anche se può restituire componenti direttamente da un form, potrebbe volere mantenere il form così che gli utenti possano inserire nuovamente i dati. Per farlo, può utilizzare `Set-UDElement` e un elemento segnaposto di cui impostare il contenuto.

In questo esempio, abbiamo un form vuoto che, una volta inviato, aggiornerà l'elemento `results` con una UDCard.

```powershell
New-UDForm -Content {

} -OnSubmit {
   Set-UDElement -Id 'results' -Content {
      New-UDCard -Content { "Hello " + (Get-Date) }
   }
}

New-UDElement -Id 'results' -Tag 'div'
```

## Form basati su schema

Invece di definire tutto il layout e la logica dei form utilizzando i cmdlet, può anche definire un form basato su una hashtable di schema. Questa versione dei form è basata su [react-jsonschema-form](https://rjsf-team.github.io/react-jsonschema-form/docs/).

### Campi

Definisca campi che accettano tipi string, number, integer, enum e boolean. Questo modifica il tipo di input mostrato.

```powershell
New-UDForm -Schema @{
   title = "Test Form"
   type = "object"
   properties = @{
       name = @{
           type = "string"
       }
       age = @{
           type = "number"
       }
   }
} -OnSubmit {
   # $EventData.name
   # $EventData.age
}
```

### Proprietà obbligatorie

Può utilizzare la proprietà `required` per impostare un elenco di proprietà obbligatorie.

```powershell
New-UDForm -Schema @{
   title = "Test Form"
   type = "object"
   properties = @{
       name = @{
           type = "string"
       }
       age = @{
           type = "number"
       }
   }
   required = @('name')
} -OnSubmit {
   # $EventData.name
   # $EventData.age
}
```

{% hint style="warning" %}
Tenga presente che le proprietà devono essere in minuscolo! Per esempio, deve assicurarsi che le chiavi nella sua hashtable di proprietà siano in minuscolo e che anche l'elenco delle proprietà obbligatorie sia in minuscolo.
{% endhint %}

### Ordinamento

Può utilizzare la proprietà `schemaUI` per modificare l'ordinamento dei campi.

```powershell
New-UDForm -Schema @{
        title = "Test"
        type = "object"
        properties = @{
            hostname = @{
                title = "Hostname"
                type = "string"
                }
            ipaddress= @{
                title = "IP Address"
                type = "string"
                format = "ipv4"
                }
            description = @{
                title = "Server Description"
                type = "string"
                }
            servertype = @{
                title = "Server Type"
                type = "string"                            
                enum = "App","DB"
                }
            environment = @{
                title = "Environment"
                type = "string"
                enum = "Prod", "Dev" , "QA"
                }
            }
		required = @('hostname','ipaddress','description','servertype','environment')                    
	} -uiSchema @{
		"ui:order" = @('environment','hostname','ipaddress','description')
	} -OnSubmit {
		Show-UDModal -Content {                        
			New-UDTypography -Text $EventData
		} -Footer {
			New-UDButton -Text "Close" -OnClick {Hide-UDModal}
		} -Persistent
	}
```

### Array

Può creare form che accettano da 0 a molti oggetti. L'utente potrà aggiungere e rimuovere oggetti dal form.

```powershell
New-UDForm -Schema @{
   title = "Test Form"
   type = "array"
   items = @{
      type = "object" 
       properties = @{
           name = @{
               type = "string"
           }
           age = @{
               type = "number"
           }
       }
   }
} -OnSubmit {
   # $EventData[0].name
   # $EventData[0].age
}
```

## Form basati su script

Può generare automaticamente form basati sugli script nel suo ambiente PowerShell Universal. I form basati su script genereranno componenti di input in base al blocco `param`. I form basati su script supportano automaticamente avanzamento e feedback.

I form basati su script supportano inoltre la visualizzazione dell'output come testo o come tabella.

```powershell
New-UDForm -Script "Script.ps1" -OutputType 'text'
```

## API

* [New-UDForm](/powershell-universal/it/comandi-powershell/new-udform.md)
* [New-UDFormValidationResult](/powershell-universal/it/comandi-powershell/new-udvalidationresult.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.devolutions.net/powershell-universal/it/app/components/inputs/form.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
