Constraint conditional on positive value in another entry in same record - is that possible?

Our app holds student attainment data. A teacher is supposed to stop the test (which has eight components) when a student scores zero. Thus after a zero there must be no positive values entered within the same record. Is there a way to set a constraint to achieve this (no positive value after a zero, otherwise an error is generated)?

We looked at your guidance on RegEx (https://backendless.com/feature-54-validating-data-object-properties-with-regular-expressions/) but it doesn’t seem to fit the case.

Thank you

Hi Michael,

It is only possible to set static constraints (e.g. “value is never greater than X”), but not dynamic ones like you describe. However, you can achieve this behaviour by having a “beforeUpdate” handler, which would assert whether the current value is 0 and deny the operation if it is (this is assuming the student starts a test with a max value, not with a 0).

Please inform us whether this could work for you, otherwise we’ll try to look for other options.

Thanks Sergey. The student starts each test with 0 for all 8 items. The kind of score that would be acceptable is 4,2,1,3,0 and what must not be accepted is 4,2,1,3,0,2 (even worse would be 0,4,2,1,3,0,2). Can that be fixed within Backendless using “beforeUpdate” or any other approach? Or must we create a separate constraint within the user interface ?

Yes, this can be fixed with a “beforeUpdate” handler, but you’ll have to start the test with some non-zero values, NULL for example. This way in the beforeUpdate handler you can check whether one of the items is zero - which would mean it was set to zero - which in effect means that the following items should be declined.

Great. All values can be preset to Null but would we also have to ensure that any Null inside a sequence of positive integers is overwritten by a real zero so that it triggers the rejection? I will talk to our software engineer and see if he knows how to implement this. I will come back when he responds.

In case all values are preset to Null simply the presence of zero in one of the values would unequivocally mean that it was overwritten by a real zero. In pseudocode your handler code should be like the following:

if( col1 = 0 || col2 = 0 || ... || col8 = 0 )
  //reject the call