How can I validate my second bit of data in EditForm? - c#

This editform allows the user to edit a product from my products table. The data will then be updated to the database. This worked fine. Then I added my final 'comment' box. See I have an audit table which will record when any edits are made to the products. The audit record contains: an ID, the ID of the product being changed, the ID of the user making the change, the audit date and a comment. The validation of the other fields e.g. product.productname works but now I have a comment box where the validation is not being checked and is causing an error. I think it has to do with the fact that a Model is passed so that the models validation can be checked but when I tried to add a second model for the audit everything went red.
<EditForm Model=#selectedProduct OnValidSubmit="HandleSubmit">
<DataAnnotationsValidator />
<div class="form-group">
<label for="productName">Name:</label>
<InputText name="productName" #bind-Value="selectedProduct .ProductName"></InputText>
<ValidationMessage For="#(() => selectedProduct.ProductName)" />
<label for="productDescription">Description:</label>
<InputText name="productDescription" #bind-Value="selectedProduct .ProductDescription"></InputText>
<ValidationMessage For="#(() => selectedProduct.ProductDescription)" />
<label for="additionalInformation">Additional Information:</label>
<InputText name="additionalInformation" #bind-Value="selectedProduct.AdditionalInformation"></InputText>
<ValidationMessage For="#(() => selectedProduct.AdditionalInformation)" />
<label for="price">Price:</label>
<InputText name="price" #bind-Value="selectedProduct.ProductPrice"></InputText>
<ValidationMessage For="#(() => selectedProduct.ProductPrice)" />
<label for="auditComments">Comments:</label>
<InputText name="auditComments" #bind-Value="auditProduct.Comments"></InputText>
<ValidationMessage For="#(() => auditProduct.Comments)" />
<button type="submit" class="btn-primary">Save</button>
<button type="submit" class="btn-secondary" #onclick="HandleCancel">Cancel</button>
</div>
</EditForm>

It appears that you are attempting to add validation to your audit table's "comments" column, however the validation is not operating. It appears that you also wish to validate the auditProduct model while using the DataAnnotationsValidator component to validate the selectedProduct model.
You must create a DataAnnotationsValidator component and give the auditProduct model to the EditForm component separately in order to add validation for it.
<EditForm Model=#selectedProduct AuditModel=#auditProduct OnValidSubmit="HandleSubmit">
<DataAnnotationsValidator />
<DataAnnotationsValidator Model="auditProduct" />
<!-- rest of the form fields -->
</EditForm>

Related

Registration on another page

I have authorization form for my website:
Inside form:
<div class="auth">
#* <h1 class="auth__header">New User</h1> *#
<div class="auth__form">
<EditForm class="form" Model="#us" OnValidSubmit="#OnValidSubmit">
<DataAnnotationsValidator/>
<label class="auth__label">Name :</label>
<InputText class="input auth__input" #bind-Value="us.Name"/>
<ValidationMessage For="#(() => us.Name)" />
<label class="auth__label">Surname :</label>
<InputText class="input auth__input" #bind-Value="us.Surname"/>
<ValidationMessage For="#(() => us.Surname)" />
<label class="auth__label">Email :</label>
<InputText class="input auth__input" #bind-Value="us.Email"/>
<ValidationMessage For="#(() => us.Email)" />
<label class="auth__label">Password :</label>
<InputText class="input auth__input" #bind-Value="us.Password"/>
<ValidationMessage For="#(() => us.Password)" />
<button>#ButtonText</button>
</EditForm>
</div>
Problem is that it's shown inside site in the #Body in Shared blocks, but I need it as a another page as in this example (Blazor with individual Accounts), and I don't know how to do it:
But in this example, authorization form is a separate element, not subject to customization, and I didn't find examples or resources on the internet that had their own form.
Thanks for answers.
Create new razor page with this code:
#inherits LayoutComponentBase
<div>
#Body
</div>
And paste this into the page you need (for my example create.razor):
#layout LoginLayout
And this will be worked.

Piranha CMS Want to extend the Users with new Data?

I wanted to Customize the Manager Area, specific the User. I added a new Column(PhoneNumber) in the Database and the Data of this field gets in the UserEditModel. I added an new text field in the Edit.cshtml.
<div class="col-sm-6">
<div class="form-group">
<label>#Localizer.General["Email address"]</label>
<input type="email" v-model="userModel.user.email" class="form-control small" maxlength="128" required />
<div class="invalid-feedback">
#Localizer.General["Email address is mandatory."]
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>#Localizer.General["PhoneNumber"]</label>
<input v-model="userModel.user.PhoneNumber" class="form-control maxlength="128" required />
<div class="invalid-feedback">
#Localizer.General["PhoneNumber is mandatory."]
</div>
</div>
</div>
In the Model the PhoneNumber is filled with the Data from the Database, but the Phonenumber Field doesnt get the Data from the Model. Do i need to declare the Field somewhere? Do i need to change something in the piranha.useredit.js?
I hope somebody can help me.
This is the method in the project Piranha.AspNetCore.Identity that is responsible for saving the user to the database.
https://github.com/PiranhaCMS/piranha.core/blob/master/identity/Piranha.AspNetCore.Identity/Models/UserEditModel.cs#L60
You can use the identity module as a template/ or use NuGet-package Piranha.AspNetCore.Identity and create your own identity Implementation.
Follow this link for more info on Piranha Modules https://piranhacms.org/docs/master/extensions/modules

