0

I want to Number validation for my site, but not able to show custom validation message to user.

       'CVV' => [
            'label'    =>  'CVV Number',
            'type' => 'numerical',              
            'numberPattern'=> '^[0-9]{3,3}$', 
            'min'   =>  100, 
            'minLength'   =>  3, 
            'max'   =>  999,
            'maxLength'   =>  3,
            'required' => true,
            'integerOnly'   =>  true,
            'tooSmall'  =>  'CVV Number is too small (minimum is 3 characters).',
            'message'  =>  'CVV Number is too small (minimum is 3 characters).',
            'tooShort'  =>  'CVV Number is too small (minimum is 3 characters).',
            'tooBig'  =>    'CVV Number is too long (maximum is 3 characters).',
            'tooLong'  =>    'CVV Number is too long (maximum is 3 characters).',                
        ],

I tried to set

AttributeType::Number

Also. But it didn't work. The issue: in the case of numbers like less than 3 characters, I am getting error message:

CVV Number is too small (minimum is 100).

Please Can anyone help me to get this expected message. Thank You.

1
  • 1
    Can you add some more context here? Is this a custom validator class you've written or is this in a rules() method on a model?
    – Brad Bell
    Mar 14, 2016 at 17:33

1 Answer 1

1

Removed above code and added the custom validation function for CVV Validation

public function validateCVV($attribute) {
    $value = $this->$attribute;
    if(!preg_match('/^[0-9]{3}$/', $value)) {
        $message = Craft::t("Oops!  Try again with a valid CVV security code on the back of your card.");
        $this->addError($attribute, $message);
    }        
}

$rules[] = ['CVC', 'validateCVV'];

Thank You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.