raffaelj/cockpit-formvalidation

A form validator and form builder for Cockpit CMS v1

0.4.1 2022-12-16 15:30 UTC

This package is auto-updated.

Last update: 2024-12-16 20:35:52 UTC


README

This addon is not compatible with Cockpit CMS v2.

See also Cockpit CMS v1 docs, Cockpit CMS v1 repo and Cockpit CMS v2 docs, Cockpit CMS v2 repo.

Form validator and form builder for Cockpit CMS v1. I mainly use it in combination with CpMultiplane, but it should be compatible with plain rest api usage.

Work in progress! Feel free to contribute with code, bug reports or feature requests.

Installation

Copy this repository into /addons and name it FormValidation or use the cli.

via git

cd path/to/cockpit
git clone https://github.com/raffaelj/cockpit_FormValidation.git addons/FormValidation

via cp cli

cd path/to/cockpit
./cp install/addon --name FormValidation --url https://github.com/raffaelj/cockpit_FormValidation/archive/master.zip

via composer

Make sure, that the path to cockpit addons is defined in your projects' composer.json file.

{
    "name": "my/cockpit-project",
    "extra": {
        "installer-paths": {
            "addons/{$name}": ["type:cockpit-module"]
        }
    }
}
cd path/to/cockpit-root
composer create-project --ignore-platform-reqs aheinze/cockpit .
composer config extra.installer-paths.addons/{\$name} "type:cockpit-module"

composer require --ignore-platform-reqs raffaelj/cockpit-formvalidation

Requirements:

  • Cockpit version >= 0.12.2
  • PECL intl extension is suggested for punycode conversion of urls and mail adresses

Features

Form builder

Field types:

  • boolean (check box)
  • text
  • textarea
  • date
  • select
  • multipleselect
  • content block (rich text stored as field.content)
  • honeypot (with default attributes for a hidden input field)
  • file (doesn't work out of the box - there is an example for advanced usage in the CpMultiplane source until I'll update the docs for it here)

It's meant for strings and I don't know (yet), what happens if it should validate arrays.

In the frontend it's possible to reuse some form options like "info", "label", "group", "lst", "width".

...

Form Validator

  • checks for required fields
  • checks for content types
  • sends response if validation fails

The idea is to add multiple checks on each field to trick spambots without using a captcha.

some templating

  • custom mail subject
  • field for reply to

see screenshots below

How to use

Create a field and click on "Validate" tab. Click on "Validate form data" to activate the validator.

When the validator is active, it checks, if required fields are present. If you want to allow sending fields, that aren't present in the form builder, you have to click "Allow extra fields".

To activate more validations, click on "Validate" for each field and add some json.

mail field

Create a text field.

{
  "type": {
    "mail": true,
    "url": false
  }
}

Honeypot

Create a boolean field and name it "confirm". Spambots will love it :-D

{
  "honeypot": {
    "fieldname": "confirm",
    "expected_value": "0",
    "response": "Hello spambot. A human wouldn't fill this field."
  }
}

If "response": "404", sender gets a 404 Path not found instead of a json response.

...

defaults:

  • if form validation is active
    • key names must be alphanumeric (a-zA-Z0-9) or '-' or '_'
    • check, if required fields are present
    • sending data with unknown field names is not allowed
  • if field validation is active
    • no defaults, only specified validations

Notes

  • Validating to type:{"phone":false} could lead to false positives. The regex is meant to allow inputs like "0123 45678" or "+49 123-456-78", but "123" returns true, too.

i18n

Use the Babel addon

To do

  • allow mail addresses with special chars (punycode) - they are valid, but filter_var($to, FILTER_VALIDATE_EMAIL) returns false
    • --> overwrite original submit function again or
    • --> change the mail validation in cockpit core --> now in core
  • i18n of error responses
  • friendly error responses --> use i18n
  • add a view to include via PHP frontend

matches:

  • required (!empty)
  • honeypot (humans wouldn't fill this field)
  • type (mail, phone, url)
  • !type (inverse type)
  • equals (= string) (for simple captchas or something like "Are you really sure? Type 'Yes'")
  • contains
    • code
    • url(s)
    • string

types:

  • mail
  • phone
  • url
  • number
  • bool
  • ascii
  • date --> must be i18n specific
  • ...

Form Mail Template Example

Create a custom mail template in config/forms/emails/formname.php to use the settings email_text_before and email_text_after.

Example

Screenshots

formbuilder

validation_01

honeypot

responses

Credits and third party libraries

Icons are from Cockpit CMS v2, (c) Artur Heinze, https://agentejo.com, MIT License