Disable button not working

Dear all, I have a view with the following code. When I'm inserting data into the two textfields the button remains disabled.


struct SettingsView: View {

    @Environment(\.modelContext) private var context
    @State private var stagione =  Calendar.current.component(.year, from: Date())
    @State private var miaSquadra = ""
    @State private var categoria = ""
    
    var body: some View {
        NavigationStack {
            GroupBox("Stagione") {
                Form {
                    TextField("Stagione:", value: $stagione, formatter: NumberFormatter())
                        .frame(width: 150)
                    TextField("Categoria:", text: $categoria)
                        .frame(width: 400)
                    TextField("Mia squadra:", value: $miaSquadra, formatter: NumberFormatter())
                        .frame(width: 400)
                    Button("Salva") {
                        let nuovaStagione = Stagione(idStagione: stagione, categoriaStagione: categoria, miaSquadra: miaSquadra)
                        context.insert(nuovaStagione)
                    }
                    .frame(maxWidth: .infinity, alignment: .trailing)
                    .buttonStyle(.borderedProminent)
                    .disabled(categoria.isEmpty || miaSquadra.isEmpty)
                }
            }
            .padding()
            GroupBox("Squadre") {

            }

        }
        .navigationTitle("Impostazioni")
    }
}

#Preview {
    SettingsView()
        .environmentObject(NavigationStateManager())
}

What am I doing wrong?

Thanks, A.

Answered by AndreB82 in 785241022

Sorry for this - solved. It was the formatter in the second textfield.

Sorry, A.

Accepted Answer

Sorry for this - solved. It was the formatter in the second textfield.

Sorry, A.

Disable button not working
 
 
Q