You are not logged in.
Pages: 1
how to validate a field that is empty in app Restfull
Offline
Offline
even putting this check is writing to the database, presenting no error.
function DataModel: TSQLModel;
begin
result := TSQLModel.Create([TPersona],SERVER_ROOT);
TPersona.AddFilterOrValidate('Name',TSynValidateText.Create(''));
end;
how can I use {"minlength": 5, "MaxLength": 10} in TSynValidateText, which syntax.
Last edited by ws (2015-03-25 00:00:41)
Offline
I found this code :
SQLRecordClasses[iClasses].AddFilterOrValidate(List^[iProps]^.Name,
TSynValidateText.Create(Format('{"MinLength":0,"MaxLength":%d}', [list^[iProps]^.Index])));
Offline
I tried this code, also did not work, was recorded in the database with two characters
function DataModel: TSQLModel;
begin
result := TSQLModel.Create([TPersona],SERVER_ROOT);
TPersona.AddFilterOrValidate('Name',TSynValidateText.Create('{"minlength": 5, "MaxLength": 10}'));
end;
Offline
returning to the tests.
I used the Validate method of TSQLRecord class, doing override, code below:
...
TPersona = class(TSQLRecord)
private
fName: RawUTF8;
fEndereco: RawUTF8;
public
function Validate(aRest: TSQLRest; const aFields: TSQLFieldBits=[0..MAX_SQLFIELDS-1];
aInvalidFieldIndex: PInteger=nil; aValidator: PSynValidate=nil): string; override;
published
...
function TPersona.Validate(aRest: TSQLRest; const aFields: TSQLFieldBits=[0..MAX_SQLFIELDS-1];
aInvalidFieldIndex: PInteger=nil; aValidator: PSynValidate=nil): string;
var texto :String;
begin
texto := '{"error":"only test"}';
result := UTF8ToString(texto);
end;
at what time the validate method is called, I created a new record and did not return the message.
how to call the validate function.
Last edited by ws (2015-04-01 19:04:42)
Offline
I understand that I have to do to override TRecord.Validate class, as well I did. In fact I intend to do a review of the field "name" persona table, it not accepted short description.
I'm using as a basis the project "30 - MVC Server" -> MVCServer.dpr
The following model code. What else to do?
unit RESTModel;
interface
uses
SynCommons,
mORMot;
type
TPersona = class(TSQLRecord) // TSQLRecord has already ID: integer primary key
private
fName: RawUTF8;
public
function Validate(aRest: TSQLRest; const aFields: TSQLFieldBits=[0..MAX_SQLFIELDS-1];
aInvalidFieldIndex: PInteger=nil; aValidator: PSynValidate=nil): string; override;
published
/// ORM will create a NAME VARCHAR(80) column
property Name: RawUTF8 index 80 read fName write fName;
protected
end;
function DataModel: TSQLModel;
const
SERVER_ROOT = 'root';
SERVER_PORT = '888';
implementation
function TPersona.Validate(aRest: TSQLRest; const aFields: TSQLFieldBits=[0..MAX_SQLFIELDS-1];
aInvalidFieldIndex: PInteger=nil; aValidator: PSynValidate=nil): string;
var text :String;
begin
text := '';
// pseudo code
if length(name)<=5 then
text := '{"error":"should be above 5 characters"}';
result := UTF8ToString(text);
end;
function DataModel: TSQLModel;
begin
result := TSQLModel.Create([TPersona],SERVER_ROOT);
TPersona.AddFilterOrValidate('Name',TSynValidateText.Create('{"minlength": 5}'));
end;
end.
Last edited by ws (2015-04-02 14:57:24)
Offline
Hi there, I've tested the TSynFilterUppercase and worked as expected, but it seems that auto validate is not working as expected when you uses MinLength and MaxLength parameters.
An workaround would be at client side, to handle manually, something like this:
var aFilter : TSynValidateText;
msg : string;
----------
msg := '';
aFilter := TSynValidateText.Create('{"MinLength":5,"MaxLength":10}');
TSQLPersona.AddFilterOrValidate('Name',aFilter);
{
aPersona.Filter();
aPersona.Validate(aClient);
}
....
aPersona.Name := 'Nam'+Int32ToUtf8(Random(10000));
...
if (aFilter.Process(0,aPersona.Name,msg)) then begin
aClient.Add(Rec,true);
Last edited by warleyalex (2015-04-02 15:44:56)
Offline
ok, now I understand. I'll need to use Delphi to my client app, and if I am working with client made with html / css / js, is it possible that my server app return me an error message if there was any violation in my restrictions?
it would be possible when making an insert / update / delete and there was no error return a message for example: {"success": "ok"} on error: {"error": "Message"}
Offline
Pages: 1