validation 相关问题

验证用于检查数据,以确保它符合为其设置的任何所需规范。通常,验证用于检查输入数据,以及在存储之前验证数据。

验证 DateTime::modify() 的字符串输入

我有一个场景,发送到 DateTime::modify($string) 的 $string 变量是从用户编辑的数据库中读取的,我如何验证 $string 是 DateTime::modify() 的正确字符串?

回答 5 投票 0

Laravel 10 与 FilamentPHP 3 - 重叠日期范围验证

如何防止数据库中现有日期范围的日期重叠? 我有 SchoolPeriod 模型和 school_periods 表,其中包含 date_from 和 date_to 列,它被定义为受保护的可填充...

回答 1 投票 0

如何使用JUnit5、@SpringBootTest测试@Valid?

这是我的控制器类。 导入 jakarta.validation.Valid; 导入 jakarta.validation.constraints.NotNull; 导入 org.springframework.http.ResponseEntity; 导入 org.springframework.web.bind.anno...

回答 1 投票 0

Laravel 按组请求验证

所以,我有一个 Creators 表,其中包含与创建者相关的所有信息。 该表有一些可为空的列,可以根据您的创建者类型来填充这些列。 作为一个例子,可以是...

回答 1 投票 0

验证 Openapi 3.0 Spring Boot RESTful API 中的 URL 参数?

在我的 openapi 规范中,我为特定资源路径指定 URL 参数,如下所示: 路径: /一些/路径: 得到: 摘要:一些总结 参数: 名称:有用参数

回答 2 投票 0

这个ABNF中的[CFWS]和[FWS]是什么意思?

电子邮件的 RFC 2282 具有以下用于引用字符串的 ABNF。 带引号的字符串 = [CFWS] DQUOTE *([FWS] qcontent) [FWS] DQUOTE [CFWS] 我用谷歌搜索...

回答 1 投票 0

Mime 验证在 Laravel 中无法正常工作

我的网页包含一个要上传的文件,我希望上传的文件只能是pdf、doc或docx。 我的表单标签也有 enctype="multipart/form-data" 我的 html 看起来像: 我的网页包含要上传的文件,我希望上传的文件只能是 pdf、doc 或 docx。 我的表单标签也有 enctype="multipart/form-data" 我的 html 看起来像: <div id="cv_upload" class="row"> <div class="col-xs-12"> <input type="file" name='cv'> </div> </div> 与此关联的 $rules 数组如下: 'cv' => 'mimes:application/pdf,application/doc,application/docx|required' 最后我的消息如下: 'cv.required' => 'A selection for C.V. is required before proceeding.', 'cv.mimes' => 'CV must be of the following file type: pdf, doc or docx.' 唯一的问题是,即使我上传了 pdf 或 doc,我收到的消息也是所需的消息。我不知道为什么这没有按预期工作。我还尝试删除“application/”,但这也没有成功。请帮忙。 确保您的表单具有属性 enctype="multipart/form-data" 此外,如果您使用 mimes 验证,格式必须为 'mimes:jpeg,bmp,png' https://laravel.com/docs/5.6/validation#rule-mimes 您可以在此处阅读有关 enctype 属性的信息:enctype='multipart/form-data' 是什么意思? 在表格中尝试输入files="true"。 在验证器中尝试输入验证,例如 required|mimes:doc,docx,pdf https://laravel.com/docs/5.4/validation#rule-mimes 编辑:当您使用mimes:application/doc时,实际上是mimetypes:application/doc 您好,我遇到了类似的问题,点击以下命令后一切都正确,问题得到解决 php artisan cache:clear , php artisan config:clear , php artisan route:clear 如果您遇到 .docx 扩展文件的任何问题,请使用如下验证 'cv' => 'mimetypes:application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/octet-stream,application/pdf' 这将有助于验证 doc、Docx 和 pdf 文件。 要显示正确的错误消息,请使用以下代码: public function messages () { return [ 'cv.mimetypes' => 'The cv file must be a file of type: pdf, doc, docx.' ]; } 参考:Laravel 7 如果有人遇到同样的问题,您必须使用文件扩展名,而不是像这样的 mime 类型: 'file' => 'required|file|mimes:pdf,doc,docx' 可以找到扩展的完整列表:https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types 如果有人正在寻找哑剧图像,请按以下方法操作。 'image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048', 有关更多规则,请遵循此 laravel 文档

