diff --git a/classes/fields/paragraph.php b/classes/fields/paragraph.php index 36541afc21..6167cd6693 100644 --- a/classes/fields/paragraph.php +++ b/classes/fields/paragraph.php @@ -146,6 +146,12 @@ public function options() { 'type' => 'number', 'help' => __( 'Set to -1 for no limit', 'pods' ), ], + static::$type . '_min_length' => [ + 'label' => __( 'Minimum Length', 'pods' ), + 'default' => 0, + 'type' => 'number', + 'help' => __( 'Set to 0 for no minimum', 'pods' ), + ], static::$type . '_placeholder' => [ 'label' => __( 'HTML Placeholder', 'pods' ), 'default' => '', @@ -279,6 +285,49 @@ public function input( $name, $value = null, $options = null, $pod = null, $id = } + /** + * {@inheritdoc} + */ + public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { + $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params ); + + $errors = []; + + if ( is_array( $validate ) ) { + $errors = $validate; + } + + $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); + + if ( is_array( $check ) ) { + $errors = array_merge( $errors, $check ); + } else { + if ( '' !== $value && '' === $check ) { + if ( $this->is_required( $options ) ) { + $errors[] = __( 'This field is required.', 'pods' ); + } + } + + $min_length = (int) pods_v( static::$type . '_min_length', $options, 0 ); + + if ( 0 < $min_length ) { + $len = pods_mb_strlen( (string) $check ); + + if ( 0 < $len && $len < $min_length ) { + $label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ); + // translators: %1$s is the field label, %2$d is the minimum number of characters. + $errors[] = sprintf( __( '%1$s must be at least %2$d characters long.', 'pods' ), $label, $min_length ); + } + } + } + + if ( ! empty( $errors ) ) { + return $errors; + } + + return $validate; + } + /** * {@inheritdoc} */ diff --git a/classes/fields/text.php b/classes/fields/text.php index 314a872e2a..0ee7fe5878 100644 --- a/classes/fields/text.php +++ b/classes/fields/text.php @@ -101,6 +101,12 @@ public function options() { 'type' => 'number', 'help' => __( 'Set to -1 for no limit', 'pods' ), ], + static::$type . '_min_length' => [ + 'label' => __( 'Minimum Length', 'pods' ), + 'default' => 0, + 'type' => 'number', + 'help' => __( 'Set to 0 for no minimum', 'pods' ), + ], static::$type . '_placeholder' => [ 'label' => __( 'HTML Placeholder', 'pods' ), 'default' => '', @@ -200,6 +206,18 @@ public function validate( $value, $name = null, $options = null, $fields = null, $errors[] = __( 'This field is required.', 'pods' ); } } + + $min_length = (int) pods_v( static::$type . '_min_length', $options, 0 ); + + if ( 0 < $min_length ) { + $len = pods_mb_strlen( (string) $check ); + + if ( 0 < $len && $len < $min_length ) { + $label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ); + // translators: %1$s is the field label, %2$d is the minimum number of characters. + $errors[] = sprintf( __( '%1$s must be at least %2$d characters long.', 'pods' ), $label, $min_length ); + } + } } if ( ! empty( $errors ) ) { diff --git a/classes/fields/wysiwyg.php b/classes/fields/wysiwyg.php index 61fec5f231..aea1b544c8 100644 --- a/classes/fields/wysiwyg.php +++ b/classes/fields/wysiwyg.php @@ -170,6 +170,12 @@ public function options() { 'type' => 'text', 'help' => __( 'Format: strong em a ul ol li b i', 'pods' ), ], + static::$type . '_min_length' => [ + 'label' => __( 'Minimum Length', 'pods' ), + 'default' => 0, + 'type' => 'number', + 'help' => __( 'Set to 0 for no minimum', 'pods' ), + ], ]; return $options; @@ -402,6 +408,49 @@ function_exists( 'did_filter' ) $this->render_input_script( $args ); } + /** + * {@inheritdoc} + */ + public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { + $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params ); + + $errors = []; + + if ( is_array( $validate ) ) { + $errors = $validate; + } + + $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); + + if ( is_array( $check ) ) { + $errors = array_merge( $errors, $check ); + } else { + if ( '' !== $value && '' === $check ) { + if ( $this->is_required( $options ) ) { + $errors[] = __( 'This field is required.', 'pods' ); + } + } + + $min_length = (int) pods_v( static::$type . '_min_length', $options, 0 ); + + if ( 0 < $min_length ) { + $len = pods_mb_strlen( (string) $check ); + + if ( 0 < $len && $len < $min_length ) { + $label = pods_v( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ); + // translators: %1$s is the field label, %2$d is the minimum number of characters. + $errors[] = sprintf( __( '%1$s must be at least %2$d characters long.', 'pods' ), $label, $min_length ); + } + } + } + + if ( ! empty( $errors ) ) { + return $errors; + } + + return $validate; + } + /** * {@inheritdoc} */