Skip to content

Commit

Permalink
remove 'required' key, explain how to add labels in validation messag…
Browse files Browse the repository at this point in the history
…es (#439)

* remove 'required' key as it is not used and duplicates rules within validation key; explain support for field names in validation messages

* update documentation accordingly
  • Loading branch information
dgvirtual authored Mar 18, 2024
1 parent fb023dd commit eb46bff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
27 changes: 21 additions & 6 deletions docs/users_and_security/user_meta.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,46 @@ modify that manually.
## Defining Meta Fields

All fields are defined within `app/Config/Users.php`. Two fields are provided (though commented out) as examples.
Additionally, if you need labels for fields in validation messages, or if you need custom validation messages,
expand the $metaFields 'validation' value into multidimensional array of it's own, with keys
`label` and `rules` like it is done with 'baz' value below (in case of custom error messages, you can add them
in `errors` subarray, see
[Validation in Codeigniter 4 documentation](https://codeigniter4.github.io/userguide/libraries/validation.html)
for more details).

```php
public $metaFields = [
'Example Fields' => [
'foo' => ['label' => 'Foo', 'type' => 'text', 'required' => true, 'validation' => 'permit_empty|string'],
'Bar' => ['type' => 'text', 'required' => true, 'validation' => 'required|string'],
'foo' => ['label' => 'Foo', 'type' => 'text', 'validation' => 'permit_empty|string'],
'Bar' => ['type' => 'text', 'validation' => 'required|string'],
'baz' => [
'label' => 'Baz',
'type' => 'checkbox',
'validation' => [
'label' => 'Baz',
'rules' => 'required|in_list[true,false]'
],
],
],
];
```



Each major grouping within the `$metaFields` array specifies a fieldset legend name. This provides logical grouping
of fields within the Edit User form.

Each entry within this fieldset is a single piece of information. The key is the name that you would refer to the
field by later. Then each has an array of information that defines it. The array has the following options that
you can specify:

- **label** is the label shown for the HTML input.
- **label** is the label shown for the HTML input (if **label** is within validation sub-array, it is displayed in validation messages).

- **type** is the type of HTML input. Currently most of the text-type inputs are supported (text, password, email, date, number, etc)
as well as checkboxes, and textareas for when you need more information.

- **required** is a boolean about whether that field is required when editing a user.

- **validation** is the set of validation rules that should be applied to this field when saving.
- **validation** is the set of validation rules that should be applied to this field when saving, or a container for validation array of
a field (with optional `label`, `rules`, `errors` keys).

## Using Meta Info

Expand Down
20 changes: 15 additions & 5 deletions src/Users/Config/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ class Users extends BaseConfig

/**
* --------------------------------------------------------------------------
* Additional User Fields
* User Fields
* --------------------------------------------------------------------------
* Validation rules used when saving a user.
*/
public $validation = [
'id' => [
// Needed for the id in email test;
// Needed for the id in email and username validation (below);
// see https://codeigniter4.github.io/userguide/installation/upgrade_435.html
'rules' => 'permit_empty|is_natural_no_zero',
],
Expand Down Expand Up @@ -117,13 +117,23 @@ class Users extends BaseConfig
* - type: the type of HTML input used. Should be the simpler inputs,
* like text, number, email, url, date, etc., as well as textarea, checkbox.
* Selects, radios, etc are not supported.
* - required: true/false
* - validation: a validation rules string. If not present will be 'permit_empty|string'
* NOTE: if you need labels for fields in validation messages, expand the 'validation'
* value into multidimensional array of it's own, with keys 'label' and 'rules',
* like it is done with 'baz' value below (adding custom error messages is also possible here).
*/
public $metaFields = [
// 'Example Fields' => [
// 'foo' => ['label' => 'Foo', 'type' => 'text', 'required' => true, 'validation' => 'permit_empty|string'],
// 'Bar' => ['type' => 'text', 'required' => true, 'validation' => 'required|string'],
// 'foo' => ['label' => 'Foo', 'type' => 'text', 'validation' => 'permit_empty|string'],
// 'Bar' => ['type' => 'text', 'validation' => 'required|string'],
// 'baz' => [
// 'label' => 'Baz',
// 'type' => 'checkbox',
// 'validation' => [
// 'label' => 'Baz',
// 'rules' => 'permit_empty|in_list[true,false]'
// ],
// ],
// ],
];
}

0 comments on commit eb46bff

Please sign in to comment.