First getting data from controller, then how to send it back? asp.net core mvc

I have made a Asp.net core view and getting data from database is working, but I want to add adding comments option. Comments send almost properly, but while sending a comment I need to pass "samochodId" from my asp.net core model which was passed first.
<div class="form-group">
<label asp-for="Komentarz.Wiadomosc" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Komentarz.Wiadomosc" class="form-control" />
<span asp-validation-for="Komentarz.Wiadomosc" class="text-danger"></span>
<input asp-for="Komentarz.samochodId" class="form-control" />
</div>
</div>
This works, but I need to remove this line:
<input asp-for="Komentarz.samochodId" class="form-control" /> and do the pass automaticly by getting this Id from My model (Model.Samochod.Id) or maybe from the route?.
How Can I do this?
use hidden field <input type="hidden" asp-for="Komentarz.samochodId" /> and make sure it's within the opening and closing of form <form></form> element

How to avoid #Html.CheckBoxFor generating hidden field?

Using #Html.CheckBoxFor generates a hidden field. Is there a way I can avoid generating that ?
Why I want to do that ? I was provided design, which has some script or library used in it for visual display. It works fine if Html is in below format (Checkbox with Label):
<div>
<input type="checkbox" value="resources" id="resources" class="cb">
<label for="resources">
I need an offset facility
</label>
<i class="tooltip-icon" data-icon="repayments"></i>
</div>
But it do not work if there is a hidden field between Checkbox and Label:
<div>
<input id="HomeLoanLead_Redraw_flexibility_Value" class="cb" type="checkbox" value="true" name="HomeLoanLead.Redraw_flexibility_Value" data-val-required="The Redraw_flexibility_Value field is required." data-val="true">
<input type="hidden" value="false" name="HomeLoanLead.Redraw_flexibility_Value">
<label for="HomeLoanLead_Redraw_flexibility_Value"> I want to make extra repayments </label>
<i class="tooltip-icon" data-icon="loan"></i>
</div>
If I try using <input type=checkbox> I m afraid I will not get out of box model binding in post action.
Please help.

MVC Problem with simple Postback

Bit of an odd one here:
The following razor syntax renders a nce simple html from with a submit button inside the bottom of it.
When i hit that button i would expect a postback to performed but for reason it dosn't ... any ideas??
Oh by the way this is the entire code for the view ...
#model FLM.PRL.EComms.Models.ReplySMS
#using (Html.BeginForm("Reply", "SMS", FormMethod.Post)) {
<h2>Follow Up</h2>
#Html.ValidationSummary(true)
#Html.HiddenFor(model => Model.From)
#Html.HiddenFor(model => Model.To)
<div class="editor-label">Reply</div>
<div class="editor-field">
#Html.EditorFor(model => Model.Message)
#Html.ValidationMessageFor(model => Model.Message)
</div>
<input type="submit" value="Reply" />
<br />
}
EDIT: Resulting markup generated by this view ...
<form action="/SMS/Reply" method="post">
<h2>Follow Up</h2>
<input data-val="true" data-val-required="The From field is required." id="From" name="From" type="hidden" value="xxxxxxxx" /><input data-val="true" data-val-required="The To field is required." id="To" name="To" type="hidden" value="xxxxxxx" /> <div class="editor-label">Reply</div>
<div class="editor-field">
<textarea class="text-box multi-line" id="Message" name="Message">
</textarea>
<span class="field-validation-valid" data-valmsg-for="Message" data-valmsg-replace="true"></span>
</div>
<input id="submitReply" type="submit" value="Reply" />
<br />
</form>
The only reason for this form not to be submitted are scripts interfering with the process. Maybe the validation scripts.
I noted that the Message field is required. Did you provided a value to this field? Don't you have any validation error message?
In the case validation is fine, I suggest removing selectively all the scripts from the page and see what happens. If removing a script results in your form working, you know it is the source of the problem.
I figured it out ... it's the hidden fields ... they are required but populated by the server during the postback so the auto validation prevents the postback client side before the server is able to populate the fields ...
solution:
Don't add empty required fields to an MVC form or turn off validation (not a good idea)

Categories