error when adding a new record with duplicate value on unique field
error when adding a new record with duplicate value on unique field
ourteamweb
Posts: 3Questions: 1Answers: 0
in Editor
When using the New button to add a new record, I get the following error when the value already exists:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'test@test.com' for key 'Username'
I would prefer if a check was performed first with a message such as "This username already exists, please try another".
Any idea where I can add that?
Thanks
Jim
This discussion has been closed.
Replies
Hi Jim,
An ugly error to show users that for certain! Are you using the Editor PHP libraries? If so, there is a built in
unique
validator which you can use. It is described on the PHP validation page (bottom of the list). The way it works is something like:You can use the parameters described in the manual if you wish to specify a custom error message (default is
This field must have a unique value
), db, table and or column.Regards,
Allan
Hi Allan,
I tried what you wrote (with a field : email)
Field::inst( 'email' )
->validator( 'Validate::unique' )
without success.
Is it still valid?
In addition, how do you specify same time that the the field is required?
Like that ?
Field::inst( 'email' )->validator( 'Validate::email_required' ),
Field::inst( 'email' )->validator( 'Validate:: unique' ),
Thx
Yes that should work for the current release of Editor. In what way did it not work? No validation or error message?
Using the validate options. So you might have:
Allan
In browsers.php, when I set :
Editor::inst( $db, 'wp_qspread_invite' ) // Nom de la BdD et celui de la table
->fields(
I get the message "Error" (nothing else)
and when I put what you proposed , data can't be parsed
What "Error" message? Is there an error reported back from the server in the JSON, or is it not valid JSON? Or is there something else that is in an error state?
Sorry - I used javascript object notation. I'll fix it now.
Allan
Hi Allan,
1 -
it seems that browser.php returns :
{"id":-1,"fieldErrors":[{"name":"email","status":null}],"sError":"","aaData":[]}
2 -
can you give me the right notation (previous question)
Thx
Odd - I don't way understand why an error message wouldn't be given. Try this:
Yes - I corrected my post above. Rather than
"required": true
it should have been"required" => true
(as it is now).Allan
Regarding Validate::unique, I understand now why it does not work.
It's not implemented in my version.
You added "unique" to Editor 1.3.0 (in May) and my version is from April.
I tried to replace Validate.php (old version) by the new one but it does not work.
Does it mean that I need to move to latest version?
I did some modifications in the SW...
Thx
Ah! That would do it :-).
Yes, there were a number of changes in the PHP libraries in order to support what was needed for the
unique
method so if you wanted to use mine you would need to update. Alternatively you could implement it using a closure function with a little bit of custom SQL, although that isn't so good for long term maintainability.Allan
Thx. I'll move to the new version.
I moved to the new version. Now I can use "unique".
I am wondering, how to manage this problem.
Lets say, I have 2 fields: fieldLetter and fieldNumber
I refuse to have twice the save value in fieldLetter+fieldNumber (conjunction)
e.g. I accept (row1: a,1 -row2: a,2)
but I refuse (row1: a,1 -row2: a,1)
How to write it?
Thx
You would need to provide your own validation method - as an anonymous function that would perform the validation required, querying the database if needed. The Editor PHP manual on validation describes how validation methods can be written.
Allan