-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fd25d78
Showing
10 changed files
with
452 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Match Input Changelog | ||
|
||
## [1.0.0] - 2017.02.19 | ||
### Added | ||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* Match Input plugin for Craft CMS 3.x | ||
* | ||
* Craft field type for text fields that match a regex pattern | ||
* | ||
* @link http://marion.newlevant.com | ||
* @copyright Copyright (c) 2017 Marion Newlevant | ||
*/ | ||
|
||
namespace marionnewlevant\matchinput; | ||
|
||
|
||
use Craft; | ||
use craft\services\Fields; | ||
use craft\events\RegisterComponentTypesEvent; | ||
use yii\base\Event; | ||
use marionnewlevant\matchinput\fields\MatchInputField; | ||
|
||
/** | ||
* | ||
* @author Marion Newlevant | ||
* @package MatchInput | ||
* @since 1.0.0 | ||
*/ | ||
class Plugin extends \craft\base\Plugin | ||
{ | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* Called after the plugin class is instantiated; do any one-time initialization | ||
* here such as hooks and events. | ||
* | ||
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically; | ||
* you do not need to load it in your init() method. | ||
* | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
// Register our fields | ||
Event::on( | ||
Fields::className(), | ||
Fields::EVENT_REGISTER_FIELD_TYPES, | ||
function (RegisterComponentTypesEvent $event) { | ||
$event->types[] = MatchInputField::class; | ||
} | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<?php | ||
/** | ||
* Match Input plugin for Craft CMS 3.x | ||
* | ||
* Craft field type for text fields that match a regex pattern | ||
* | ||
* @link http://marion.newlevant.com | ||
* @copyright Copyright (c) 2017 Marion Newlevant | ||
*/ | ||
|
||
namespace marionnewlevant\matchinput\fields; | ||
|
||
use Craft; | ||
use craft\base\ElementInterface; | ||
use craft\fields\PlainText; | ||
|
||
/** | ||
* Field | ||
* | ||
* Whenever someone creates a new field in Craft, they must specify what | ||
* type of field it is. The system comes with a handful of field types baked in, | ||
* and we’ve made it extremely easy for plugins to add new ones. | ||
* | ||
* https://craftcms.com/docs/plugins/field-types | ||
* | ||
* @author Marion Newlevant | ||
* @package MatchInput | ||
* @since 1.0.0 | ||
*/ | ||
class MatchInputField extends PlainText | ||
{ | ||
public static function validateRegex($regex) | ||
{ | ||
set_error_handler(function() { return true; }, E_NOTICE); | ||
// preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred. | ||
$valid = (preg_match($regex, '') !== false); | ||
restore_error_handler(); | ||
|
||
return $valid; | ||
} | ||
|
||
// Public Properties | ||
// ========================================================================= | ||
|
||
/** | ||
* @var string The input’s inputMask text | ||
*/ | ||
public $inputMask; | ||
|
||
/** | ||
* @var string The input’s errorMessage text | ||
*/ | ||
public $errorMessage; | ||
|
||
// Static Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* Returns the display name of this class. | ||
* | ||
* @return string The display name of this class. | ||
*/ | ||
public static function displayName(): string | ||
{ | ||
return Craft::t('matchInput', 'Match Input'); | ||
} | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* Returns the validation rules for attributes. | ||
* | ||
* Validation rules are used by [[validate()]] to check if attribute values are valid. | ||
* Child classes may override this method to declare different validation rules. | ||
* | ||
* More info: http://www.yiiframework.com/doc-2.0/guide-input-validation.html | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
$rules = parent::rules(); | ||
$rules[] = [['inputMask', 'errorMessage'], 'string']; | ||
$rules[] = ['inputMask', 'required']; | ||
$rules[] = ['inputMask', 'isValidRegex']; | ||
return $rules; | ||
} | ||
|
||
public function isValidRegex($object, $attribute) | ||
{ | ||
$inputMask = $this->$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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %} | ||
|
Oops, something went wrong.