Share via

[Required(AllowEmptyStrings = true)] not working

Dondon510 261 Reputation points
2022-08-17T16:02:25.527+00:00

I have a not required field, but why it keeps validated?, I have tried to remove the 'readonly' attribute but still keeps validated, what I missed?

    [Required(AllowEmptyStrings = true)]  
    public string parentOrganizationName { get; set; } = "";  
 

   <div class="row">  
              <div class="col-6 mb-1">  
                   <label class="form-label" for="parentOrganizationName">Parent Organization</label>  
                        <span data-bs-toggle="tooltip" data-bs-placement="top" title="Parent organization is your root organization, this readonly field value was defined by the vendor.">  
                        <i data-feather="info"></i>  
                                            </span>  
                                            <input type="text" class="form-control" id="parentOrganizationName" asp-for="parentOrganizationName" value="@Model.parentOrganizationName"  />  
                                        </div>  
                                    </div>  


  
  
Developer technologies | ASP.NET Core | Other
0 comments No comments

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,086 Reputation points
    2022-08-17T17:48:13.827+00:00

    I don't believe the asp/net require logic supports AllowEmptyStrings]

    as [Required] just means not null or empty, why include if empty allowed? in form post, the value would never be null. the binder would find and set the value (which is always a string value) or it not included in the form data and your default value would be used.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.