FatFractal customer forums



Author Topic: Required fields?  (Read 1603 times)

drdaz7

  • Newbie
  • *
  • Posts: 34
    • View Profile
Required fields?
« on: April 29, 2014, 09:21:26 AM »
Hello there!

I've skimmed and Googled the documentation, but I haven't found anything as yet about this...

Is there some way of indicating that certain fields are required on an object we are committing, and error out when those fields are missing? I can add a layer to do this kind of thing in the client code, but it would be pretty ugly.

Cheers

jonnycools

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Required fields?
« Reply #1 on: April 29, 2014, 09:26:34 AM »
You can only define the schema in fddl. What you need to do here is a create PRE handler on the collection crom here you can check in JavaScript if something is missing or the user doesn't have permission to edit... Then just throw an error

gkc

  • Administrator
  • *****
  • Posts: 375
    • View Profile
Re: Required fields?
« Reply #2 on: April 29, 2014, 02:11:21 PM »
For example, to assert that newly-created objects in an /OrderLine collection should always have a reference to a parent order in (let's say) the /Order collection, and a reference to a product in the /Product collection, and to assert that objects in /OrderLine may not have their parent order changed, you could do the following.

in application.ffdl (partial)
Code: [Select]
CREATE OBJECTTYPE OrderLine (product REFERENCE /Product, order REFERENCE /Order)
CREATE COLLECTION /OrderLine OBJECTTYPE OrderLine

CREATE HANDLER validateOrderLineCreate PRE ON /OrderLine CREATE AS javascript:require('scripts/EventHandlers.js').validateOrderLineCreate();
CREATE HANDLER validateOrderLineUpdate PRE ON /OrderLine UPDATE AS javascript:require('scripts/EventHandlers.js').validateOrderLineUpdate();

in EventHandlers.js
Code: [Select]
var ff = require('ffef/FatFractal');
exports.validateOrderLineCreate = function() {
    ff.logger.info ("Executing event handler: validateOrderLineCreate");

    var orderLine = ff.getEventHandlerData();

    var order = ff.getReferredObject('order', orderLine);
    if (! order) {
        var errMsg = 'OrderLine ' + JSON.stringify(orderLine) + ' does not have a parent order assigned';
        ff.logger.error (errMsg);
        throw  {statusCode:400, statusMessage:errMsg};
    }

    var product = ff.getReferredObject('product', orderLine);
    if (! product) {
        var errMsg = 'OrderLine ' + JSON.stringify(orderLine) + ' does not have a product reference';
        ff.logger.error (errMsg);
        throw  {statusCode:400, statusMessage:errMsg};
    }
};

function getRefUrl(obj, refName) {
    if (! obj || ! obj.ffRefs || ! obj.ffRefs[0] || ! refName)
        return null;
    for (var i = 0; i < obj.ffRefs.length; i++) {
        if (obj.ffRefs[i].name == refName) {
            return obj.ffRefs[i].url;
        }
    }
    return null;
}

function refChanged(oldObj, newObj, refName) {
    var oldRefUrl = getRefUrl(oldObj, refName);
    var newRefUrl = getRefUrl(newObj, refName);

    print ("refChanged determined that oldRefUrl is [" + oldRefUrl + "] and newRefUrl is [" + newRefUrl + "]");

    return (! (oldRefUrl == newRefUrl));
}

exports.validateOrderLineUpdate = function() {
    ff.logger.info ("Executing event handler: validateOrderLineUpdate");

    var oldOrderLine = ff.getUpdateEventHandlerData()['old'];
    var newOrderLine = ff.getUpdateEventHandlerData()['new'];

    if (refChanged(oldOrderLine, newOrderLine, 'order') {
        var errMsg = 'OrderLine may not be updated to be associated with a different order';
        ff.logger.error (errMsg);
        throw  {statusCode:400, statusMessage:errMsg};
    }
};


Adding in validations of ordinary fields is very straightforward; this is a more complicated use case which comes up quite often.
« Last Edit: April 29, 2014, 02:13:38 PM by gkc »

 

Copyright © FatFractal customer forums