#1 2015-11-30 10:19:37

dolfin
Member
Registered: 2015-09-11
Posts: 22

Unique validation on ajax request

Hello!
I  have a simple TSQLRecord class with one unique field:

type
  TCategory = class(TSQLRecord)
  private
    FName: RawUTF8;
  published
    property Name: RawUTF8 index 64 read FName write FName stored AS_UNIQUE;
  end;

If ajax post request is performed with "Name" value wich is already exists, the server respones with such answer:

{
  "errorCode":400,
  "errorText":"Bad Request"
}

And there is no detail info on what was wrong. I tried to use validators to get more relevant answer(for example "errorText":"Name field must be unique") but with no luck. As I understood to use a validator I have to define my own server method and explicitly call Rec.Validate method. Am I right? And what is the right way to perform filter and validation while working with ajax client?

Many thanks in advance!

Offline

#2 2015-11-30 10:27:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Unique validation on ajax request

Which HTTP error do you expect in such case?

Online

#3 2015-11-30 12:58:19

esmondb
Member
From: London
Registered: 2010-07-20
Posts: 299

Re: Unique validation on ajax request

I had a similar problem with a service and chose 409 as the http error code. I'm a bit confused with how to handle mORMot errors with ajax. Are errors within mORMot only returned with an ok 200 response with the error encoded as JSON within the response or does it use http error codes?

Offline

#4 2015-11-30 15:54:38

dolfin
Member
Registered: 2015-09-11
Posts: 22

Re: Unique validation on ajax request

Well, I think the best idea is to have a possibility to add a validator on server side, wich will process the requests and responses with custom json or at least with custom error message like "errorText":"'Name' field must be unique".   
As I know usually "422 Unprocessable Entity" or "409 Conflict" errors are used in such case.

Offline

#5 2015-11-30 17:57:56

danielkuettner
Member
From: Germany
Registered: 2014-08-06
Posts: 357

Re: Unique validation on ajax request

The solution is very easy: drop the unique index, and the ajax request has no error anymore.

Offline

Board footer

Powered by FluxBB