How to force MVC to Validate IValidatableObject

You can manually call Validate() by passing in a new instance of ValidationContext, like so: [HttpPost] public ActionResult Create(Model model) { if (!ModelState.IsValid) { var errors = model.Validate(new ValidationContext(model, null, null)); foreach (var error in errors) foreach (var memberName in error.MemberNames) ModelState.AddModelError(memberName, error.ErrorMessage); return View(post); } } A caveat of this approach is that in … Read more

How to inject in @FacesValidator with @EJB, @PersistenceContext, @Inject, @Autowired

JSF 2.3+ If you’re already on JSF 2.3 or newer, and want to inject CDI-supported artifacts via e.g. @EJB, @PersistenceContext or @Inject, then simply add managed=true to the @FacesValidator annotation to make it CDI-managed. @FacesValidator(value=”emailExistValidator”, managed=true) JSF 2.2- If you’re not on JSF 2.3 or newer yet, then you basically need to make it a … Read more

Spring MVC 3 Validation – Unable to find a default provider

If you are using Maven, you must add a dependency to the Hibernate Validator Annotation Processor. <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator-annotation-processor</artifactId> <version>4.1.0.Final</version> </dependency> You can find it in the JBoss repository : <repository> <id>JBoss Repository</id> <url>https://repository.jboss.org/nexus/content/repositories/releases</url> <name>JBoss Repository</name> </repository>

How do I validate that two values do not equal each other in a Rails model?

Create custom validataion: validate :check_email_and_password def check_email_and_password errors.add(:password, “can’t be the same as email”) if email == password end But keep in mind that storing password as a plain text is bad idea. You should store it hashed. Try some authentication plugin like authlogic or Restful authentication.

Rails 3 validates inclusion of when using a find (how to proc or lambda)

Just use a proc, like so: validates :currency_code, :presence => true, :inclusion => { :in => proc { Currency.all_codes } } validates :country_code, :presence => true, :inclusion => { :in => proc { Country.all_codes } } It’s worth noting for anyone else who may stumble across this that the proc also has the record accessible … Read more

How to validate timestamp in javascript

You can validate if a string is a valid timestamp like this: var valid = (new Date(timestamp)).getTime() > 0; var valid = (new Date(‘2012-08-09’)).getTime() > 0; // true var valid = (new Date(‘abc’)).getTime() > 0; // false Revisiting this answer after many years, validating dates can still be challenging. However, some of the argument is … Read more

Javascript regular expression to validate URL

This validate the URL in general console.log(‘http://www.google-com.123.com’, validateUrl(‘http://www.google-com.123.com’)); // true console.log(‘http://www.google-com.123’, validateUrl(‘http://www.google-com.123’)); // false console.log(‘https://www.google-com.com’, validateUrl(‘https://www.google-com.com’)); // true console.log(‘http://google-com.com’, validateUrl(‘http://google-com.com’)); // true console.log(‘http://google.com’, validateUrl(‘http://google.com’)); //true console.log(‘google.com’, validateUrl(‘google.com’)); //false console.log(‘http://www.gfh.’, validateUrl(‘http://www.gfh.’)); //false console.log(‘http://www.gfh.c’, validateUrl(‘http://www.gfh.c’)); //false console.log(‘http://www.gfh:800000’, validateUrl(‘http://www.gfh:800000’)); //false console.log(‘www.google.com ‘, validateUrl(‘www.google.com ‘)); //false console.log(‘http://google’, validateUrl(‘http://google’)); //false console.log(‘//cdnblabla.cloudfront.net/css/app.css’, validateUrl(‘//cdnblabla.cloudfront.net/css/app.css’)); //true function validateUrl(value) { return /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(value); } Should … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)