Validate::validate_max_string_length( object $field )

Description #

Validate if length of a string is at least as defined.

Parameters #

  • $field
    object (Required) Instance of @see AP_Field object.

Source #

File: lib/class-validate.php

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
public static function validate_max_string_length( $field ) {
    $value = $field->value();
 
    if ( ! empty( $value ) && $field->get( 'max_length' ) ) {
        $max_length = $field->get( 'max_length', 10 );
        $value      = wp_strip_all_tags( $value );
        $value      = html_entity_decode( $value, ENT_XML1, 'UTF-8' );
 
        if ( mb_strlen( $value, 'utf-8' ) > $max_length ) {
            $field->add_error(
                'max-string-length',
                sprintf(
                    // Translators: placeholder contain field label.
                    __( 'Value provided in field %1$s must not exceeds %2$d characters.', 'anspress-question-answer' ),
                    $field->get( 'label' ),
                    $max_length
                )
            );
        }
    }
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment