From a188d2439b6cde2fb6e9c8d382d99bca8705759a Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Thu, 26 Nov 2009 23:48:50 +0000 Subject: [PATCH] MINOR: reverted r93693 due to the field being required for the CMS git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@93701 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/RestrictedTextField.php | 30 ++++++++ forms/UniqueRestrictedTextField.php | 27 +++++++ javascript/UniqueFields.js | 111 ++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100755 forms/RestrictedTextField.php create mode 100755 forms/UniqueRestrictedTextField.php diff --git a/forms/RestrictedTextField.php b/forms/RestrictedTextField.php new file mode 100755 index 000000000..8516cb930 --- /dev/null +++ b/forms/RestrictedTextField.php @@ -0,0 +1,30 @@ +restrictedChars = $restrictedChars; + parent::__construct($name, $title, $value); + } + + function Field() { + Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang'); + Requirements::javascript( SAPPHIRE_DIR . '/javascript/UniqueFields.js' ); + + if($this->maxLength){ + $field = "id() . "\" name=\"{$this->name}\" value=\"" . $this->attrValue() . "\" maxlength=\"$this->maxLength\" />"; + }else{ + $field = "id() . "\" name=\"{$this->name}\" value=\"" . $this->attrValue() . "\" />"; + } + + return $field."id()."]\" id=\"".$this->id()."-restricted-chars\" value=\"".$this->restrictedChars."\" />"; + } +} +?> \ No newline at end of file diff --git a/forms/UniqueRestrictedTextField.php b/forms/UniqueRestrictedTextField.php new file mode 100755 index 000000000..e294ba5fc --- /dev/null +++ b/forms/UniqueRestrictedTextField.php @@ -0,0 +1,27 @@ +charRegex = $charRegex; + $this->charReplacement = $charReplacement; + $this->charMessage = $charMessage; + + parent::__construct($name, $restrictedField, $restrictedTable, $restrictedMessage, $title, $value, $maxLength); + } + + function Field() { + return parent::Field()."id()."]\" id=\"".$this->id()."-restricted-chars\" value=\"".$this->charRegex."\" />id()."]\" id=\"".$this->id()."-restricted-chars-replace\" value=\"".$this->charReplacement."\" />id()."]\" id=\"".$this->id()."-restricted-chars-message\" value=\"".$this->charMessage."\" />"; + } +} +?> \ No newline at end of file diff --git a/javascript/UniqueFields.js b/javascript/UniqueFields.js index d097c6f09..ce2f6c231 100755 --- a/javascript/UniqueFields.js +++ b/javascript/UniqueFields.js @@ -36,3 +36,114 @@ UniqueFormField.prototype = { return suggested; } } + +UniqueRestrictedTextField = Class.extend('UniqueFormField'); +UniqueRestrictedTextField.applyTo('input.UniqueRestrictedTextField'); +UniqueRestrictedTextField.prototype = { + initialize: function() { + this.loadMessages(); + if(this.loadRestrictedValues){ this.loadRestrictedValues()}; + this.loadRestrictedChars(); + this.onblur = this.validate.bind(this); + }, + loadRestrictedChars: function() { + this.charRegex = new RegExp( $(this.id + '-restricted-chars').value, 'g' ); + this.charReplacement = $(this.id + '-restricted-chars-replace').value; + this.trimRegex = new RegExp( '^[' + this.charReplacement + ']+|[' + this.charReplacement + ']+$', 'g' ); + }, + suggestNewValue: function( fromString ) { + var prefix = ''; + var count = 1; + var suggested = fromString || this.value; + + if( suggested.length == 0 ) { + suggested = $('Form_EditForm_Title').value.toLowerCase(); + } + + var escaped = suggested.replace(this.charRegex, this.charReplacement); + escaped = escaped.replace( this.trimRegex, '' ); + + // this.loadRestrictedValues is never called, i think someone missed a function.. + if(this.restrictedValues){ + if( !this.restrictedValues[escaped] ) + return escaped; + } + + var prefix = escaped; + + if( prefix.charAt(prefix.length-1) != this.charReplacement ) + prefix = prefix + this.charReplacement; + + suggested = prefix; + suggested = suggested.replace( this.charRegex, this.charReplacement ); + suggested = suggested.replace( this.trimRegex, '' ); + + if(this.restrictedValues){ + while( this.restrictedValues[suggested] ) { + suggested = prefix + (++count); + suggested = suggested.replace( this.charRegex, this.charReplacement ); + suggested = suggested.replace( this.trimRegex, '' ); + } + } + + + return suggested; + }, + validate: function() { + // check that the value is not in use, and matches the pattern + var suggested = this.value; + if(this.restrictedValues){ + var suggestedValue = this.restrictedValues[suggested]; + } + + if( suggested == null || suggested.length == 0 || suggestedValue || suggested.match( this.charRegex ) ) { + var message; + if( suggested == null ) + message = ss.i18n._t('UNIQUEFIELD.ENTERNEWVALUE', 'You will need to enter a new value for this field'); + else if( suggested.length == 0 ) + message = ss.i18n._t('UNIQUEFIELD.CANNOTLEAVEEMPTY', 'This field cannot be left empty'); + else if( suggestedValue ) + message = this.restrictedMessage; + else + message = this.charMessage; + + suggested = this.suggestNewValue(); + statusMessage(ss.i18n.sprintf( + ss.i18n._t('UNIQUEFIELD.SUGGESTED', "Changed value to '%s' : %s"), + suggested, + message + )); + } + + this.value = suggested; + }, + loadMessages: function() { + this.restrictedMessage = $(this.id + '-restricted-message').value; + this.charMessage = $(this.id + '-restricted-chars-message').value; + } +} + +RestrictedTextField = Class.create(); +RestrictedTextField.applyTo('input.text.restricted'); +RestrictedTextField.prototype = { + + initialize: function() { + this.restrictedChars = $(this.id + '-restricted-chars').value; + // this.restrictedRegex = new RegExp( $(this.id.'-restricted-chars').value, 'g' ); + }, + + onkeyup: function() { + + var lastChar = this.value.charAt(this.value.length - 1); + + for( var index = 0; index < this.restrictedChars.length; index++ ) { + if( lastChar == this.restrictedChars.charAt(index) ) { + alert(ss.i18n.sprintf( + ss.i18n._t('RESTRICTEDTEXTFIELD.CHARCANTBEUSED', "The character '%s' cannot be used in this field"), + lastChar + )); + this.value = this.value.substring( 0, this.value.length - 1 ); + } + } + } +}