Custom Error Message for PriceInCents – Vue3 and Vuelidate

Hey everyone,

This is a quick post to show how you can add a custom error message when using Vuelidate in Vue3. In my case I have a price field that should not be greater than $1000. The issue is that I store the amount in cents which results in the following error:

This is obviously a little ambiguous to the user who isn’t aware that everything is running in cents behind the scenes. In order to avoid the issue I used a helper in @vuelidate/validates:

// Import the following in your component
import {
  required,  
  maxValue,
  helpers, // Helpers provides access to the message customisation
} from "@vuelidate/validators";

...

// Add the following validation rule
priceInCents: {
          required,
          maxValue: helpers.withMessage("Price cannot be more than $1000.00" , maxValue(100000)),
        },

With the new rule in place the error is much more useful:

While it doesn’t seem to show up on Google too well this functionality is documented by Vuelidate on the following page: https://vuelidate-next.netlify.app/custom_validators.html#custom-error-messages

OpenXml Validation Spreadsheet Value Between Two Numbers – C#

Hi everyone,

Just a quick post on how to validate a numeric cell to ensure that the value is between two numbers when using OpenXml:

// Restrict min and max values
var dataValidations = new DocumentFormat.OpenXml.Spreadsheet.DataValidations { Count = 0 };
DocumentFormat.OpenXml.Spreadsheet.DataValidation dataValidation;
dataValidation = new DocumentFormat.OpenXml.Spreadsheet.DataValidation {
Type = DataValidationValues.Decimal,
AllowBlank = false,
SequenceOfReferences = new ListValue() { InnerText = $”{columnName}2:{columnName}1048576″ },
Operator = DataValidationOperatorValues.Between,
Formula1 = new Formula1 { Text = “10 “},
Formula2 = new Formula2 { Text = “20 “},
};
dataValidations.Append(dataValidation);
dataValidations.Count++;
worksheetPart.Worksheet.Append(dataValidations);

The snippet above will ensure that any values are between 10 and 20. I didn’t come across any examples for this while searching, so hopefully this will be able to help someone else out!

Thanks,
Chris

Change Default MVC5 Password Complexity Requirements – Passwords must have at least one non letter or digit character. Passwords must have at least one digit (‘0’-‘9’). Passwords must have at least one uppercase (‘A’-‘Z’)

Hey everyone,

I’ve started on a new MVC5 project and came across the following error message while trying to register a new user:

Passwords must have at least one non letter or digit character.
Passwords must have at least one digit (‘0’-‘9’).
Passwords must have at least one uppercase (‘A’-‘Z’)

While having a secure password is obviously important, I felt that most users would probably find these requirements a little extreme. After a bit of Googling I came across a StackOverflow post that mentioned a config class that you can use to edit these settings:

// App_Start > IdentityConfig.cs

...

// Configure validation logic for usernames
            manager.UserValidator = new UserValidator(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };

...

Detecting FileSize – jQuery

Hey everyone,

Just a quick post on how to detect a file’s size with jQuery. I’m currently using a version of this for basic client side validation:



    Upload image:
    

$(document).ready(function(){
    $('#image-file').bind('change', function() {
        
        var fileSize = this.files && this.files.length > 0 && this.files[0].size ? this.files[0].size / 1024 / 1024 : 0;
        
        if(fileSize > 0){
            $('body').append('
' + fileSize + ' MB
'); } else{ $('body').append('
' + fileSize + ' MB
'); } }); });
.file{
padding: 5px 7px;
border-radius: 3px;
background-color: green;
color: white;
margin: 5px;
}

.file.valid{
background-color: rgb(127, 197, 127);
}

.file.invalid{
background-color: rgb(197, 127, 127);
}

Here’s the link to the full fiddle: http://jsfiddle.net/nLLMF/

There’re also a whole heap of other options listed on this StackOverflow post if you’re chasing something a bit more robust: http://stackoverflow.com/a/13249924/522859

http://jsfiddle.net/nLLMF/embedded/

preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash – CakePHP

Hey everyone,

Ran into the following error while I was mucking around with CakePHP today:


preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash

The error seemed a little weird as I wasn’t using any regex. It turned out that it was because of my custom validation methods. I had marked them as private instead of public. A bit of an ambiguous error message so hopefully this will be able to help some of you out!

Validating Boolean Value of False in Rails

I came across a bit of a weird one this afternoon when trying to validate. Several of my fields appeared to be incorrectly flagged as not having a value.

This is the validation I was using:

#Define validation
validates :order_id, :user_id, :acknowledged, :completed, :presence => true

 

The fields that were flagging as not present were :acknowledged and completed. After hardcoding the values and still not getting anywhere I realised that while values were being provided, they were boolean values of false. Apparently :presence will treat a value of false as not being present.

After another Google search I came across this post which shows how to validate a boolean value.

When implemented within my app, the new validation looks like this:

#Define validation
validates :order_id, :user_id, :presence => true
validates :acknowledged, :completed, :inclusion => {:in => [true, false]}

Hopefully that’ll be able to help a few of you out as well.

Validation Error Messages not Displaying – Ruby on Rails

Just another quick problem I ran into, the validation was indicating that an error had occurred however it did not show what the error was:

1 error prohibited this message from being saved:

— Usually an error would appear here —

Turns out the problem was pretty straight forward – it’s always the little things! I’d simply missed the each when displaying the error messages:


  
    

prohibited this message from being saved:

Simply adding the correct code fixed the problem straight away.

1 error prohibited this message from being saved:

Country can’t be blank

Good Luck!