I'm currently trying to work through MVC validation, and am coming up against some problems where a field is required depending on the value of another field. An example is below (that I haven't figured out yet) - If the PaymentMethod == "Cheque", then the ChequeName should be required, otherwise it can be let through.
[Required(ErrorMessage = "Payment Method must be selected")]
public override string PaymentMethod
{ get; set; }
[Required(ErrorMessage = "ChequeName is required")]
public override string ChequeName
{ get; set; }
I'm using the System.ComponentModel.DataAnnotations for the [Required], and have also extended a ValidationAttribute to try and get this working, but I can't pass a variable through to do the validation (extension below)
public class JEPaymentDetailRequired : ValidationAttribute
{
public string PaymentSelected { get; set; }
public string PaymentType { get; set; }
public override bool IsValid(object value)
{
if (PaymentSelected != PaymentType)
return true;
var stringDetail = (string) value;
if (stringDetail.Length == 0)
return false;
return true;
}
}
Implementation:
[JEPaymentDetailRequired(PaymentSelected = PaymentMethod, PaymentType = "Cheque", ErrorMessage = "Cheque name must be completed when payment type of cheque")]
Has anyone had experience with this sort of validation? Would it just be better to write it into the controller?
i am working an asp.net mvc 2 web app using model metadata, templates and client side validation using jquery. I have noticed that the boolean and datetime fields of my model are treated as required from the model metadata, even though i haven't applied any Required attribute on them. This is not a problem for server side validation, but when using the client side validation (<%Html.EnableClientValidation(); %>) on my edit model page, the validation fails and i cannot post the model changes. Specifically, when setting the boolean property to unchecked (false), the validation fails and the "Field is required" message appears.
The only workaround i have found so far is to override the default Editor templates for my models and write my own.
I am using the CreateUserWizard in my VB.Net 3.5 web app. I added my own custom fields (First Name, Last Name...) to the form by using the CreateUserWizardStep and the CreateUserWizard1_CreatedUser event. Everything inserts fine into the aspnet_ tables and my own table. However, I cannot get the form validation to work. In other words, the form is ignoring the RequiredFieldValidators that I added.
I'm working on a very dynamic site build at the moment. What I'm trying to do is creating something like a survey that can be created dynamically from a control panel. In the control panel you add input fields (these are saved in a database), what the user then see is a form that I generate from the database. So if I add 3 input fields to the database the survey will contain 3 fields. If I add 20 fields the survey will have 20 fields.
Now my problem is that I want to validate these fields, and I would like to be able to hook me in with the standard validation flow. I can't create a Model with validation rules since the number of fields and their names are dynamic the only thing I know is what kind of data that is expected in every field (this rule is found in the database). In an ordinary case I would get the automatic highlighted fields that are not valid and so on thanks to the built in validation flow with ValidationResult and so on.
So the question now is can I somehow simulate parts of the validation and then hook me back in with the validation result, and if not valid, the form prints the error messages and fill the fields with the data that was given?
I am a completely new to ASP but have been given the task of creating some simple forms for my employer. how I could validate the Email, Website fields? And also make the Post Code a required field?
am trying to use JQuery validation plugin to validate some fields on one page.at page also has a link button which submits the page to server. Now the problem that I am facing is jQuery validator doesn't fire when I submit the page with that link button. If I change the link button to normal button or image button then validation works fine. I can't change the control on the page and have to use the link button.
How can I conditionally disable client-side validation of some fields ?
Here is the example of what I need to do:
user writes name and address and submits. Both are validated both server and client side. If validation passes e-mail is send to given address and (name, address) pair is stored.as a response same page is displayed (I don't want redirect!) this time with confirmation. User can retype their name and submit. In such case e-mail will be resend to the address that corresponds to given name.
It does not work because: client side validation will not pass when user clicks resent button on the second (confirmation screen). It would say that e-mail is required even if there is no field for it. How can I disable javascript validating email before the confirm page gets send to user ?
Example ASP.NET MVC page
<%if (Model.Confirm == false){ %> <% using (Html.BeginForm()) { %> Your email: <%: Html.EditorFor(m => m.Email) %> Your name: <%: Html.EditorFor(m => m.Name) %> <input type="submit" value="Send Email" />
I'm writing a small web application in c# and i'm running into a small problem.
Basically, on page_load() my backend MySQL db is queried to bring back the user data, and that is then inserted into the textbox controls as below:
[Code]....
Now the problem occurs when the RequiredFieldValidator kicks in. In my form processing function, I call Page.IsValid(), it fails and displays the required message next to the required fields.
I was wondering if there is a workaround for this, or a better way to go about what i'm trying to do.
I'm using MVC 2 with some Models from a LinqToSql project that I built. I see that when I post back to a Controller Action after editing a form that has a DateTime field from the Model, the MVC Html.ValidationMessageFor() helper will nicely display an error beside the Date text box. This seems to happen automatically when the you test ModelState.IsValid() in the Controller Action, as if the MVC model binding automatically knows that the DateTime field cannot be empty.
My question is... I have some other string fields in these LinqToSql generated classes that are Not-Nullable (marked as Not Nullable in Sql Server which passes thourgh to the LinqToSql generated classes), so why doesn't Mr. MVC pick up on those as well and display a "Required" message in the ValidationMessageFor() placeholders I have added for those fields?
Sure, I have successfully added the MetadataType(typeof<t>) buddy classes to cover these Non-nullable string fields, but it sure does seem redundant to add all this metadata in buddy classes when the LinqToSql generated classes already contain enough info that MVC could sniff out. It MVC validation works with DateTime automatically, why not these Not-nullable fields too?
I have a form where users enter their preferred contact method. It has:
- a radio button where a user selects whether they'd like to be contact by phone or email or either
- a textbox for the user's email address
- a textbox for the user's phone number
I have validation via a custom attribute that verifies that if the user selected phone they entered a phone number, if they selected email they entered an email, etc... but it's at a class level on my ViewModel. As such the validation doesn't mark the expected field as invalid.
Is there a way that my IsValid override (or some other method) can mark the child property's value as invalid so I get highlighting?
When I Login the page using username and password  and then click the logout button and after again click Login button without entering username and password it will be login,I want to show error
I have a textbox and a datepicker control.The validator requires that neither can be selected. ie. both blank. However if one has a value the other must also have a value.
Can anybody tell me how to implement this given that a CustomValidator requires a RequiredFieldValidator to fire?
I'm working on an application (a web application, asp.net and c#) which is datetime-dependent, so, based on the current date, it will launch forms for the logged user to fill in.
I've been thinking about how we're going to simulate real usage of the application, for debugging and testing purposes.
public class MyDateClass { private DateTime _currentDate; public DateTime GetCurrentDate() { // get the date, which may be different from DateTime.Now return _currentDate; } public void SetCurrentDate(DateTime newCurrentDate) { // set the date to the value chosen by the user _currentDate = newCurrentDate; } }
allowing me to set the current data, by invoking the SetCurrentDate method, for example, in the code-behind of a link button and a calendar input.
how should I exactly store the DateTime variable, throughout all the application? I can't work with the session in this class, right? Should I work with the Thread?
I have this "how to" type of question. I have a page where there are four gridviews. Now these four grids pull data from four different tables. The 1st grid retrieves data from table 1 with a where clause in it's query that comes from a hidden fileld value in the page. Till this things are fine. However, the 2nd grid has a dependency on the 1st grid and the where clause in it's query should be the data selected in grid view1. The 3rd depends on the 2nd one..and so on.
I have a shopping cart. Basically when looking at the product i want to display certain textbox and fields. e.g. if shoes then display the sizes available with qunaityt box against each
what is the best approach for this?
create textboxes dynamically or have static on screen and hide and unhide depending on value
I'm currently using a GridView and I want to set the CssClass for the Row depending on a property of the object that the row is being bound to.I tried the following:
<asp:GridView id="searchResultsGrid" runat="server" AllowPaging="true" PageSize="20" AutoGenerateColumns="false"> <!-- The following line doesn't work because apparently "Code blocks aren't allowed in this context --> <RowStyle CssClass="<%#IIF(DataBinder.Eval(Container.DataItem,"NeedsAttention","red","") %> <Columns> <!--............--> </Columns> </asp:GridView>
Now I could simply handle theGridView's RowDataBound event and change the css class of the row there...but I'm trying to keep a clear separation between the UI and the page/business logic layers.
I'm working in an environment where a single web application (running in a web-farm) dynamically connects to different SQL Server databases (possibly on different servers) based on user selection (this part is non-negotiable, unless you can gift me $1,000,000 and/or some kind of time altering device).
I'd like to cache some of the commonly used data from the database(s) in the application, such as "settings", and am looking for a best practice to use with the SqlCacheDependency class or a related database cache dependent solution (not opposed to a custom implementation of SQL notification or polling; notification is preferred over polling).
So far, I'm thinking of using a collection of SqlCacheDependency objects (one for each database) that can be referenced by connection string name or related key. So when the connection is changed to a different database, the cache can be requested/populated by a key of <databasekey>+<datakey> and use the appropriate SqlCacheDependency object (probably handled by a cache-managing wrapper).Or is it just better to take the hit and always go to the database?
My website will target UK and US markets, and i need to display a different price/currency for both markets - what is the best way around this?
1) a simple drop down box asking users preference? 2) Using culture class in asp .net to auto detect eg System.Threading.Thread.CurrentThread.CurrentUICulture
I'm struggling with a web form where I have created one date field and couple of drop down lists. These drop down lists populate themselves from a backend MS ACCESS database file. My problem is that I am not able to make the two drop down menus dependent in a manner that when the first drop down menu is selected the second should be populated accordingly?