Using DataAnnotations to compare two model properties

Make sure that your project references system.web.mvc v3.xxxxx. Then your code should be something like this: using System.Web.Mvc; . . . . [Required(ErrorMessage = “This field is required.”)] public string NewPassword { get; set; } [Required(ErrorMessage = “This field is required.”)] [Compare(nameof(NewPassword), ErrorMessage = “Passwords don’t match.”)] public string RepeatPassword { get; set; }

Django’s ModelForm unique_together validation

I solved this same problem by overriding the validate_unique() method of the ModelForm: def validate_unique(self): exclude = self._get_validation_exclusions() exclude.remove(‘problem’) # allow checking against the missing attribute try: self.instance.validate_unique(exclude=exclude) except ValidationError, e: self._update_errors(e.message_dict) Now I just always make sure that the attribute not provided on the form is still available, e.g. instance=Solution(problem=some_problem) on the initializer.

Laravel: Validate maximum File size?

According to the documentation: $validator = Validator::make($request->all(), [ ‘file’ => ‘max:500000’, ]); The value is in kilobytes, for example: max:10240 = max 10 MB. max:1 = max 1024 bytes. Note that there are efforts to change the value of 1 kilobytes from original 1024 to 1000 bytes, but major frameworks like Laravel remain using original … Read more

how to validate a URL / website name in EditText in Android?

Short answer Use WEB_URL pattern in Patterns Class Patterns.WEB_URL.matcher(potentialUrl).matches() It will return True if URL is valid and false if URL is invalid. Long answer As of Android API level 8 there is a WEB_URL pattern. Quoting the source, it “match[es] most part of RFC 3987”. If you target a lower API level you could … Read more

How to cleanse (prevent SQL injection) dynamic SQL in SQL Server?

I believe there are three different cases that you have to worry about: strings (anything that requires quotes): ”” + replace(@string, ””, ”””) + ”” names (anything where quotes aren’t allowed): quotename(@string) things that cannot be quoted: this requires whitelisting Note: Everything in a string variable (char, varchar, nchar, nvarchar, etc.) that comes from user-controlled … Read more

Validate data using DataAnnotations with WPF & Entity Framework?

You can use the DataAnnotations.Validator class, as described here: http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx But if you’re using a “buddy” class for the metadata, you need to register that fact before you validate, as described here: http://forums.silverlight.net/forums/p/149264/377212.aspx TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(myEntity), typeof(myEntityMetadataClass)), typeof(myEntity)); List<ValidationResult> results = new List<ValidationResult>(); ValidationContext context = new ValidationContext(myEntity, null, null) bool valid = Validator.TryValidateObject(myEntity, context, … Read more

How can I void a form action and execute jQuery when all HTML form elements are validated?

According to this: Do any browsers yet support HTML5’s checkValidity() method?, and this may not be the latest truth since HTML5 is a work in progress, the Form.checkValidity() and element.validity.valid should let you access validation information from JavaScript. Assuming that’s true, your jQuery would need to attach itself to the form submit and make use … Read more