Issue with [Url] data annotaiion - validation

Kind of new to data annotations for validation and need some help on a weird issue I am having. This is a Blazor server application but not sure that makes any difference. In my model I have many properties but this is where the issue is:
[Url]
public string WebsiteURL { get; set; }
As you can see I do not have the [Required] attribute set as I just want to validate if there is text in the textbox. It displays the error message as I would expect when a bad URL is entered:
Now, if I clear the textbox, the error still remains:
I want the validation error message to clear if the textbox is cleared after the validation message is displayed. Even if I click my submit button, the message is still there. How can I do this?
Thank you,
Jim

It seems with is an issue with a few of the data annotation validator tags:
https://github.com/dotnet/runtime/issues/53820
It is strange to me because if I want the value to NOT be blank or null then [Required] should be used for this. I understand the [Url] tag checks for correct format, but it should allow or have an option tag to allow blanks/nulls as well.
To get around this, I had to use RegEx validators for Urls and Email addresses.
Thanks all,
Jim

Related

Pre-filling a hosted MailChimp signup form with an email address shows validation errors

We're trying to pre-fill the email address field of a hosted MailChimp form. Here's the blog post that talks about exactly this matter: https://blog.mailchimp.com/how-to-pre-fill-items-on-your-mailchimp-hosted-form/.
So here's our Newsletter signup form without any validation errors:
https://camping.us2.list-manage.com/subscribe/post?u=761a52bbd46ab21474b3af314&id=5cc638b5e6.
The problem arises when I add an email address to the URL as the value for the first form field, MERGE0, like this (url-encoding the email address, so # becomes %40):
https://camping.us2.list-manage.com/subscribe/post?u=761a52bbd46ab21474b3af314&id=5cc638b5e6&MERGE0=test%40camping.info.
Now, the form correctly copies the email address into the input field with name MERGE0 but it also displays three validation error messages:
Note: the email address field is mandatory.
When I try to pre-fill the user's first or last name fields adding MERGE1=John or MERGE2=Doe to the form's url, all is well - no error happens. It seems to be a problem with mandatory fields.
I'm arguing that this is a problem on MailChimp's side but they refuse to help saying that this is custom code and none of their business.
Can anybody help fix these errors?
It turned out that the problem was caused by a mistake in the URL.
Instead of camping.us2.list-manage.com/subscribe/POST?u=761a52bbd46ab...
it had to be camping.us2.list-manage.com/subscribe?u=761a52bbd46ab...
And the errors were gone!
This really made sense in the end, because posting the form should actually trigger the validation.

Sitecore Item Validators

I created an item validator with the validation rule template. I'm using it to check if one field for a date is after another field for a date following this tutorial: https://sitecorejohn.wordpress.com/2010/03/17/validate-that-the-value-of-one-datetime-field-follows-another-with-sitecore. For some reason the item that is pulled with GetItem() in my validator does not have the change that the content editor has made until the item is saved. I thought Sitecore.Data.Validators.BaseValidator.UpdateItem would take care of this but it seems that my control to validate is null. That makes sense since it is an item validator instead of a field validator but that means that if this fires off on blur content editors may see false error messages or not see error messages when they should. I'm also running into an issue where my Evaluate method is firing twice on save; once before the item is actually saved so GetItem() returns with the non-updated values and once after save which has the expected values. If anyone has any insight as to why this might be happening I'd like to know. I have a feeling that the validator executing twice on save might be a config issue but I didn't see anything very obvious in the pipeline.
To get the new value of the field that's being validated you can use BaseValidator's
GetControlValidationValue();

How to turn off error messages in visualforce?

I have a VisualForce page that has three functionalities,
Hence it has three 'public PageReference' subroutines,
That are called when an action happens in the page [i.e. button]
One of the functionalities requires user input [i.e. inputText],
So the main APEX code has a variable declaration for that input:
public String UserInput { get; set; }
Since each task is not really related,
When I hit the button on the other two functionalities,
I get a VisualForce error,
Because the inputText object has no user input,
How can I prevent that from happening,
Another way to solve this,
How do I turn off VisualForce Error messages?
So I can do error handling from APEX,
Looking at the debug log file,
The error is in VisualForce NOT APEX,
Thanks
If you don't want to fire validation you can use the immediate = true attribute on the CommandButton/CommandLink to bypass any validation. - This isn't optimal in my opinion
If you only want to submit part of a page and still fire validation have a look at the ActionRegion. This should allow you to wrap a particular Region for the action call. This is probably best for your purpose.
Sorry I don't have time to post full examples but that should point you in the right direction :)

How do I properly validate DateTime in a CRUD situation for a model within an MVC project?

I have users selecting a DateTime using a caldendar/clock popup that allows them to make a selection. However, if they were to go in and edit the date to read something like: 6/12/201 10:36:47 AM instead of 6/12/2012 10:36:47 AM my Action gets past the ModelState.IsValid check and I get an error when the Controller attempts to save the object to the database (rightly so).
How do I properly validate DateTime in this situation? (I want to prevent a user from even being able to 'submit' the form if the datetime is not properly formatted)
Note: I currently have in place jquery.validate and typically (fields not DateTime) I use #Html.ValidationMessageFor(model => model.Field) - This does not appear to do anything for my current DateTime fields when they are formatted inappropriately.
you can simply add date client side validator for you DateTime field using snipet like that:
$(element).rules('add', {
date: true,
});
where "element" is your input for DateTime field.
Are you sure you're importing all of the requisite validation js files? it sounds like your server validation is working OK but the client is doing nothing. I suggest reading the following post by Brad Wilson: link

MVC3, turn off field validation for a field not in a ViewModel

I have a Form in an MVC3 project. One of my input fields should accept HTML. Unfortunately I cannot have a ViewModel which this value maps to. The Field is autogenerated and read in automatically. I am getting the following error.
A potentially dangerous Request.Form value was detected from the client
Since there is no viewmodel, I cannot apply the [AllowHTML] attribute. Does anyone know a workaround that does not involve disabling validation for the entire page?
Thank You
Additional Information:
I can access the unvalidated value by doing the following:
using System.Web.WebPages;
using System.Web.Helpers;
.....Inside Controller....
string value = Request.Unvalidated("input-40");
The problem now is that the Request.Params collection throws an exception. I would like to access all the other values and have them be validated...just not that one. Is there a way for me to validate the other fields either explicitly or access a validated collection.
The following would be fine
string value = System.Web.Something.ValidateInput(Request.Unvalidated("input-41"));
Unfortunately I don't know where/if this method exists
You can try the ValidateInput(false) attribute:
[ValidateInput(false)]
public ActionResult YourAction(FormCollection yourCollection)
{
// your stuff
}
Use ValidateInput attribute for your action method. Seems to be unsafe but should work, cannot test it now.

Resources