Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

AjaxUniqueTextField is flagging a unique value when editing. #24

Closed
wants to merge 2 commits into from

3 participants

@Firesphere

Fix for the currennt (Unique) value being flagged as false. The current ID is at position 5, for calling the AjaxUniqueTextField(), at position 5 aff $this->ID, so it's properly escaped in the validator.
A problem mentioned a few times in IRC as wel.

Firesphere Fix for the currennt (Unique) value being flagged as false. The curre…
…nt ID is at position 5, for calling the AjaxUniqueTextField(), at position 5 aff $this->ID, so it's properly escaped in the validator.
041b563
@chillu
Owner

Sorry, I don't understand what you're trying to do here. Could you add some inline documentation to the PHP file please?
Also, we can't change the parameter signature of the __construct() method, this would be an API change that needs careful consideration and will confuse a lot of people. I'd suggest a setID() method instead.

@Firesphere

Valid point on the construct. I've been using this for a while now, but I could find a better method preferably indeed.

Simon Erkelens 4228c68
@Firesphere

I'll have a look on a better method later. Right now, I got a projectmanager breathing down my neck.

@wilr
Collaborator

Closing, doesn't merge cleanly and patch needs work. Looks like the field hasn't been updated to 3.0 yet but neither is it deprecated.

@wilr wilr closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 20, 2011
  1. Fix for the currennt (Unique) value being flagged as false. The curre…

    Firesphere authored
    …nt ID is at position 5, for calling the AjaxUniqueTextField(), at position 5 aff $this->ID, so it's properly escaped in the validator.
  2. No commit message

    Simon Erkelens authored
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 7 deletions.
  1. +14 −7 forms/AjaxUniqueTextField.php
View
21 forms/AjaxUniqueTextField.php
@@ -14,12 +14,16 @@ class AjaxUniqueTextField extends TextField {
protected $restrictedRegex;
- function __construct($name, $title, $restrictedField, $restrictedTable, $value = "", $maxLength = null, $validationURL = null, $restrictedRegex = null ){
+ function __construct($name, $title, $restrictedField, $restrictedTable, $id, $value = "", $maxLength = null, $validationURL = null, $restrictedRegex = null ){
$this->maxLength = $maxLength;
$this->restrictedField = $restrictedField;
$this->restrictedTable = $restrictedTable;
+
+
+ // We kinda need the ID of the page or DataObject, to exclude it in the validator function. Haven't found a better method yet.
+ $this->id = $id;
$this->validateURL = $validationURL;
@@ -107,16 +111,19 @@ function jsValidation() {
}
- function validate( $validator ) {
-
- $result = DB::query(sprintf(
- "SELECT COUNT(*) FROM \"%s\" WHERE \"%s\" = '%s'",
+function validate( $validator ) {
+ // Excluding the ID of the DataObject from the query, so the AjaxUnique is actually unique on this DataObject.
+ // Without excluding the ID itself, the count will always return 1 if the field itself hasn't changed. This means a "Not unique", even though it is.
+ // First thought was to make the result > 1, but that won't work sadly.
+ $result = DB::query(sprintf(
+ "SELECT COUNT(*) FROM \"%s\" WHERE \"%s\" = '%s' AND ID != '%d'",
$this->restrictedTable,
$this->restrictedField,
- Convert::raw2sql($this->value)
+ Convert::raw2sql($this->value),
+ $this->id
))->value();
- if( $result && ( $result > 0 ) ) {
+ if( $result && ( $result > 0 ) ) {
$validator->validationError( $this->name, _t('Form.VALIDATIONNOTUNIQUE', "The value entered is not unique") );
return false;
}
Something went wrong with that request. Please try again.