Usage

Usage

It's fair simple. Call Field facade with field type as method name. Pass desired field slug as first argument and accepted options in second argument.

Field::text('text', [
    'title' => ['Text field']
]);

I've told you it will be simple :)

Field title

By default, title is generated based on slug. However, you can specify it with title argument.

Field::text('text', [
    'title' => ['Inspiring title']
]);

Field description

Description is displayed above input. It's usefull for providing additional explanations about field for the users.

Field::text('text', [
    'description' => 'This is a descriptive description about field.'
]);

Field default value

You can define initial field values with default argument.

Field::text('text', [
    'default' => "Why is the Rum Gone?"
]);

Defining children fields

Some fields like checkboxes or radios have multiple entries and you can specify it with items argument.

Field::checkboxes('checkboxes', [
    'items' => [
        'checkbox-1' => 'First checkbox',
        'checkbox-2' => 'Second checkbox'
    ]
]);

Defining children fields

If field handles children fields, they can be defined with children method and array of fields as argument.

Field::repeatable('repeatable')->children([
    Field::text('text'),
]);

Displaying fields in grid

Normally, each field takes the whole row. However you can change this behaviour with column argument.

For example, field with column set to 1-2 will take half of the row.

Field::text('text' [
    'column' => '1-2'
]);

Full grid units reference you can find in Pure documentation

Validating fields

Fields validation is done by Parsley and you can use it's various validators. Call validate method with array of validators as argument.

Field::text('quote')->validate(['required']);

Validators with arguments needs to be in specific schema, where name and argument are separated with colon: <name>:<argument>.

Field::text('quote')->validate(['max:10']);

Available validators

Validator Description
required Validates that a required field has been filled with a non blank value.
type:email Validates that a value is a valid email address.
type:url Validates that a value is a valid url.
type:number Validates that a value is a valid number.
type:integer Validates that a value is a valid integer.
type:digits Validates that a value is only digits.
type:alphanum Validates that a value is a valid alphanumeric string.
min:6 Validates that a given number is greater than or equal to some minimum number.
max:6 Validates that a given number is less than or equal to some maximum number.
rage:[6, 12] Validates that a given number is between some minimum and maximum number.
mincheck:3 Validates that a certain minimum number of checkboxes in a group are checked.
maxcheck:3 Validates that a certain maximum number of checkboxes in a group are checked.
check:[1, 3] Validates that the number of checked checkboxes in a group is within a certain range.
pattern:\d+ Validates that a value matches a specific regular expression (regex).

Sanitizing fields values

Fields values can be sanitized before saving to the database. Use sanitize method with Closure as argument. Callback needs to return field value, of course after desirable modifications.

Field::text('quote')->sanitize(function ($value) {
    return ucfirst($value);
});

You can also use WordPress buildin sanitization functions

Field::text('quote')->sanitize('sanitize_text_field');

When you call sanitize method without any arguments, the sanitize_text_field function will be used.

// uses sanitize_text_field()
Field::text('quote')->sanitize();