Serenity: Check data before recording

Created on 20 Feb 2018  路  8Comments  路  Source: serenity-is/Serenity

How to verify if the data in the form already exists in the DB and so do not duplicate the records.
Image Example.
The product "Alice Mutton" already exists for the supllier and the category, but clicking the save button will save again, thus doubling the product.
How can I validate this before saving?
How to compare the name of the product that is in the form if it already exists in the DB?
image

In a windows forms application I do this:
var content = (from c in mdc.Products
where c.DescricaoProduto == txtProduto.Text
select c);

                 if (conteID.Count ()> 0)
                 {
                     MessageBox.Show ("Product Already Registered !!");
                     txtProduct.Focus ();
                 }

But inside the serenity I can not.

Most helpful comment

@juliermefelix Hope this help:

private class MySaveHandler : Common.SequenceGUID.SequenceGUIDSaveHandler<MyRow> {
    protected override void BeforeSave()
    {
        base.BeforeSave();

        if (!this.Connection.Exists<SupplierRow>(MyRow.Fields.CompanyName == this.Row.CompanyName))
        {
            // do anything you want to do
        }
        else
        {
            throw new ValidationError("This supplier already registered");
        }
    }
}

By the way, you can use UniqueConstraint attribute on ROW class to validate, something like:

[UniqueConstraint(new string[] { "SupplierId", "CompanyName"})]
public sealed class ViewInventoryQuoteRow : Row, IIdRow, INameRow
{
   ...
}

All 8 comments

i think u should not do this in client. In client u just validate if this validate is simple( like check format, number or string, etc.. ).
if u still want to validate data exist in DB.

  • u cant retrieve data in ts in check it, using event change or addValidateRule.
  • Validate on Create, update in repository.cs

I'm trying to make a repository a beforesave, but I'm not able to reference the dialog fields so I can check in the database.

private class MySaveHandler: SaveRequestHandler {

聽聽聽聽聽聽聽聽聽聽聽聽 protected override void BeforeSave ()
聽聽聽聽聽聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 base.BeforeSave ();
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 if (base.IsCreate)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var memberID = fld.Idemember;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var eventID = fld.IDEvento;
聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽 'I'm not sure how to reference the fields in the form (dialog) to do the tests here comparing with the variables.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 }

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽聽 }

聽聽聽聽聽聽聽聽 }

im not sure for you solution. im usually validate on typescript.

if u C# do u try this? sorry guy because my project dont use repository (just use endpoints file) so i dont have much knowledge about it
`
private Boolean Validatesomething(SaveRequest request)
{
//validate something like

        var a = request.Entity.CatCode;

        var b = request.Entity.ChoiceCode;


        return true;
    }

    public SaveResponse Create(IUnitOfWork uow, SaveRequest<MyRow> request)
    {
        var b = Validatesomething(request);

        return new MySaveHandler().Process(uow, request, SaveRequestType.Create);
    }

    public SaveResponse Update(IUnitOfWork uow, SaveRequest<MyRow> request)
    {
        var b = Validatesomething(request);

        return new MySaveHandler().Process(uow, request, SaveRequestType.Update);
    }`

I need something like this:

private class MySaveHandler: SaveRequestHandler {

         protected override void BeforeSave ()
         {

             base.BeforeSave ();
                var FormInscricao = new Forms.InscricaoForm ();
                 var campoform = FormInscricao.Idember;

             var content = (from c in MyRow.Fields
                            where c.IdMember == campoform
                            select c);

             if (content.Count ()> 0)

                 {
                  throw new ValidationError ("user already registered");

                 }


         }

     }

but this is giving error in the line of "where"

@juliermefelix Hope this help:

private class MySaveHandler : Common.SequenceGUID.SequenceGUIDSaveHandler<MyRow> {
    protected override void BeforeSave()
    {
        base.BeforeSave();

        if (!this.Connection.Exists<SupplierRow>(MyRow.Fields.CompanyName == this.Row.CompanyName))
        {
            // do anything you want to do
        }
        else
        {
            throw new ValidationError("This supplier already registered");
        }
    }
}

By the way, you can use UniqueConstraint attribute on ROW class to validate, something like:

[UniqueConstraint(new string[] { "SupplierId", "CompanyName"})]
public sealed class ViewInventoryQuoteRow : Row, IIdRow, INameRow
{
   ...
}

@minhhungit
Thank you!
It worked now.

@minhhungit
Thank you!
It worked now.

@juliermefelix
You probably know this by now - but here is how you access the entity that was entered into the form fields.. this.Row.

                 base.BeforeSave ();
                 if (base.IsCreate)
                 {
                     var memberID = fld.Idemember;
                     var eventID = fld.IDEvento;
         //    should be this to access your new Entity
                       var memberID = this.Row.Idemember;
                     var eventID = this.Row.IDEvento;



  'I'm not sure how to reference the fields in the form (dialog) to do the tests here comparing with the variables.

                 }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GitHubOrim picture GitHubOrim  路  3Comments

moostafaa picture moostafaa  路  3Comments

kilroyFR picture kilroyFR  路  3Comments

Amitloh picture Amitloh  路  3Comments

chintankukadiya18 picture chintankukadiya18  路  3Comments