checking fields before creating an item

hello,

i have created an itemtype, when i create an item of this type the forms contains couples of field. for each couples i need to check if one of the item as a value the other one should too.

i had an idea of using a method on before add, check if the couples respect the rule i want and if not i want to cancel the creation of the part and have the user fix the missing value.

let me know if you think of a beter way or if u know how to cancel the creation of an item on an on before add event.

thanks.

Lucas

  • Hi Lucas,

    can you describe your use case more detailed?

    If you want to prevent that users save new items that have missing values you have a couple of options:
    1. Simply mark the property as "required"
    2. Define an xpath "pattern" for your property. This one is useful when your field has to contain a certain kind of input (e.g. number).
    3. Use an "onBeforeAdd/Update/Version" Method that checks the properties. This variant is preferred, when you need more "intelligent" checks that you cannot cover with the options above.

  • hello,

    i think i need an intelligent checking because i want to implement a multi field logic  .

    a simple approach of my problem would be, two fields on an item creation form and if one of them is filled in the other one should be too and if its not the case the user should not be able to create the item and should be able to modify the invalids inputs .

    but the thing i don't understand is how to disallow the creation of the part if the logic i implement gives a "false" output (when one field is filled in and the other isn't) or basically how to replicate the "required" behaviour through code

    thanks,

    Lucas.

  • Simple throw an error inside your onAfterAdd/Update/Version Method. This way you can also give your users some feedback about what they did wrong.

    e.g.

    Innovator inn = this.getInnovator();
    var myprop = this.getProperty("name","");
    if (myprop != "sampleValue")
    {
       return inn.newError("Cannot save item, name is wrong");
    }

    The newError will prevent that your new or updated item will be saved.