{"record":{"id":"11c14051fb21181a","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"password-cannot-be-empty","errorCode":null,"errorMessage":"Password cannot be empty","messagePattern":"Password cannot be empty","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/MainDemo.Wpf/Domain/FieldsViewModel.cs","lineNumber":40,"sourceCode":"    [ObservableProperty]\n    private string? _text1;\n\n    [ObservableProperty]\n    private string? _text2;\n\n    [ObservableProperty]\n    private string? _password1 = string.Empty;\n\n    [ObservableProperty]\n    private string? _password2 = \"pre-filled\";\n\n    public string? Password1Validated\n    {\n        get => _password1Validated;\n        set\n        {\n            if (string.IsNullOrEmpty(value))\n                throw new ArgumentException(\"Password cannot be empty\");\n            SetProperty(ref _password1Validated, value);\n        }\n    }\n\n    public string? Password2Validated\n    {\n        get => _password2Validated;\n        set\n        {\n            if (string.IsNullOrEmpty(value))\n                throw new ArgumentException(\"Password cannot be empty\");\n            SetProperty(ref _password2Validated, value);\n        }\n    }\n\n    public FieldsTestObject TestObject => new() { Name = \"Mr. Test\" };\n\n    [ObservableProperty]","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MainDemo.Wpf/Domain/FieldsViewModel.cs#L22-L58","documentation":"`FieldsViewModel.Password1Validated` (in the MainDemo fields sample) throws `ArgumentException` from its setter if the new value is null or empty. It exists to demonstrate explicit validation on a password-style field backed by `SetProperty`.","triggerScenarios":"Setting `Password1Validated` to `null` or `\"\"`; clearing the bound password box; initializing/refreshing the view model with an empty value; data binding pushing the empty default of `_password1 = string.Empty` through.","commonSituations":"The backing field `_password1` is initialized to `string.Empty`, so any binding that round-trips that default into the validated property throws; clearing the field at runtime; binding the validated property to a control whose initial state is empty.","solutions":["Never bind/copy the empty default of `_password1` into `Password1Validated`; only set it once the user has entered a non-empty value.","Use `INotifyDataErrorInfo` / `ValidationRule` instead of throwing from the setter so an empty box is flagged rather than crashing the binding.","If empty is acceptable, change the guard to treat whitespace-only or only allow the throw on explicit submission.","Guard the assignment site: `if (!string.IsNullOrEmpty(newValue)) vm.Password1Validated = newValue;`."],"exampleFix":"// before\npublic string? Password1Validated\n{\n    get => _password1Validated;\n    set\n    {\n        if (string.IsNullOrEmpty(value))\n            throw new ArgumentException(\"Password cannot be empty\");\n        SetProperty(ref _password1Validated, value);\n    }\n}\n\n// after\npublic string? Password1Validated\n{\n    get => _password1Validated;\n    set\n    {\n        if (string.IsNullOrEmpty(value))\n        {\n            AddError(nameof(Password1Validated), \"Password cannot be empty\");\n            return;\n        }\n        ClearErrors(nameof(Password1Validated));\n        SetProperty(ref _password1Validated, value);\n    }\n}","handlingStrategy":"validation","validationCode":"if (!string.IsNullOrEmpty(newValue))\n    vm.Password1Validated = newValue;\n// else surface a validation error to the UI instead of throwing","typeGuard":"static bool IsValidPassword(string? value) => !string.IsNullOrEmpty(value);","tryCatchPattern":null,"preventionTips":["Avoid round-tripping the empty default of the backing field into the validated property.","Use INotifyDataErrorInfo/ValidationRule for password validation instead of setter throws.","Guard assignments with IsNullOrEmpty checks at the call site."],"tags":["wpf","mvvm","validation","setter","sample-code","password"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}