Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular-formly custom Validation on multiple fields on a repeat section

I was trying to create a custom validator for angular-formly repeated section.

The form should be valid only if the sum of the percentage inputs is 100. So for example if repeat section has 2 fields 50 and 50 should be a valid option or 25 and 75 and so on.

While I was working on a JSbin in order to do that I found out that the repeated model is not actually updated onKeydown. Therefore iterating though all the repeat section values and calculating their sum is not possible.

I also tried with modelOptions: { updateOn: 'Keydown' } with no success. It actually makes the validator not to get called at all.


UPDATE

I came up with the following solution from the matching fields example. Unfortunately it appears that the example its self has a problem.

Play with the following JSbin and see that there are many cases where the validator gets called and returns true but the field/fields still remain red (indicating they have a problem).

Here is the JSBin.

like image 302
George Nikolaides Avatar asked Dec 15 '15 08:12

George Nikolaides


2 Answers

Apologies since I didn't had the time to get back on this one. It has an open issue on GitHub for 2 months now. I fixed it temporary by using 7.1.2 version of angular-formly and just waiting for an update. The updated version of JSBin I have on the question should be working.

UPDATE

Since I had time and fixed this with repeat section (with a hacky way of course) I tough I should post it in case someone else is looking for this.

(note: the solution doesn't depend on formly version)

Working JSBin

like image 185
George Nikolaides Avatar answered Oct 14 '22 13:10

George Nikolaides


You made a typo while using modelOptions: { updateOn: 'Keydown' } the Keydown's k should be small case instead of uppercase, after fixing typo its working as expected. Also the ngModelOptions accept all event name in small case like keydown, keyup, blur, focus, etc.

modelOptions: { updateOn: 'keydown' }

Forked Plunkr

like image 1
Pankaj Parkar Avatar answered Oct 14 '22 14:10

Pankaj Parkar