I have a date field in my grid. I can't seem to figure out how can I turn off the validation if a user doesn't enter a date. In my data model the date property is not decorated with required attribute. Also in the database that I am using the date column is allowed a null and has a default value. I also turned off the Client Validation in the web config file. But still can't figure out why the date field has client validation turned on. The telerik version I am using is 2011.2.712.340.
So I was able to find out how to turn off the client validation if the user is not required to enter a date while filling a form. When declaring the data model if you have the property to as nullable:
public DateTime? YourDateProperty {get; set;}
then you can edit or insert a new record. In my case I had the date field as one of the columns in the grid and I was having problems where I could not insert a new record without the user having to enter a date.
Related
I have a model that includes the following property:
[Required]
public string City { get; set; }
Basically, a user selects a stored value from a dropdown. The fields in my partial view are populated with the corresponding data. In certain circumstances, the stored data will not have a value for City. I am trying to use jQuery to check that if the field is empty, to basically not require the field and to allow submission. I have tried the following:
$("#City").attr("data-val", "false");
$("#City").rules("remove", "required");
$("#City").attr("disabled", "disabled");
$("#City").attr("aria-required", "false");
$("#City").removeAttr("required");
None of these work. On submit, the proper method is hit in the controller with a ModelState.IsValid of false and an error stating that "The City field is required."
How can I use jQuery to prevent this field from being validated on submit?
Before checking whether the ModelState.IsValid, you can remove the errors on that property of the Model ModelState["City"].Errors.Clear(); Therefore meaning regardless of what errors have occurred on your City property, the model state will be valid.
Note, this fix should be applied in the controller on the server - it is not related to jQuery.
Even though there were some great suggestions from posters, I ended up reevaluating our needs and found that the Required attribute was not required for that specific property.
Thanks for your suggestions everyone.
I've got 3 custom DropDowns for DateTime model properties, each representing Day/Month/Year that set a hidden input with each value, with an also custom model binder that works fine.
I also have created a RequiredIf validation attribute with client side validation and it works fine both server and client side adding the corresponding data-val-requiredif attributes to the hidden inputs where the selected DropDown value is stored.
The problem comes when I use this RequiredIf attribute on DateTime property using the custom control.
[RequiredIf(...)]
public DateTime Start { get; set; }
and the issue is in each dropdown hidden input generated with Html.HiddenFor (binded to Start.Month in this example):
<input data-val="true" data-val-number="The field Month must be a number." id="Start_Month" name="Start.Month" type="hidden" value="">
The problem is pretty clear, the validation attribute is on the DateTime property, not it's Month subproperty, so the data-val-requiredif... validation attributes are not set on that input.
Question:
What is the best approach to make Day/Month/Year inherit the dataannotation validation attributes from it's DateTime model?
I wouldn't use data attribute names with multiple hyphens like your "data-val-number". I've also had issues with upper case so always use one hyphen after data and always lower case.
If you're passing the date back to a controller in separate pieces (i.e. month / day / year), you'll have to put it back together in a DateTime format on the server for the validation to work.
If you're putting the date together in a JSON model to pass back to the controller, MVC will convert the JSON to a model object for you. Make sure your JSON model is exactly the same as what the controller parameter is expecting.
I'm creating an ASP.NET MVC3 application, and I have a form where user have to enter a price. For improving user experience, I'd like to implement a thousand separator. I've done it, you can see it working on this FIDDLE.
BUT
This Price field is part of a model and is declared in my Model file as
public decimal? Price {get; set;}
When I submit my form, Price is always null. However, it has the value entered in the form if I remove my thousand separator. This is because Price seems to be interpreted as a string, and not as a decimal, so MVC doesn't match the two properties (I suppose).
So, how can force MVC to recognize my value as a correct decimal and submit a model with the price entered by the user ?
Thanks for your help.
I believe it will be easier to make the following: you can either create a hidden field on the form and store the number without separators or you can remove separators right before submitting the form.
Example:
$("#target-form").submit(function(event) {
value_with_separators = $('.thousandSeparator').val();
clean_value = value_with_separators.replace(/\s+/g, '');
$('.thousandSeparator').val(clean_value);
});
There's also another way. You can implement custom ModelBinder and perform required conversions there. Here is a link to a related question with code samples.
Hope it helps!
I'm trying to make a page where the user can search for articles. There is an option to limit the search based on a FromDate and a ToDate. Both of these are DateTime.
The user is asked to enter the date on the form dd.MM.yyas this is normal in our country. I can see the date in the URL after submitting the form and it looks as I want it to. The problem however is that MVC3 assumes the format is MM.dd.yy. How can I change this?
Some code:
This is the Razor code I use
<div class="toDate" >
<label>til dato</label>
#Html.TextBoxFor(m => m.DateTo, new { placeholder = "dd.mm.yy" })
</div>
And in the model all I have is:
public DateTime DateTo { set; get; }
I have tried using EditorFor, but I lose my placeholder text.
In short: How do I make my MVC3 model accept a DateTime input from the user on the form dd.MM.yy
If my question isn't clear enough, let me know and I'll try to clarify.
EDIT:
It appears that I need some clarification, so I'll try to do that by making a scenario:
The user enters his search criteria. He sets a DateFrom and a DateTo in two text boxes.
The form is submitted and posted back to the server.
On the server the DateTime object is treated on the form MM.dd.yy. I wish for it to be interpreted as dd.MM.yy.
I hope that helps.
[DisplayFormat(DataFormatString = "{0:dd MM yy}")]
pubilc DateTime DateTo { get; set }
Try setting up a custom validation attribute where you validate the date format according to your liking.
Here's a nice tutorial for it: http://msdn.microsoft.com/en-us/gg618485 (It's not as difficulty as it might seem at first, and can be really helpful).
Alternatively just build a regex validation attribute with something like:
RegularExpression(#"[0-3][0-9]\.[0-1][0-9].[0-9][0-9]")]
Above regex isn't perfect, but serves as an example.
Edit: If 3nigma's solution works for you that is obviously highly preferable to mine. :)
I have a business requirement where in i need to display SSN Field into three separate text boxes and if any one of the textbox is blank and user clicks save, I need to display a common error message on top of the SSN Field saying- "SSN is required field". I have added required field attribute on the view model on all these fields so i am getting three edit messages saying SSN is required. how to present only one edit in this case ?
I am using ASP.NET MVC 2
My view model is like this
[Required]
SSN_FirstThreeDigits
[Required]
SSN_SecondTwoDigits
[Required]
SSN_ThirdFourDigits
Any suggestions
Thanks
Subu
SSNValidator.com now has an API for validating social security numbers. Data elements include whether or not the number was issued, approximate date of issuance, state of issuance and if it was used in a death claim. Go to the site and click the API/Batch Processing links for details.
The Mvc.ValidationTookit Alpha Release: Conditional Validation with MVC 3 can do this.
You may need a custom model binder. Please have a look at this article and see how he validates date of birth