validation 相关问题

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

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

如何在 Laravel 中正确使用音频文件类型验证?

我对 Laravel 还很陌生,正在尝试学习这个框架。我目前正在制作一个简单的帖子/排行榜页面,用户应该能够向其中上传音频文件。一切正常...

回答 1 投票 0

Laravel Blade 错误指令与 php 等效

我无法理解 Laravel @error Blade 指令背后的逻辑。 它如何与 @error 指令之间包含的 $message 一起使用? 它如何知道要显示哪条 $message? 我想...

回答 1 投票 0

如何在没有 <form>

我知道,如果我在 元素中有输入,我可以简单地将 novalidate 属性添加到表单中以禁用 HTML 验证工具提示。但是,我的输入不是 elem 形式... 我知道,如果我在 input 元素中有一个 <form>,我可以简单地将 novalidate 属性添加到表单中以禁用 HTML 验证工具提示。但是,我的输入不在表单元素中(我使用 AngularJS 并且使用 ng-form 指令)。 我可以监听 invalid 事件并阻止提交验证,但我无法阻止验证工具提示。 这是一个简单的 JSFiddle 来显示问题。 虽然 Dekel 的答案 可以完成这项工作,但实现此目的的另一种方法是简单地向输入元素添加“title”属性,并且验证消息将被 title 属性中的内容覆盖。 你可以在title属性中添加一些有意义的文字, <input type="email" title="Your Email"> 在 HTML 中,输入元素有两个选项: 将它们放入<form>容器中。 为输入添加 form 属性,该值应为输入相关表单的 id。 使用选项#2,您只需在 DOM 中的某处添加一个带有 novalidate 的空表单,并将输入附加到该表单: <form novalidate> <p>Input in form with novalidate. This one is fine.</p> <input type="email" required> </form> <p>Input without form. How to disable validation tooltips? (hover over input to see validation tooltip)</p> <input type="email" required form="novalidatedform"> <form id="novalidatedform" novalidate /> 有关 input 元素的更多信息请参见 MDN 网站。 您也可以使用此脚本 <script> document.getElementById( "inputId" ).addEventListener( "invalid", function( event ) { event.preventDefault(); }); </script> 将 novalidate 添加到 form 标签应该就足够了

回答 4 投票 0

如何使用java验证html?使用 jsoup 库时遇到问题

我需要使用java验证HTML。所以我尝试使用 jsoup 库。但我的一些测试用例失败了。 例如,这是我的 html 内容。我对此内容没有任何控制权。我明白了...

回答 2 投票 0

Joi 验证中从开始到结束的限制范围

在nodeJs中添加Joi验证,以限制endDate不超过startDate之后3年。 例子 : 开始日期 2024-03-29 , 最大结束日期应为:2027-03-29。 我试过: const maxEndDate = Joi.ref('

回答 1 投票 0

您可以更改默认表单验证消息的区域设置吗?

有没有办法更改默认表单验证消息的区域设置,以便无论浏览器的语言如何,它们始终以英语显示。 我不想通过

回答 1 投票 0

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