有没有办法在验证 PasswordBox 时在 AdornedElementPlaceholder 中显示错误消息。
我有这样的事情:
<ControlTemplate x:Key="DefaultErrorTemplate">
<StackPanel Orientation="Horizontal">
<AdornedElementPlaceholder x:Name="placeholder" />
<Border Background="Red"
ToolTip="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
ToolTipService.InitialShowDelay="0"
VerticalAlignment="Top"
Margin="3"
Width="20"
Height="20"
CornerRadius="10">
<TextBlock Text="!"
Foreground="White"
FontWeight="Bold"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
</StackPanel>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
和我的 BaseControlStyle 即时通讯使用该验证
<Style TargetType="Control"
x:Key="ControlBaseStyle">
<Setter Property="Validation.ErrorTemplate"
Value="{StaticResource DefaultErrorTemplate}" />
Run Code Online (Sandbox Code Playgroud)
它对我拥有的几乎所有控件(Combobox、DateTimePicker、TextBox)都非常有用,但是当我想使用相同的样式时,passwordBox
它就不起作用了。
在图片中,您可以看到它与 simpe TextBox 一起工作,但不与 PasswordBox 一起工作。我不知道如何提取错误消息以在工具提示中显示它
AdornedElementPlaceholder
它显示用户名属性的错误消息
[Required(ErrorMessage = "Please enter username.")]
Run Code Online (Sandbox Code Playgroud)
我不想用 passwordBox 实现同样的事情来向用户反馈输入密码时的错误(约束)
任何帮助都非常感谢。
提前致谢 :)
编辑:
我已将此用于密码属性
[Required(ErrorMessage = "Please enter password.")]
[RegularExpression(@"^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$")]
[StringLength(maximumLength: 15, ErrorMessage = "Minimum …
Run Code Online (Sandbox Code Playgroud) 我创建了一些registrationSchema
export const registrationSchema = (translate) => Yup.object().shape({
//... other properties that are validated.
// for example username
username: Yup.string()
.min(6, translate('validation:common.username.min', { min: 6 }))
.max(20, translate('validation:common.username.max', { max: 20 }))
.required(translate('validation:common.username.required')),
DOB: Yup.lazy(value => {
console.log('yup', value);
if (moment().diff(moment(value), 'years') < 18)
// Here return DOB error somehow
})
...
Run Code Online (Sandbox Code Playgroud)
到目前为止,它的工作原理就像是一种魅力。但是现在我需要验证用户是否年满18岁。我从日期选择器中获取DateOfBirth,并且可以随时检查它是否小于18。
if(moment().diff(moment(date),'years) < 18)
Run Code Online (Sandbox Code Playgroud)
这个日期值是我在使用Yup.lazy时获得的,但是如果在18年以下才能在DOB字段中显示它,则不知道如何抛出验证错误。我什至不知道我是否使用正确的yup方法。我想使用yup.date()。但是如何在架构中获取选择日期以检查其有效年龄。