mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
API CHANGE Removed deprecated RestrictedTextField, UniqueTextField and UniqueRestrictedTextField
This commit is contained in:
parent
53f43f5176
commit
e0e1f81186
@ -1,30 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* A Text field that cannot contain certain characters
|
||||
* @deprecated 2.5
|
||||
* @package forms
|
||||
* @subpackage fields-formatted
|
||||
*/
|
||||
class RestrictedTextField extends TextField {
|
||||
|
||||
protected $restrictedChars;
|
||||
|
||||
function __construct($name, $title = null, $value = "", $restrictedChars = "", $maxLength = null){
|
||||
$this->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 = "<input class=\"text restricted\" type=\"text\" id=\"" . $this->id() . "\" name=\"{$this->name}\" value=\"" . $this->attrValue() . "\" maxlength=\"$this->maxLength\" />";
|
||||
}else{
|
||||
$field = "<input class=\"text restricted\" type=\"text\" id=\"" . $this->id() . "\" name=\"{$this->name}\" value=\"" . $this->attrValue() . "\" />";
|
||||
}
|
||||
|
||||
return $field."<input type=\"hidden\" name=\"restricted-chars[".$this->id()."]\" id=\"".$this->id()."-restricted-chars\" value=\"".$this->restrictedChars."\" />";
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,27 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Text field that automatically checks that the value entered is unique for the given
|
||||
* set of fields in a given set of tables
|
||||
* @deprecated 2.5
|
||||
* @package forms
|
||||
* @subpackage fields-formattedinput
|
||||
*/
|
||||
class UniqueRestrictedTextField extends UniqueTextField {
|
||||
|
||||
protected $charRegex;
|
||||
protected $charReplacement;
|
||||
protected $charMessage;
|
||||
|
||||
function __construct($name, $restrictedField, $restrictedTable, $restrictedMessage, $charRegex, $charReplacement, $charMessage, $title = null, $value = "", $maxLength = null ){
|
||||
$this->charRegex = $charRegex;
|
||||
$this->charReplacement = $charReplacement;
|
||||
$this->charMessage = $charMessage;
|
||||
|
||||
parent::__construct($name, $restrictedField, $restrictedTable, $restrictedMessage, $title, $value, $maxLength);
|
||||
}
|
||||
|
||||
function Field() {
|
||||
return parent::Field()."<input type=\"hidden\" name=\"restricted-chars[".$this->id()."]\" id=\"".$this->id()."-restricted-chars\" value=\"".$this->charRegex."\" /><input type=\"hidden\" name=\"restricted-chars[".$this->id()."]\" id=\"".$this->id()."-restricted-chars-replace\" value=\"".$this->charReplacement."\" /><input type=\"hidden\" name=\"restricted-chars[".$this->id()."]\" id=\"".$this->id()."-restricted-chars-message\" value=\"".$this->charMessage."\" />";
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,67 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Text field that automatically checks that the value entered is unique for the given
|
||||
* set of fields in a given set of tables
|
||||
* @deprecated 2.3
|
||||
* @package forms
|
||||
* @subpackage fields-formattedinput
|
||||
*/
|
||||
class UniqueTextField extends TextField {
|
||||
|
||||
protected $restrictedField;
|
||||
protected $restrictedTable;
|
||||
protected $restrictedMessage;
|
||||
|
||||
function __construct($name, $restrictedField, $restrictedTable, $restrictedMessage, $title = null, $value = "", $maxLength = null ){
|
||||
$this->maxLength = $maxLength;
|
||||
|
||||
$this->restrictedField = $restrictedField;
|
||||
|
||||
$this->restrictedTable = $restrictedTable;
|
||||
$this->restrictedMessage = $restrictedMessage;
|
||||
|
||||
parent::__construct($name, $title, $value);
|
||||
}
|
||||
|
||||
function Field() {
|
||||
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
|
||||
Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/jquery/jquery.js");
|
||||
Requirements::javascript(SAPPHIRE_DIR . "/javascript/UniqueFields.js");
|
||||
|
||||
/*
|
||||
$restrictedValues = array();
|
||||
$restrictedInputs = array();
|
||||
|
||||
// if the restrictedFields and tables have been specified,
|
||||
// then get the restricted values
|
||||
if( !empty( $this->restrictedField ) && !empty( $this->restrictedTable ) ) {
|
||||
$result = DB::query("SELECT \"{$this->restrictedField}\" FROM \"{$this->restrictedTable}\"");
|
||||
|
||||
$count = 1;
|
||||
|
||||
while( $restrictedValue = $result->nextRecord() )
|
||||
$restrictedValues[$restrictedValue[$this->restrictedField]] = 1;
|
||||
|
||||
$result = DB::query("SELECT \"{$this->restrictedField}\" FROM \"{$this->restrictedTable}_Live\"");
|
||||
|
||||
while( $restrictedValue = $result->nextRecord() )
|
||||
$restrictedValues[$restrictedValue[$this->restrictedField]] = 1;
|
||||
|
||||
// remove the initial value of this field
|
||||
$restrictedValues = array_diff_assoc( $restrictedValues, array( $this->attrValue() => 1 ) );
|
||||
|
||||
foreach( $restrictedValues as $restrictedValue => $discard )
|
||||
$restrictedInputs[] = "<input type=\"hidden\" id=\"".$this->id()."-restricted-".($count++)."\" value=\"$restrictedValue\" name=\"restricted-values[".$this->id()."]\" />";
|
||||
}
|
||||
*/
|
||||
|
||||
$fieldSize = $this->maxLength ? min( $this->maxLength, 30 ) : 30;
|
||||
|
||||
if($this->maxLength){
|
||||
return /*implode("", $restrictedInputs).*/"<input class=\"".$this->class."\" type=\"text\" id=\"" . $this->id() . "\" name=\"{$this->name}\" value=\"" . $this->attrValue() . "\" maxlength=\"$this->maxLength\" size=\"$fieldSize\" /><input type=\"hidden\" name=\"restricted-messages[".$this->id()."]\" id=\"".$this->id()."-restricted-message\" value=\"{$this->restrictedMessage}\" />";
|
||||
}else{
|
||||
return /*implode("", $restrictedInputs).*/"<input class=\"".$this->class."\" type=\"text\" id=\"" . $this->id() . "\" name=\"{$this->name}\" value=\"" . $this->attrValue() . "\" size=\"30\" /><input type=\"hidden\" name=\"restricted-messages[".$this->id()."]\" id=\"".$this->id()."-restricted-message\" value=\"{$this->restrictedMessage}\" />";
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,152 +0,0 @@
|
||||
UniqueFormField = Class.create();
|
||||
UniqueFormField.prototype = {
|
||||
validate: function() {
|
||||
// check that the value is not in use, and matches the pattern
|
||||
var suggested = this.value;
|
||||
|
||||
if( this.restrictedValues[suggested] || suggested == null ) {
|
||||
suggested = this.suggestNewValue();
|
||||
var message = i18n.sprintf(
|
||||
ss.i18n._t('UNIQUEFIELD.SUGGESTED', "Changed value to '%s' : %s"),
|
||||
suggested,
|
||||
this.restrictedMessage
|
||||
);
|
||||
jQuery(this).trigger('validate', {message: message, suggested: suggested});
|
||||
this.value = suggested;
|
||||
}
|
||||
},
|
||||
suggestNewValue: function() {
|
||||
var parts = this.value.match( /(.*)(\d+)$/ );
|
||||
var prefix = '';
|
||||
var count = 1;
|
||||
|
||||
|
||||
if( parts )
|
||||
prefix = parts[1];
|
||||
else
|
||||
prefix = this.value;
|
||||
|
||||
if( prefix.charAt(prefix.length-1) != ' ' )
|
||||
prefix = prefix + ' ';
|
||||
|
||||
var suggested = prefix + count;
|
||||
|
||||
while( this.restrictedValues[suggested] )
|
||||
suggested = prefix + (++count);
|
||||
|
||||
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();
|
||||
var message = ss.i18n.sprintf(
|
||||
ss.i18n._t('UNIQUEFIELD.SUGGESTED', "Changed value to '%s' : %s"),
|
||||
suggested,
|
||||
message
|
||||
);
|
||||
|
||||
jQuery(this).trigger('validate', {message: message, suggested: suggested});
|
||||
}
|
||||
|
||||
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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user