diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d73ca72 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Match Input Changelog + +## [1.0.0] - 2017.02.19 +### Added +- Initial release diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..1b29fae --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2017 Marion Newlevant + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a5079e5 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# Match Input plugin for Craft CMS 3.x + +Craft field type for text fields that match a regex pattern. + +## Installation + +To install Match Input, follow these steps: + +1. Download and unzip the file and place the `matchinput` directory into your `craft/plugins` directory +3. -OR- install with Composer via `composer require marionnewlevant/match-input` +4. Install plugin in the Craft Control Panel under Settings > Plugins + +Match Input works on Craft 3.x. + +## Match Input Overview + +A _Match Input_ field is +a _Plain Text_ field with the addition of a regex pattern that the field must match to +be valid. + +## Using Match Input + +When you create the field, you specify the `Input Mask`. +This is the [PCRE Pattern](http://php.net/manual/en/pcre.pattern.php) which the +input is required to match. +You also specify an `Error Message` to display when the field does not match the +pattern. + +If you need to translate the `Error Message` (for a multi-language control panel), those translations +will be in the `site` translation category. + +## Sample Input Masks +- `https://` - not a valid pattern (no delimiters) +- `/https:\/\//` - valid pattern, will match string with `https://` in it anywhere +- `#https://#` - valid pattern, will match string with `https://` in it anywhere (sometimes / isn't the best delimiter) +- `#^https://#` - will match string that begins with `https://` +- `/^\d{5}(-\d{4})?$/` - will match 5 digits, optionally followed by `-` and 4 digits (uses ^ and $ to match the entire string) + +## Acknowledgements +Brought to you by [Marion Newlevant](http://marion.newlevant.com). +Icon interior by SlideGenius from the Noun Project diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7bdf996 --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "marionnewlevant/match-input", + "description": "Craft field type for text fields that match a regex pattern", + "version": "1.0.0", + "type": "craft-plugin", + "keywords": [ + "craft", "craftcms", "craft-plugin", "matchinput" + ], + "license": "MIT", + "authors": [ + { + "name": "Marion Newlevant", + "homepage": "http://marion.newlevant.com" + } + ], + "require": { + }, + "autoload": { + "psr-4": { + "marionnewlevant\\matchinput\\": "src/" + } + }, + "extra": { + "name": "Match Input", + "handle": "matchInput", + "version": "1.0.0", + "schemaVersion": "1.0.0", + "hasSettings": false, + "hasCpSection": false, + "documentationUrl": "https://github.com/marionnewlevant/craft3-match_input/blob/master/README.md", + "changelogUrl": "https://raw.githubusercontent.com/marionnewlevant/craft3-match_input/master/CHANGELOG.md" + } +} diff --git a/src/Icon.svg b/src/Icon.svg new file mode 100755 index 0000000..ef27a8d --- /dev/null +++ b/src/Icon.svg @@ -0,0 +1,20 @@ + + \ No newline at end of file diff --git a/src/Plugin.php b/src/Plugin.php new file mode 100644 index 0000000..fb863be --- /dev/null +++ b/src/Plugin.php @@ -0,0 +1,54 @@ +types[] = MatchInputField::class; + } + ); + } + +} diff --git a/src/fields/MatchInputField.php b/src/fields/MatchInputField.php new file mode 100644 index 0000000..0598459 --- /dev/null +++ b/src/fields/MatchInputField.php @@ -0,0 +1,171 @@ +$object; + if (!self::validateRegex($inputMask)) + { + $this->addError($object, Craft::t('matchInput', 'Not a valid regex (missing delimiters?)')); + } + } + + + /** + * Returns the component’s settings HTML. + * + * @return string|null + */ + public function getSettingsHtml() + { + return Craft::$app->getView()->renderTemplate( + 'matchinput' + . DIRECTORY_SEPARATOR + . '_components' + . DIRECTORY_SEPARATOR + . 'fields' + . DIRECTORY_SEPARATOR + . '_settings', + [ + 'field' => $this + ] + ); + } + + /** + * Returns the field’s input HTML. + * + * @param mixed $value The field’s value. This will either be the [[normalizeValue() normalized value]], + * raw POST data (i.e. if there was a validation error), or null + * @param ElementInterface|null $element The element the field is associated with, if there is one + * + * @return string The input HTML. + */ + public function getInputHtml($value, ElementInterface $element = null): string + { + + return Craft::$app->getView()->renderTemplate( + 'matchinput'. DIRECTORY_SEPARATOR . '_components'. DIRECTORY_SEPARATOR . 'fields'. DIRECTORY_SEPARATOR . '_input', + [ + 'name' => $this->handle, + 'value' => $value, + 'field' => $this, + ] + ); + } + + /** + * @inheritdoc + */ + public function getElementValidationRules(): array + { + $rules = parent::getElementValidationRules(); + // add our rule + $rules[] = 'validateMatchesRegex'; + return $rules; + } + + /** + * Validates the field value. + * + * @param ElementInterface $element + * @param array|null $params + * + * @return void + */ + public function validateMatchesRegex(ElementInterface $element, array $params = null) + { + $value = $element->getFieldValue($this->handle); + $match = preg_match($this->inputMask, $value); + if ($match !== 1) + { + $element->addError($this->handle, Craft::t('site', $this->errorMessage)); + } + } +} diff --git a/src/templates/_components/fields/_input.twig b/src/templates/_components/fields/_input.twig new file mode 100644 index 0000000..9ff7a4c --- /dev/null +++ b/src/templates/_components/fields/_input.twig @@ -0,0 +1,33 @@ +{# +/** + * Match Input plugin for Craft CMS 3.x + * + * Field Input + * + * @author Marion Newlevant + * @copyright Copyright (c) 2017 Marion Newlevant + * @link http://marion.newlevant.com + * @package MatchInput + * @since 1.0.0 + */ +#} + +{% import "_includes/forms" as forms %} + +{% set config = { + id: name, + name: name, + value: value, + class: 'nicetext', + maxlength: field.maxLength, + showCharsLeft: true, + placeholder: field.placeholder, + rows: field.initialRows +} %} + +{% if field.multiline %} + {{ forms.textarea(config) }} +{% else %} + {{ forms.text(config) }} +{% endif %} + diff --git a/src/templates/_components/fields/_settings.twig b/src/templates/_components/fields/_settings.twig new file mode 100644 index 0000000..cabff66 --- /dev/null +++ b/src/templates/_components/fields/_settings.twig @@ -0,0 +1,76 @@ +{# +/** + * Match Input plugin for Craft CMS + * + * Field Settings + * + * @author Marion Newlevant + * @copyright Copyright (c) 2017 Marion Newlevant + * @link http://marion.newlevant.com + * @package MatchInput + * @since 1.0.0 + */ +#} + +{% import "_includes/forms" as forms %} + +{{ forms.textField({ + label: "Input Mask"|t('matchInput'), + instructions: "regex pattern that the input must match. See [php manual](http://php.net/manual/en/reference.pcre.pattern.syntax.php) for syntax. Example: '/^\\d{5}(-\\d{4})?$/' to match a zip-code"|t('matchInput'), + id: 'inputMask', + name: 'inputMask', + value: field.inputMask, + translatable: true, + errors: field.getErrors('inputMask') +}) }} + +{{ forms.textField({ + label: "Error Message"|t('matchInput'), + instructions: "Error message when pattern does not match field"|t('matchInput'), + id: 'errorMessage', + name: 'errorMessage', + value: field.errorMessage, + translatable: true, + errors: field.getErrors('errorMessage') +}) }} + +{# below here straight from _components/fieldtypes/PlainText/settings.html #} + +{{ forms.textField({ + label: "Placeholder Text"|t('app'), + instructions: "The text that will be shown if the field doesn’t have a value."|t('app'), + id: 'placeholder', + name: 'placeholder', + value: field.placeholder, + translatable: true, + errors: field.getErrors('placeholder') +}) }} + +{{ forms.textField({ + label: "Max Length"|t('app'), + instructions: "The maximum length of characters the field is allowed to have."|t('app'), + id: 'maxLength', + name: 'maxLength', + value: field.maxLength, + size: 3, + errors: field.getErrors('maxLength') +}) }} + +{{ forms.checkboxField({ + label: "Allow line breaks"|t('app'), + name: 'multiline', + checked: field.multiline, + toggle: 'initialRowsContainer' +}) }} + + +
diff --git a/src/translations/en/matchInput.php b/src/translations/en/matchInput.php new file mode 100644 index 0000000..84acb48 --- /dev/null +++ b/src/translations/en/matchInput.php @@ -0,0 +1,10 @@ + 'MatchInput', + 'Match Input' => 'Match Input', + 'Not a valid regex (missing delimiters?)' => 'Not a valid regex (missing delimiters?)', + 'Input Mask' => 'Input Mask', + "regex pattern that the input must match. See [php manual](http://php.net/manual/en/reference.pcre.pattern.syntax.php) for syntax. Example: '/^\\d{5}(-\\d{4})?$/' to match a zip-code" => "regex pattern that the input must match. See [php manual](http://php.net/manual/en/reference.pcre.pattern.syntax.php) for syntax. Example: '/^\\d{5}(-\\d{4})?$/' to match a zip-code", + 'Error Message' => 'Error Message', + 'Error message when pattern does not match field' => 'Error message when pattern does not match field', +]; \ No newline at end of file