Validating Fields

When writing field code, whether with Piklist or not, you should be extra cautious of how you handle data coming into WordPress and how it’s presented to the end user. Piklist makes validating your data easy with the validate parameter.

You can also create your own custom validation rules using the piklist_validation_rules filter.

Choose the appropriate type of validation for each of your fields.

email

Verifies that the input is in the proper format for an email address.

Uses the standard WordPress function is_email

'validate' => array(
      array(
        'type' => 'email'
      )
    )

email_domain

Verifies that the input is a valid email domain.

Uses the standard PHP function checkdnsrr

'validate' => array(
      array(
        'type' => 'email_domain'
      )
    )

email_exists

Checks that the entered email is not already registered to another user.

Uses the standard WordPress function email_exists

'validate' => array(
      array(
        'type' => 'email_exists'
      )
    )

file_exists

Verifies that the file path entered leads to an actual file.

'validate' => array(
      array(
        'type' => 'file_exists'
      )
    )

hex_color

Verifies that the data entered is a valid hex color.

'validate' => array(
      array(
        'type' => 'hex_color'
      )
    )

image

Verifies that the file path entered leads to an image file.

Uses the standard PHP function exif_imagetype

'validate' => array(
      array(
        'type' => 'image'
      )
    )

ip_address

Verifies that the data entered is a valid IP Address.

'validate' => array(
      array(
        'type' => 'ip_address'
      )
    )

limit

Verifies that the number of items are within the defined limit.

Options

min

The minimum allowed value. default: 1

max

The maximum allowed value default: infinite

count

Determines what you want to count: words, characters, files, images, add-more’s

values: words, characters

default: false (files, images, add-more’s)

,'validate' => array(
      array(
        'type' => 'limit'
        ,'options' => array(
          'min' => 2
          ,'max' => 2
        )
      )
    )
,'validate' => array(
      array(
        'type' => 'limit'
        ,'options' => array(
          'min' => 2
          ,'max' => 6
          ,'count' => 'characters'
        )
      )
    )

match

Checks to see if two fields match.

Options

field

The field name to match

piklist('field', array(
    'type' => 'password'
    ,'scope' => 'user'
    ,'field' => 'user_pass_repeat'
    ,'label' => 'Repeat New Password'
    ,'value' => false // Setting to false forces no value to show in form.
    ,'validate' => array(
      array(
        'type' => 'match'
        ,'options' => array(
          'field' => 'user_pass' // does this match the field user_pass_repeat
        )
      )
    )
  ));

range

Verifies that the data entered is within the defined range.

Options

min

The minimum allowed value. default: 1

max

The maximum allowed value default: 10

,'validate' => array(
      array(
        'type' => 'range'
        ,'options' => array(
          'min' => 2
          ,'max' => 8
        )
      )
    )

safe_text

Verifies that the data entered is alphanumeric.

,'validate' => array(
      array(
        'type' => 'safe_text'
      )
    )

url

Verifies that the data entered is a valid URL.

,'validate' => array(
      array(
        'type' => 'url'
      )
    )

username_exists

Checks that the entered username does not already exist.

Uses the standard WordPress function username_exists

,'validate' => array(
      array(
        'type' => 'username_exists'
      )
    )

Have ideas for improving the documentation?

This documentation is a community effort. Please create an issue or pull request to help!

Improve this page