Forum Discussion

carstenw's avatar
carstenw
Creator II
9 years ago

Set form fields to 'required' based on state

Hi all, I hope you can help me with this one: I have a workflow controlled Item with some properties set as required. When the Item reaches a certain state, some more properties should be made required. Until this state is reached these properties are also hidden, so I can't just make them required on the ItemType, as the user would have no way of entering data into the hidden fields. Showing / hiding the fields on a specific state is achieved using javascript, now I need to set them required, prevent saving the form if they are not filled and paint them blue to indicate they are mandatory. Any help on the matter will be greatly appreciated.   Best regards Carsten

4 Replies

  • on an event like "onBeforeUpdate"or on the lifecycle transition your context will be the full content of your object. So in your method you should do some test like:
    if (this.getProperty("product_line","")==""){
    
    return this.newError("product line not selected");
    
    }
  • Hi Carsten, Setting a property as required impacts the Database configuration which has no idea of workflows/lifecycle, so it can't be adapted to certain states. What you need to do is the following:
    • Client side : for painting in blue the fields, as it will not be the Out of the box required configuration, you will need to do it via CSS. Either by applying CSS through javascript or by applying a class on the field when it is required and having a CSS on the form which will set this class with a blue background.
    • Server Side: you need to have a method OnBeforeUpdate which will check the state and then check for the mandatory fields (you could also have this verification on a lifecycle transition, which may make more sense). I have seen implémentations where you had an extra itemtype (or maybe a filtered list could work just fine) where you would store the information of required fields per state.
    Does it help ?  
  • Hi Yoann, thanks for getting back to me on this! I am still having some trouble, namely to get the values of the fields to the server method. Lets say I have a drop-down called "product_line". How can my server method know if anything have been selected in this drop-down?
  • Thanks Yoann, this was what I was looking for. Idadvertently I used the names of the form selects instead of the data source(doh!) For completeness sake and for future readers, here is my solution for the server method [edit]sorry for the formatting on the code, don't know how to make it nice[/edit]
    
    var inn = this.getInnovator();
    
    var niceFields = new List<KeyValuePair<string, string>>
    {
    new KeyValuePair<string, string>("part_number", "Part Number"),
    new KeyValuePair<string, string>("uom_dd", "Unit of Measure"),
    new KeyValuePair<string, string>("purch_man_dd", "Purchased / Manufactured"),
    new KeyValuePair<string, string>("lot_serial_dd", "Lot / Serial"),
    new KeyValuePair<string, string>("product_line_dd", "Product Line"),
    new KeyValuePair<string, string>("ksup_dd", "KSUP"),
    new KeyValuePair<string, string>("item_type_dd", "Item Type")
    };
    
    var lcState = this.getProperty("state");
    
    if (lcState == "Awaiting Part Details")
    {
    foreach (var partField in niceFields)
    {
    if (this.getProperty(partField.Key, "") == "")
    {
    return inn.newError("Please fill out " + partField.Value);
    }
    }
    }
    return this;