回答 6 投票 0

如何在C#中增强服务器端表单验证以更新表单?

我在教师控制器的更新操作中实现了基本的服务器端验证,以检查某些字段的空值或空值,例如 TeacherFname、TeacherLname、EmployeeNumber、

回答 1 投票 0

Laravel - 使用嵌套验证请求访问信息

我想访问嵌套验证数组中的信息以进行自定义验证,这是我的代码: 返回 [ ..., '金额' => ['可空', '数字'], 'currency_id' => ['可空', '整数...

回答 1 投票 0

如何验证 .NET 中包含不可见字符的 Unicode 字母

在.NET中,我有一个简单的行,断言字符串必须仅包含字母或数字 if (str.Any(c => !char.IsLetterOrDigit(c)) { 抛出新的异常(); } 问题是字符串可以...

回答 1 投票 0

Pydantic - 在验证之前更改嵌套模型中的数据

我有一个带有 pydantic 的嵌套对象的小例子。 从输入导入字典 从 pydantic 导入 BaseModel、Field、ValidationError 类用户类型(基础模型): 名称:str = 字段(最小长度...

回答 1 投票 0

生成列的长度是否受到@Size和@Column注解的影响?

我的实体拥有以下属性: @塞特 @大小(最小值= 3,最大值= 255) @Column(长度= 500) 私有字符串xo; h2 数据库中生成的列是 性格各异(255) 我预计会是...

回答 1 投票 0

如何找到 html 计算器中使用的公式

我已搜索但无法找到此计算器的公式。 https://www.saltyunderground.com/support/berghia-calculator.html 打开源代码页面并找到公式,然后用于创建自定义计算...

回答 1 投票 0

使用子组件时,Blazor EditForm 验证不起作用

我有一个名为 EditOffice 的 Blazor 组件。它看起来如下: 我有一个名为 EditOffice 的 Blazor 组件。看起来如下: <EditForm Model="@Office" OnValidSubmit="@HandleValidSubmit"> <DataAnnotationsValidator /> <ValidationSummary /> <InputTextRow Label="Name" @bind-Value="@Office.Name" Placeholder="Enter name" /> <InputTextRow Label="ABN" @bind-Value="@Office.ABN" Placeholder="Enter ABN" /> ... <button type="submit" class="btn btn-primary edit-btn">Save office</button> </EditForm> 我创建了名为 InputTextRow 的子组件,试图Tidy我的代码。它们看起来如下: <div class="form-group row"> <label for="@Id" class="col-sm-3">@Label: </label> <InputText id="@Id" @oninput="OnValueChanged" @bind-Value="@Value" class="form-control col-sm-8" placeholder="@Placeholder"></InputText> <ValidationMessage class="offset-sm-3 col-sm-8" For="@(() => Value)" /> </div> @code { public string Id => Label.ToLower().Replace(" ", ""); [Parameter] public string Label { get; set; } [Parameter] public string Value { get; set; } [Parameter] public string Placeholder { get; set; } [Parameter] public EventCallback<string> ValueChanged { get; set; } Task OnValueChanged(ChangeEventArgs e) { Value = e.Value.ToString(); return ValueChanged.InvokeAsync(Value); } } ValidationMessage 在我的子组件中不起作用。知道为什么吗? 我知道我有点晚了,但这是我的答案:) 所以现在有更好的解决方案。 TL:懒人的灾难恢复解决方案 请注意 - 这是 实验性的 ,但软件包已经在候选版本中,所以我想不用担心。 使用 Microsoft.AspNetCore.Components.DataAnnotations.Validation 包和 <ObjectGraphDataAnnotationsValidator /> 而不是 <DataAnnotationsValidator /> 并使用这个东西: using System.ComponentModel.DataAnnotations; public class YourComplexModel { // other properties [ValidateComplexType] // <--life saver public ChildModel ChildModel { get; set; } = new ChildModel(); } 来自 MS Docs 的片段 链接Microsoft 文档: Blazor 支持使用内置 DataAnnotationsValidator 的数据注释来验证表单输入。但是,DataAnnotationsValidator 仅验证绑定到表单的模型的顶级属性,这些属性不是集合或复杂类型属性。 要验证绑定模型的整个对象图,包括集合类型和复杂类型属性,请使用实验性 Microsoft.AspNetCore.Components.DataAnnotations.Validation 包提供的 ObjectGraphDataAnnotationsValidator: <EditForm Model="@model" OnValidSubmit="@HandleValidSubmit"> <ObjectGraphDataAnnotationsValidator /> ... </EditForm> 使用 [ValidateComplexType] 注释模型属性。在以下模型类中,ShipDescription 类包含附加数据注释,用于验证模型何时绑定到表单: Starship.cs: using System; using System.ComponentModel.DataAnnotations; public class Starship { ... [ValidateComplexType] public ShipDescription ShipDescription { get; set; } = new ShipDescription(); ... } ShipDescription.cs: using System; using System.ComponentModel.DataAnnotations; public class ShipDescription { [Required] [StringLength(40, ErrorMessage = "Description too long (40 char).")] public string ShortDescription { get; set; } [Required] [StringLength(240, ErrorMessage = "Description too long (240 char).")] public string LongDescription { get; set; } } 如果您想使用 FluentValidations,您只需像这样更新您的 InputTextRow.razor: <div class="form-group row"> <label for="@Id" class="col-sm-3">@Label: </label> <InputText id="@Id" @oninput="OnValueChanged" @bind-Value="@Value" class="form-control col-sm-8" placeholder="@Placeholder"></InputText> <ValidationMessage class="offset-sm-3 col-sm-8" For="ValueExpression" /> </div> @code { [Parameter] public string Label { get; set; } [Parameter] public string Value { get; set; } [Parameter] public EventCallback<string> ValueChanged { get; set; } [Parameter] public Expression<Func<string>> ValueExpression { get; set; } = default!; [Parameter] public string Placeholder { get; set; } [CascadingParameter] public EditContext? EditContext { get; set; } public string Id => Label.ToLower().Replace(" ", ""); Task OnValueChanged(ChangeEventArgs e) { Value = e.Value.ToString(); return ValueChanged.InvokeAsync(Value); } } 因此进行了 3 处更改: 添加了ValueExpression参数。当您将属性绑定到该参数时,该参数将被填充。您需要它作为 ValidationMessage 的输入 将 EditContext 添加为 CascadingParameter。当您的组件位于 EditForm 中时,此参数会自动填充 ValueExpression 现在作为 ValidationMessage's For argument 中的参数 如果您想要条件检查是否有可用的 ValidationMessage,您可以这样检查: private bool _hasValidationMessages => EditContext is null || ValueExpression is null ? false : EditContext.GetValidationMessages(FieldIdentifier.Create(ValueExpression)).Any(); 我遇到了完全相同的问题。我的代码与你的非常相似。我的子组件确实进行了验证,但未显示验证错误消息。 我确实使用了这个扩展方法: using System; using Microsoft.AspNetCore.Components.Forms; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace TimeRecording.Extensions { public static class EditContextExtensions { static PropertyInfo IsModifiedProperty; static MethodInfo GetFieldStateMethod; /// <summary> /// Validates an entire object tree /// </summary> /// <param name="editContext">The EditContext to validate the Model of</param> /// <returns>True if valid, otherwise false</returns> public static bool ValidateObjectTree(this EditContext editContext) { var validatedObjects = new HashSet<object>(); ValidateObject(editContext, editContext.Model, validatedObjects); editContext.NotifyValidationStateChanged(); return !editContext.GetValidationMessages().Any(); } public static void ValidateProperty(this EditContext editContext, FieldIdentifier fieldIdentifier) { if (fieldIdentifier.Model == null) return; var propertyInfo = fieldIdentifier.Model.GetType().GetProperty( fieldIdentifier.FieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); var validatedObjects = new HashSet<object>(); ValidateProperty(editContext, fieldIdentifier.Model, propertyInfo, validatedObjects); } private static void ValidateObject( EditContext editContext, object instance, HashSet<object> validatedObjects) { if (instance == null) return; if (validatedObjects.Contains(instance)) return; if (instance is IEnumerable && !(instance is string)) { foreach (object value in (IEnumerable)instance) ValidateObject(editContext, value, validatedObjects); return; } if (instance.GetType().Assembly == typeof(string).Assembly) return; validatedObjects.Add(instance); var properties = instance.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (PropertyInfo property in properties) ValidateProperty(editContext, instance, property, validatedObjects); } private static void ValidateProperty( EditContext editContext, object instance, PropertyInfo property, HashSet<object> validatedObjects) { NotifyPropertyChanged(editContext, instance, property.Name); object value = property.GetValue(instance); ValidateObject(editContext, value, validatedObjects); } private static void NotifyPropertyChanged( EditContext editContext, object instance, string propertyName) { if (GetFieldStateMethod == null) { GetFieldStateMethod = editContext.GetType().GetMethod( "GetFieldState", BindingFlags.NonPublic | BindingFlags.Instance); } var fieldIdentifier = new FieldIdentifier(instance, propertyName); object fieldState = GetFieldStateMethod.Invoke(editContext, new object[] { fieldIdentifier, true }); if (IsModifiedProperty == null) { IsModifiedProperty = fieldState.GetType().GetProperty( "IsModified", BindingFlags.Public | BindingFlags.Instance); } object originalIsModified = IsModifiedProperty.GetValue(fieldState); editContext.NotifyFieldChanged(fieldIdentifier); IsModifiedProperty.SetValue(fieldState, originalIsModified); } } }

回答 3 投票 0

如何在模板中以 Angular 形式访问动态设置的输入名称属性

我将 ngx-datatable 组件包装在表单标签中,以便我可以验证表格单元格中的输入。由于表填充方式的性质,我动态设置输入名称属性 我将 ngx-datatable 组件包装在 form 标签中,以便我可以验证表格单元格中的 inputs。由于表格填充方式的性质,我动态设置输入 name 属性 <form #tableForm="ngForm"> <ngx-datatable [rows]="_rows"> <ng-container *ngFor="let column of rowDeffinition; let columnIndex=index"> <ngx-datatable-column [prop]="column.key" [name]="column.label"> <ng-template ngx-datatable-cell-template let-rowIndex="rowIndex" let-value="value" let-row="row"> <input class="cell-input" (blur)="updateCellValue($event, column.key, rowIndex)" type="text" [ngModel]="value" [name]="rowIndex + '-' + column.key" /> ... </ng-template> </ngx-datatable-column> </ng-container> </ngx-datatable> </form> 通常,name属性会在模板中创建一个局部变量,您可以通过变量名称访问输入控件属性。 <input type="text" name="name" [(ngModel)]="name" required minlength="4" /> <div *ngIf="name.invalid && name.touched"> 我想知道如何以与设置输入名称相同的方式动态地执行此操作。到目前为止,我能够通过 表单参考 访问输入控件,但这变得相当冗长 <span *ngIf="!tableForm.controls[rowIndex + '-' + column.key]?.valid && !tableForm.controls[rowIndex + '-' + column.key]?.pristine" class="[ c-validation-message ]"> required </span> 您可以将输入包装在新组件中,并且可以通过父组件中的@ViewChildren(...)访问这些生成的组件.ts文件: @ViewChildren(NgxDatatableInput) datatableInputs: QueryList<NgxDatatableInput>; 我建议在父组件中创建方法,该方法通过 datatableInputs 作为参数从 name 检索具体的 datatableInput。之后,您可以在生成的新ValidationSpanComponent中使用此方法: <ValidationSpan [control]="getDatatableInput(dynamicName)"> </ValidationSpan> ValidationSpanComponent的模板: <span *ngIf="!control.valid && !control.pristine" class="[ c-validation-message ]"> required </span> 受到这个答案的启发我想出了以下解决方案 <form #tableForm="ngForm"> <ngx-datatable [rows]="_rows"> <ng-container *ngFor="let column of rowDeffinition; let columnIndex=index"> <ngx-datatable-column [prop]="column.key" [name]="column.label"> <ng-template ngx-datatable-cell-template let-rowIndex="rowIndex" let-value="value" let-row="row"> <!-- Helper template allowing to define few variables for more readable property binding--> <ng-template #cellContent [ngTemplateOutlet]="cellContent" let-cellName="cellName" let-isValidInput="isValidInput" let-isPristineInput="isPristineInput" let-isRequiredInput="isRequiredInput" [ngTemplateOutletContext]="{ cellName: rowIndex + '-' + column.key, isValidInput: tableForm.controls[rowIndex + '-' + column.key]?.valid, isPristineInput: tableForm.controls[rowIndex + '-' + column.key]?.pristine, isRequiredInput: column.input?.required }"> <input class="cell-input" (blur)="updateCellValue($event, column.key, rowIndex)" type="text" [ngModel]="value" [name]="cellname" /> ... </ng-template> </ng-template> </ngx-datatable-column> </ng-container> </ngx-datatable> </form> 这极大地提高了总体可读性,因为我的表具有非常复杂的逻辑,并且我以如下结构跟踪单元格的状态: interface EditTableRowStatus { editing: { [columnId: string]: boolean}, changed: { [columnId: string]: boolean}, addedRow: boolean; } let rowsStates = EditTableRowStatus[] 这导致模板中的属性绑定超级复杂 <input class="cell-input" *ngIf="column.input?.type === INPUT_TYPES.TEXT && (rowsStates[rowIndex]?.editing?.[column.key] || rowsStates[rowIndex]?.addedRow)" [autofocus]="!rowsStates[rowIndex]?.addedRow || columnIndex === 0 " (blur)="updateCellValue($event, column.key, rowIndex)" type="text" [ngModel]="value" [ngClass]="{'has-changes': rowsStates[rowIndex]?.changed?.[column.key]}" [name]="rowIndex + '-' + column.key" [required]="column.input?.required" /> 现在变得更具可读性 <input class="cell-input" *ngIf="inputType === INPUT_TYPES.TEXT && (isEditing || isAddedRow)" [autofocus]="!isAddedRow || columnIndex === 0 " (blur)="updateCellValue($event, column.key, rowIndex)" type="text" [ngModel]="value" [ngClass]="{'has-changes': isChanged}" [name]="cellName" [required]="isRequiredInput" />

回答 2 投票 0

如何在规则方法中使用多个请求类(Livewire)

受保护的函数规则() { 返回(新的 FormRequest)->rules(); } 像上面的代码如何使用多个请求类?

回答 2 投票 0

如何使用 TextFinder() 将 Google 表格侧栏中表单输入的值传递到 Google 表格单元格

我已经为这个问题困惑了好几个小时了。 这就是我想要实现的目标。最终目标是能够使用 Google 表格中的侧边栏修改作业名称。侧边栏可以是

回答 1 投票 0

渲染和捕获表单数据的问题..REACT

我遇到了问题。我创建了一个表单,当我单击 index.tsx 文件中的添加按钮时,它没有添加。但是,当我尝试渲染虚拟产品列表时,它起作用了。让我分享...

回答 1 投票 0

实现 if 语句逻辑时,表单提交按钮不起作用

我试图通过简单的空字符串输入验证来实现提交按钮逻辑,但是单击按钮后整个窗口变得空了。在符合if语句的同时,一个块ne...

回答 1 投票 0

z 模式中的“anyof”和“oneof”有什么区别?

看起来两者都可以很好地配合我的输入验证代码。那么具体的区别是什么呢? 具有 oneof 的架构 [{ "id": "我的操作", “oneOf”:[{“$ref”:“A1”}, {“$ref”:“A2...

回答 3 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.