Skip to content

BoolField.validate is broken #15

@MrMage

Description

@MrMage

BoolField.validate currently contains the following code:

// If POSTing from a `checkbox` style input, the key's absence means `false`.
let bool = value.bool ?? false

However, if a checkbox named e.g. "someBoolValue" has been checked, its form value will be a string with content "someBoolValue". Thus, you should be checking for the field's string value instead:

// If POSTing from a `checkbox` style input, the key's absence means `false`.
let bool = value.string != nil

or check both, as a failsafe:

// If POSTing from a `checkbox` style input, the key's absence means `false`.
let bool = value.string != nil || (value.bool ?? false)

See e.g. https://github.com/nodes-vapor/admin-panel-provider/blob/master/Sources/AdminPanelProvider/Models/AdminPanelUserForm.swift (lines 62/63 at the moment) for examples of where this is done correctly:

    let shouldResetPassword = data["shouldResetPassword"]?.string != nil
    let sendEmail = data["sendEmail"]?.string != nil

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions