Constraints on database or form validation?


I’m working on a prototype of a hobby project. In it, there is the concept of a portfolio, and each portfolio has allocations, but those allocations cannot add up to greater than 100%, and they must total 100%.

Portfolio ZZZ
-allocation 1: 50%
-allocation 2: 50%

If the user tries to add another allocation, they shouldn’t be able to without first decreasing the % of one of the others. A portfolio can have unlimited allocations as long as they add up to 100%.

Also is there any way to do this on one screen? That is allow the addition of multiple allocations to one portfolio with a multi line form (each line an allocation) where more lines can be added if needed?