forms.mjs 292 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669
  1. /**
  2. * @license Angular v19.2.13
  3. * (c) 2010-2025 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. import * as i0 from '@angular/core';
  7. import { InjectionToken, Directive, forwardRef, Optional, Inject, ɵisPromise as _isPromise, ɵisSubscribable as _isSubscribable, ɵRuntimeError as _RuntimeError, Self, untracked, computed, signal, EventEmitter, Input, Host, SkipSelf, booleanAttribute, ChangeDetectorRef, Output, inject, Injectable, NgModule, Version } from '@angular/core';
  8. import { ɵgetDOM as _getDOM } from '@angular/common';
  9. import { from, forkJoin, Subject } from 'rxjs';
  10. import { map } from 'rxjs/operators';
  11. /**
  12. * Base class for all ControlValueAccessor classes defined in Forms package.
  13. * Contains common logic and utility functions.
  14. *
  15. * Note: this is an *internal-only* class and should not be extended or used directly in
  16. * applications code.
  17. */
  18. class BaseControlValueAccessor {
  19. _renderer;
  20. _elementRef;
  21. /**
  22. * The registered callback function called when a change or input event occurs on the input
  23. * element.
  24. * @docs-private
  25. */
  26. onChange = (_) => { };
  27. /**
  28. * The registered callback function called when a blur event occurs on the input element.
  29. * @docs-private
  30. */
  31. onTouched = () => { };
  32. constructor(_renderer, _elementRef) {
  33. this._renderer = _renderer;
  34. this._elementRef = _elementRef;
  35. }
  36. /**
  37. * Helper method that sets a property on a target element using the current Renderer
  38. * implementation.
  39. * @docs-private
  40. */
  41. setProperty(key, value) {
  42. this._renderer.setProperty(this._elementRef.nativeElement, key, value);
  43. }
  44. /**
  45. * Registers a function called when the control is touched.
  46. * @docs-private
  47. */
  48. registerOnTouched(fn) {
  49. this.onTouched = fn;
  50. }
  51. /**
  52. * Registers a function called when the control value changes.
  53. * @docs-private
  54. */
  55. registerOnChange(fn) {
  56. this.onChange = fn;
  57. }
  58. /**
  59. * Sets the "disabled" property on the range input element.
  60. * @docs-private
  61. */
  62. setDisabledState(isDisabled) {
  63. this.setProperty('disabled', isDisabled);
  64. }
  65. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: BaseControlValueAccessor, deps: [{ token: i0.Renderer2 }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
  66. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: BaseControlValueAccessor, isStandalone: true, ngImport: i0 });
  67. }
  68. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: BaseControlValueAccessor, decorators: [{
  69. type: Directive
  70. }], ctorParameters: () => [{ type: i0.Renderer2 }, { type: i0.ElementRef }] });
  71. /**
  72. * Base class for all built-in ControlValueAccessor classes (except DefaultValueAccessor, which is
  73. * used in case no other CVAs can be found). We use this class to distinguish between default CVA,
  74. * built-in CVAs and custom CVAs, so that Forms logic can recognize built-in CVAs and treat custom
  75. * ones with higher priority (when both built-in and custom CVAs are present).
  76. *
  77. * Note: this is an *internal-only* class and should not be extended or used directly in
  78. * applications code.
  79. */
  80. class BuiltInControlValueAccessor extends BaseControlValueAccessor {
  81. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: BuiltInControlValueAccessor, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  82. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: BuiltInControlValueAccessor, isStandalone: true, usesInheritance: true, ngImport: i0 });
  83. }
  84. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: BuiltInControlValueAccessor, decorators: [{
  85. type: Directive
  86. }] });
  87. /**
  88. * Used to provide a `ControlValueAccessor` for form controls.
  89. *
  90. * See `DefaultValueAccessor` for how to implement one.
  91. *
  92. * @publicApi
  93. */
  94. const NG_VALUE_ACCESSOR = new InjectionToken(ngDevMode ? 'NgValueAccessor' : '');
  95. const CHECKBOX_VALUE_ACCESSOR = {
  96. provide: NG_VALUE_ACCESSOR,
  97. useExisting: forwardRef(() => CheckboxControlValueAccessor),
  98. multi: true,
  99. };
  100. /**
  101. * @description
  102. * A `ControlValueAccessor` for writing a value and listening to changes on a checkbox input
  103. * element.
  104. *
  105. * @usageNotes
  106. *
  107. * ### Using a checkbox with a reactive form.
  108. *
  109. * The following example shows how to use a checkbox with a reactive form.
  110. *
  111. * ```ts
  112. * const rememberLoginControl = new FormControl();
  113. * ```
  114. *
  115. * ```html
  116. * <input type="checkbox" [formControl]="rememberLoginControl">
  117. * ```
  118. *
  119. * @ngModule ReactiveFormsModule
  120. * @ngModule FormsModule
  121. * @publicApi
  122. */
  123. class CheckboxControlValueAccessor extends BuiltInControlValueAccessor {
  124. /**
  125. * Sets the "checked" property on the input element.
  126. * @docs-private
  127. */
  128. writeValue(value) {
  129. this.setProperty('checked', value);
  130. }
  131. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CheckboxControlValueAccessor, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  132. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: CheckboxControlValueAccessor, isStandalone: false, selector: "input[type=checkbox][formControlName],input[type=checkbox][formControl],input[type=checkbox][ngModel]", host: { listeners: { "change": "onChange($event.target.checked)", "blur": "onTouched()" } }, providers: [CHECKBOX_VALUE_ACCESSOR], usesInheritance: true, ngImport: i0 });
  133. }
  134. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CheckboxControlValueAccessor, decorators: [{
  135. type: Directive,
  136. args: [{
  137. selector: 'input[type=checkbox][formControlName],input[type=checkbox][formControl],input[type=checkbox][ngModel]',
  138. host: { '(change)': 'onChange($event.target.checked)', '(blur)': 'onTouched()' },
  139. providers: [CHECKBOX_VALUE_ACCESSOR],
  140. standalone: false,
  141. }]
  142. }] });
  143. const DEFAULT_VALUE_ACCESSOR = {
  144. provide: NG_VALUE_ACCESSOR,
  145. useExisting: forwardRef(() => DefaultValueAccessor),
  146. multi: true,
  147. };
  148. /**
  149. * We must check whether the agent is Android because composition events
  150. * behave differently between iOS and Android.
  151. */
  152. function _isAndroid() {
  153. const userAgent = _getDOM() ? _getDOM().getUserAgent() : '';
  154. return /android (\d+)/.test(userAgent.toLowerCase());
  155. }
  156. /**
  157. * @description
  158. * Provide this token to control if form directives buffer IME input until
  159. * the "compositionend" event occurs.
  160. * @publicApi
  161. */
  162. const COMPOSITION_BUFFER_MODE = new InjectionToken(ngDevMode ? 'CompositionEventMode' : '');
  163. /**
  164. * The default `ControlValueAccessor` for writing a value and listening to changes on input
  165. * elements. The accessor is used by the `FormControlDirective`, `FormControlName`, and
  166. * `NgModel` directives.
  167. *
  168. *
  169. * @usageNotes
  170. *
  171. * ### Using the default value accessor
  172. *
  173. * The following example shows how to use an input element that activates the default value accessor
  174. * (in this case, a text field).
  175. *
  176. * ```ts
  177. * const firstNameControl = new FormControl();
  178. * ```
  179. *
  180. * ```html
  181. * <input type="text" [formControl]="firstNameControl">
  182. * ```
  183. *
  184. * This value accessor is used by default for `<input type="text">` and `<textarea>` elements, but
  185. * you could also use it for custom components that have similar behavior and do not require special
  186. * processing. In order to attach the default value accessor to a custom element, add the
  187. * `ngDefaultControl` attribute as shown below.
  188. *
  189. * ```html
  190. * <custom-input-component ngDefaultControl [(ngModel)]="value"></custom-input-component>
  191. * ```
  192. *
  193. * @ngModule ReactiveFormsModule
  194. * @ngModule FormsModule
  195. * @publicApi
  196. */
  197. class DefaultValueAccessor extends BaseControlValueAccessor {
  198. _compositionMode;
  199. /** Whether the user is creating a composition string (IME events). */
  200. _composing = false;
  201. constructor(renderer, elementRef, _compositionMode) {
  202. super(renderer, elementRef);
  203. this._compositionMode = _compositionMode;
  204. if (this._compositionMode == null) {
  205. this._compositionMode = !_isAndroid();
  206. }
  207. }
  208. /**
  209. * Sets the "value" property on the input element.
  210. * @docs-private
  211. */
  212. writeValue(value) {
  213. const normalizedValue = value == null ? '' : value;
  214. this.setProperty('value', normalizedValue);
  215. }
  216. /** @internal */
  217. _handleInput(value) {
  218. if (!this._compositionMode || (this._compositionMode && !this._composing)) {
  219. this.onChange(value);
  220. }
  221. }
  222. /** @internal */
  223. _compositionStart() {
  224. this._composing = true;
  225. }
  226. /** @internal */
  227. _compositionEnd(value) {
  228. this._composing = false;
  229. this._compositionMode && this.onChange(value);
  230. }
  231. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: DefaultValueAccessor, deps: [{ token: i0.Renderer2 }, { token: i0.ElementRef }, { token: COMPOSITION_BUFFER_MODE, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
  232. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: DefaultValueAccessor, isStandalone: false, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]", host: { listeners: { "input": "$any(this)._handleInput($event.target.value)", "blur": "onTouched()", "compositionstart": "$any(this)._compositionStart()", "compositionend": "$any(this)._compositionEnd($event.target.value)" } }, providers: [DEFAULT_VALUE_ACCESSOR], usesInheritance: true, ngImport: i0 });
  233. }
  234. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: DefaultValueAccessor, decorators: [{
  235. type: Directive,
  236. args: [{
  237. selector: 'input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]',
  238. // TODO: vsavkin replace the above selector with the one below it once
  239. // https://github.com/angular/angular/issues/3011 is implemented
  240. // selector: '[ngModel],[formControl],[formControlName]',
  241. host: {
  242. '(input)': '$any(this)._handleInput($event.target.value)',
  243. '(blur)': 'onTouched()',
  244. '(compositionstart)': '$any(this)._compositionStart()',
  245. '(compositionend)': '$any(this)._compositionEnd($event.target.value)',
  246. },
  247. providers: [DEFAULT_VALUE_ACCESSOR],
  248. standalone: false,
  249. }]
  250. }], ctorParameters: () => [{ type: i0.Renderer2 }, { type: i0.ElementRef }, { type: undefined, decorators: [{
  251. type: Optional
  252. }, {
  253. type: Inject,
  254. args: [COMPOSITION_BUFFER_MODE]
  255. }] }] });
  256. function isEmptyInputValue(value) {
  257. return value == null || lengthOrSize(value) === 0;
  258. }
  259. /**
  260. * Extract the length property in case it's an array or a string.
  261. * Extract the size property in case it's a set.
  262. * Return null else.
  263. * @param value Either an array, set or undefined.
  264. */
  265. function lengthOrSize(value) {
  266. // non-strict comparison is intentional, to check for both `null` and `undefined` values
  267. if (value == null) {
  268. return null;
  269. }
  270. else if (Array.isArray(value) || typeof value === 'string') {
  271. return value.length;
  272. }
  273. else if (value instanceof Set) {
  274. return value.size;
  275. }
  276. return null;
  277. }
  278. /**
  279. * @description
  280. * An `InjectionToken` for registering additional synchronous validators used with
  281. * `AbstractControl`s.
  282. *
  283. * @see {@link NG_ASYNC_VALIDATORS}
  284. *
  285. * @usageNotes
  286. *
  287. * ### Providing a custom validator
  288. *
  289. * The following example registers a custom validator directive. Adding the validator to the
  290. * existing collection of validators requires the `multi: true` option.
  291. *
  292. * ```ts
  293. * @Directive({
  294. * selector: '[customValidator]',
  295. * providers: [{provide: NG_VALIDATORS, useExisting: CustomValidatorDirective, multi: true}]
  296. * })
  297. * class CustomValidatorDirective implements Validator {
  298. * validate(control: AbstractControl): ValidationErrors | null {
  299. * return { 'custom': true };
  300. * }
  301. * }
  302. * ```
  303. *
  304. * @publicApi
  305. */
  306. const NG_VALIDATORS = new InjectionToken(ngDevMode ? 'NgValidators' : '');
  307. /**
  308. * @description
  309. * An `InjectionToken` for registering additional asynchronous validators used with
  310. * `AbstractControl`s.
  311. *
  312. * @see {@link NG_VALIDATORS}
  313. *
  314. * @usageNotes
  315. *
  316. * ### Provide a custom async validator directive
  317. *
  318. * The following example implements the `AsyncValidator` interface to create an
  319. * async validator directive with a custom error key.
  320. *
  321. * ```ts
  322. * @Directive({
  323. * selector: '[customAsyncValidator]',
  324. * providers: [{provide: NG_ASYNC_VALIDATORS, useExisting: CustomAsyncValidatorDirective, multi:
  325. * true}]
  326. * })
  327. * class CustomAsyncValidatorDirective implements AsyncValidator {
  328. * validate(control: AbstractControl): Promise<ValidationErrors|null> {
  329. * return Promise.resolve({'custom': true});
  330. * }
  331. * }
  332. * ```
  333. *
  334. * @publicApi
  335. */
  336. const NG_ASYNC_VALIDATORS = new InjectionToken(ngDevMode ? 'NgAsyncValidators' : '');
  337. /**
  338. * A regular expression that matches valid e-mail addresses.
  339. *
  340. * At a high level, this regexp matches e-mail addresses of the format `local-part@tld`, where:
  341. * - `local-part` consists of one or more of the allowed characters (alphanumeric and some
  342. * punctuation symbols).
  343. * - `local-part` cannot begin or end with a period (`.`).
  344. * - `local-part` cannot be longer than 64 characters.
  345. * - `tld` consists of one or more `labels` separated by periods (`.`). For example `localhost` or
  346. * `foo.com`.
  347. * - A `label` consists of one or more of the allowed characters (alphanumeric, dashes (`-`) and
  348. * periods (`.`)).
  349. * - A `label` cannot begin or end with a dash (`-`) or a period (`.`).
  350. * - A `label` cannot be longer than 63 characters.
  351. * - The whole address cannot be longer than 254 characters.
  352. *
  353. * ## Implementation background
  354. *
  355. * This regexp was ported over from AngularJS (see there for git history):
  356. * https://github.com/angular/angular.js/blob/c133ef836/src/ng/directive/input.js#L27
  357. * It is based on the
  358. * [WHATWG version](https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address) with
  359. * some enhancements to incorporate more RFC rules (such as rules related to domain names and the
  360. * lengths of different parts of the address). The main differences from the WHATWG version are:
  361. * - Disallow `local-part` to begin or end with a period (`.`).
  362. * - Disallow `local-part` length to exceed 64 characters.
  363. * - Disallow total address length to exceed 254 characters.
  364. *
  365. * See [this commit](https://github.com/angular/angular.js/commit/f3f5cf72e) for more details.
  366. */
  367. const EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
  368. /**
  369. * @description
  370. * Provides a set of built-in validators that can be used by form controls.
  371. *
  372. * A validator is a function that processes a `FormControl` or collection of
  373. * controls and returns an error map or null. A null map means that validation has passed.
  374. *
  375. * @see [Form Validation](guide/forms/form-validation)
  376. *
  377. * @publicApi
  378. */
  379. class Validators {
  380. /**
  381. * @description
  382. * Validator that requires the control's value to be greater than or equal to the provided number.
  383. *
  384. * @usageNotes
  385. *
  386. * ### Validate against a minimum of 3
  387. *
  388. * ```ts
  389. * const control = new FormControl(2, Validators.min(3));
  390. *
  391. * console.log(control.errors); // {min: {min: 3, actual: 2}}
  392. * ```
  393. *
  394. * @returns A validator function that returns an error map with the
  395. * `min` property if the validation check fails, otherwise `null`.
  396. *
  397. * @see {@link /api/forms/AbstractControl#updateValueAndValidity updateValueAndValidity}
  398. *
  399. */
  400. static min(min) {
  401. return minValidator(min);
  402. }
  403. /**
  404. * @description
  405. * Validator that requires the control's value to be less than or equal to the provided number.
  406. *
  407. * @usageNotes
  408. *
  409. * ### Validate against a maximum of 15
  410. *
  411. * ```ts
  412. * const control = new FormControl(16, Validators.max(15));
  413. *
  414. * console.log(control.errors); // {max: {max: 15, actual: 16}}
  415. * ```
  416. *
  417. * @returns A validator function that returns an error map with the
  418. * `max` property if the validation check fails, otherwise `null`.
  419. *
  420. * @see {@link /api/forms/AbstractControl#updateValueAndValidity updateValueAndValidity}
  421. *
  422. */
  423. static max(max) {
  424. return maxValidator(max);
  425. }
  426. /**
  427. * @description
  428. * Validator that requires the control have a non-empty value.
  429. *
  430. * @usageNotes
  431. *
  432. * ### Validate that the field is non-empty
  433. *
  434. * ```ts
  435. * const control = new FormControl('', Validators.required);
  436. *
  437. * console.log(control.errors); // {required: true}
  438. * ```
  439. *
  440. * @returns An error map with the `required` property
  441. * if the validation check fails, otherwise `null`.
  442. *
  443. * @see {@link /api/forms/AbstractControl#updateValueAndValidity updateValueAndValidity}
  444. *
  445. */
  446. static required(control) {
  447. return requiredValidator(control);
  448. }
  449. /**
  450. * @description
  451. * Validator that requires the control's value be true. This validator is commonly
  452. * used for required checkboxes.
  453. *
  454. * @usageNotes
  455. *
  456. * ### Validate that the field value is true
  457. *
  458. * ```ts
  459. * const control = new FormControl('some value', Validators.requiredTrue);
  460. *
  461. * console.log(control.errors); // {required: true}
  462. * ```
  463. *
  464. * @returns An error map that contains the `required` property
  465. * set to `true` if the validation check fails, otherwise `null`.
  466. *
  467. * @see {@link /api/forms/AbstractControl#updateValueAndValidity updateValueAndValidity}
  468. *
  469. */
  470. static requiredTrue(control) {
  471. return requiredTrueValidator(control);
  472. }
  473. /**
  474. * @description
  475. * Validator that requires the control's value pass an email validation test.
  476. *
  477. * Tests the value using a [regular
  478. * expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
  479. * pattern suitable for common use cases. The pattern is based on the definition of a valid email
  480. * address in the [WHATWG HTML
  481. * specification](https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address) with
  482. * some enhancements to incorporate more RFC rules (such as rules related to domain names and the
  483. * lengths of different parts of the address).
  484. *
  485. * The differences from the WHATWG version include:
  486. * - Disallow `local-part` (the part before the `@` symbol) to begin or end with a period (`.`).
  487. * - Disallow `local-part` to be longer than 64 characters.
  488. * - Disallow the whole address to be longer than 254 characters.
  489. *
  490. * If this pattern does not satisfy your business needs, you can use `Validators.pattern()` to
  491. * validate the value against a different pattern.
  492. *
  493. * @usageNotes
  494. *
  495. * ### Validate that the field matches a valid email pattern
  496. *
  497. * ```ts
  498. * const control = new FormControl('bad@', Validators.email);
  499. *
  500. * console.log(control.errors); // {email: true}
  501. * ```
  502. *
  503. * @returns An error map with the `email` property
  504. * if the validation check fails, otherwise `null`.
  505. *
  506. * @see {@link /api/forms/AbstractControl#updateValueAndValidity updateValueAndValidity}
  507. *
  508. */
  509. static email(control) {
  510. return emailValidator(control);
  511. }
  512. /**
  513. * @description
  514. * Validator that requires the number of items in the control's value to be greater than or equal
  515. * to the provided minimum length. This validator is also provided by default if you use
  516. * the HTML5 `minlength` attribute. Note that the `minLength` validator is intended to be used
  517. * only for types that have a numeric `length` or `size` property, such as strings, arrays or
  518. * sets. The `minLength` validator logic is also not invoked for values when their `length` or
  519. * `size` property is 0 (for example in case of an empty string or an empty array), to support
  520. * optional controls. You can use the standard `required` validator if empty values should not be
  521. * considered valid.
  522. *
  523. * @usageNotes
  524. *
  525. * ### Validate that the field has a minimum of 3 characters
  526. *
  527. * ```ts
  528. * const control = new FormControl('ng', Validators.minLength(3));
  529. *
  530. * console.log(control.errors); // {minlength: {requiredLength: 3, actualLength: 2}}
  531. * ```
  532. *
  533. * ```html
  534. * <input minlength="5">
  535. * ```
  536. *
  537. * @returns A validator function that returns an error map with the
  538. * `minlength` property if the validation check fails, otherwise `null`.
  539. *
  540. * @see {@link /api/forms/AbstractControl#updateValueAndValidity updateValueAndValidity}
  541. *
  542. */
  543. static minLength(minLength) {
  544. return minLengthValidator(minLength);
  545. }
  546. /**
  547. * @description
  548. * Validator that requires the number of items in the control's value to be less than or equal
  549. * to the provided maximum length. This validator is also provided by default if you use
  550. * the HTML5 `maxlength` attribute. Note that the `maxLength` validator is intended to be used
  551. * only for types that have a numeric `length` or `size` property, such as strings, arrays or
  552. * sets.
  553. *
  554. * @usageNotes
  555. *
  556. * ### Validate that the field has maximum of 5 characters
  557. *
  558. * ```ts
  559. * const control = new FormControl('Angular', Validators.maxLength(5));
  560. *
  561. * console.log(control.errors); // {maxlength: {requiredLength: 5, actualLength: 7}}
  562. * ```
  563. *
  564. * ```html
  565. * <input maxlength="5">
  566. * ```
  567. *
  568. * @returns A validator function that returns an error map with the
  569. * `maxlength` property if the validation check fails, otherwise `null`.
  570. *
  571. * @see {@link /api/forms/AbstractControl#updateValueAndValidity updateValueAndValidity}
  572. *
  573. */
  574. static maxLength(maxLength) {
  575. return maxLengthValidator(maxLength);
  576. }
  577. /**
  578. * @description
  579. * Validator that requires the control's value to match a regex pattern. This validator is also
  580. * provided by default if you use the HTML5 `pattern` attribute.
  581. *
  582. * @usageNotes
  583. *
  584. * ### Validate that the field only contains letters or spaces
  585. *
  586. * ```ts
  587. * const control = new FormControl('1', Validators.pattern('[a-zA-Z ]*'));
  588. *
  589. * console.log(control.errors); // {pattern: {requiredPattern: '^[a-zA-Z ]*$', actualValue: '1'}}
  590. * ```
  591. *
  592. * ```html
  593. * <input pattern="[a-zA-Z ]*">
  594. * ```
  595. *
  596. * ### Pattern matching with the global or sticky flag
  597. *
  598. * `RegExp` objects created with the `g` or `y` flags that are passed into `Validators.pattern`
  599. * can produce different results on the same input when validations are run consecutively. This is
  600. * due to how the behavior of `RegExp.prototype.test` is
  601. * specified in [ECMA-262](https://tc39.es/ecma262/#sec-regexpbuiltinexec)
  602. * (`RegExp` preserves the index of the last match when the global or sticky flag is used).
  603. * Due to this behavior, it is recommended that when using
  604. * `Validators.pattern` you **do not** pass in a `RegExp` object with either the global or sticky
  605. * flag enabled.
  606. *
  607. * ```ts
  608. * // Not recommended (since the `g` flag is used)
  609. * const controlOne = new FormControl('1', Validators.pattern(/foo/g));
  610. *
  611. * // Good
  612. * const controlTwo = new FormControl('1', Validators.pattern(/foo/));
  613. * ```
  614. *
  615. * @param pattern A regular expression to be used as is to test the values, or a string.
  616. * If a string is passed, the `^` character is prepended and the `$` character is
  617. * appended to the provided string (if not already present), and the resulting regular
  618. * expression is used to test the values.
  619. *
  620. * @returns A validator function that returns an error map with the
  621. * `pattern` property if the validation check fails, otherwise `null`.
  622. *
  623. * @see {@link /api/forms/AbstractControl#updateValueAndValidity updateValueAndValidity}
  624. *
  625. */
  626. static pattern(pattern) {
  627. return patternValidator(pattern);
  628. }
  629. /**
  630. * @description
  631. * Validator that performs no operation.
  632. *
  633. * @see {@link /api/forms/AbstractControl#updateValueAndValidity updateValueAndValidity}
  634. *
  635. */
  636. static nullValidator(control) {
  637. return nullValidator();
  638. }
  639. static compose(validators) {
  640. return compose(validators);
  641. }
  642. /**
  643. * @description
  644. * Compose multiple async validators into a single function that returns the union
  645. * of the individual error objects for the provided control.
  646. *
  647. * @returns A validator function that returns an error map with the
  648. * merged error objects of the async validators if the validation check fails, otherwise `null`.
  649. *
  650. * @see {@link /api/forms/AbstractControl#updateValueAndValidity updateValueAndValidity}
  651. *
  652. */
  653. static composeAsync(validators) {
  654. return composeAsync(validators);
  655. }
  656. }
  657. /**
  658. * Validator that requires the control's value to be greater than or equal to the provided number.
  659. * See `Validators.min` for additional information.
  660. */
  661. function minValidator(min) {
  662. return (control) => {
  663. if (control.value == null || min == null) {
  664. return null; // don't validate empty values to allow optional controls
  665. }
  666. const value = parseFloat(control.value);
  667. // Controls with NaN values after parsing should be treated as not having a
  668. // minimum, per the HTML forms spec: https://www.w3.org/TR/html5/forms.html#attr-input-min
  669. return !isNaN(value) && value < min ? { 'min': { 'min': min, 'actual': control.value } } : null;
  670. };
  671. }
  672. /**
  673. * Validator that requires the control's value to be less than or equal to the provided number.
  674. * See `Validators.max` for additional information.
  675. */
  676. function maxValidator(max) {
  677. return (control) => {
  678. if (control.value == null || max == null) {
  679. return null; // don't validate empty values to allow optional controls
  680. }
  681. const value = parseFloat(control.value);
  682. // Controls with NaN values after parsing should be treated as not having a
  683. // maximum, per the HTML forms spec: https://www.w3.org/TR/html5/forms.html#attr-input-max
  684. return !isNaN(value) && value > max ? { 'max': { 'max': max, 'actual': control.value } } : null;
  685. };
  686. }
  687. /**
  688. * Validator that requires the control have a non-empty value.
  689. * See `Validators.required` for additional information.
  690. */
  691. function requiredValidator(control) {
  692. return isEmptyInputValue(control.value) ? { 'required': true } : null;
  693. }
  694. /**
  695. * Validator that requires the control's value be true. This validator is commonly
  696. * used for required checkboxes.
  697. * See `Validators.requiredTrue` for additional information.
  698. */
  699. function requiredTrueValidator(control) {
  700. return control.value === true ? null : { 'required': true };
  701. }
  702. /**
  703. * Validator that requires the control's value pass an email validation test.
  704. * See `Validators.email` for additional information.
  705. */
  706. function emailValidator(control) {
  707. if (isEmptyInputValue(control.value)) {
  708. return null; // don't validate empty values to allow optional controls
  709. }
  710. return EMAIL_REGEXP.test(control.value) ? null : { 'email': true };
  711. }
  712. /**
  713. * Validator that requires the number of items in the control's value to be greater than or equal
  714. * to the provided minimum length. See `Validators.minLength` for additional information.
  715. *
  716. * The minLengthValidator respects every length property in an object, regardless of whether it's an array.
  717. * For example, the object {id: 1, length: 0, width: 0} should be validated.
  718. */
  719. function minLengthValidator(minLength) {
  720. return (control) => {
  721. const length = control.value?.length ?? lengthOrSize(control.value);
  722. if (length === null || length === 0) {
  723. // don't validate empty values to allow optional controls
  724. // don't validate values without `length` or `size` property
  725. return null;
  726. }
  727. return length < minLength
  728. ? { 'minlength': { 'requiredLength': minLength, 'actualLength': length } }
  729. : null;
  730. };
  731. }
  732. /**
  733. * Validator that requires the number of items in the control's value to be less than or equal
  734. * to the provided maximum length. See `Validators.maxLength` for additional information.
  735. *
  736. * The maxLengthValidator respects every length property in an object, regardless of whether it's an array.
  737. * For example, the object {id: 1, length: 0, width: 0} should be validated.
  738. */
  739. function maxLengthValidator(maxLength) {
  740. return (control) => {
  741. const length = control.value?.length ?? lengthOrSize(control.value);
  742. if (length !== null && length > maxLength) {
  743. return { 'maxlength': { 'requiredLength': maxLength, 'actualLength': length } };
  744. }
  745. return null;
  746. };
  747. }
  748. /**
  749. * Validator that requires the control's value to match a regex pattern.
  750. * See `Validators.pattern` for additional information.
  751. */
  752. function patternValidator(pattern) {
  753. if (!pattern)
  754. return nullValidator;
  755. let regex;
  756. let regexStr;
  757. if (typeof pattern === 'string') {
  758. regexStr = '';
  759. if (pattern.charAt(0) !== '^')
  760. regexStr += '^';
  761. regexStr += pattern;
  762. if (pattern.charAt(pattern.length - 1) !== '$')
  763. regexStr += '$';
  764. regex = new RegExp(regexStr);
  765. }
  766. else {
  767. regexStr = pattern.toString();
  768. regex = pattern;
  769. }
  770. return (control) => {
  771. if (isEmptyInputValue(control.value)) {
  772. return null; // don't validate empty values to allow optional controls
  773. }
  774. const value = control.value;
  775. return regex.test(value)
  776. ? null
  777. : { 'pattern': { 'requiredPattern': regexStr, 'actualValue': value } };
  778. };
  779. }
  780. /**
  781. * Function that has `ValidatorFn` shape, but performs no operation.
  782. */
  783. function nullValidator(control) {
  784. return null;
  785. }
  786. function isPresent(o) {
  787. return o != null;
  788. }
  789. function toObservable(value) {
  790. const obs = _isPromise(value) ? from(value) : value;
  791. if ((typeof ngDevMode === 'undefined' || ngDevMode) && !_isSubscribable(obs)) {
  792. let errorMessage = `Expected async validator to return Promise or Observable.`;
  793. // A synchronous validator will return object or null.
  794. if (typeof value === 'object') {
  795. errorMessage +=
  796. ' Are you using a synchronous validator where an async validator is expected?';
  797. }
  798. throw new _RuntimeError(-1101 /* RuntimeErrorCode.WRONG_VALIDATOR_RETURN_TYPE */, errorMessage);
  799. }
  800. return obs;
  801. }
  802. function mergeErrors(arrayOfErrors) {
  803. let res = {};
  804. arrayOfErrors.forEach((errors) => {
  805. res = errors != null ? { ...res, ...errors } : res;
  806. });
  807. return Object.keys(res).length === 0 ? null : res;
  808. }
  809. function executeValidators(control, validators) {
  810. return validators.map((validator) => validator(control));
  811. }
  812. function isValidatorFn(validator) {
  813. return !validator.validate;
  814. }
  815. /**
  816. * Given the list of validators that may contain both functions as well as classes, return the list
  817. * of validator functions (convert validator classes into validator functions). This is needed to
  818. * have consistent structure in validators list before composing them.
  819. *
  820. * @param validators The set of validators that may contain validators both in plain function form
  821. * as well as represented as a validator class.
  822. */
  823. function normalizeValidators(validators) {
  824. return validators.map((validator) => {
  825. return isValidatorFn(validator)
  826. ? validator
  827. : ((c) => validator.validate(c));
  828. });
  829. }
  830. /**
  831. * Merges synchronous validators into a single validator function.
  832. * See `Validators.compose` for additional information.
  833. */
  834. function compose(validators) {
  835. if (!validators)
  836. return null;
  837. const presentValidators = validators.filter(isPresent);
  838. if (presentValidators.length == 0)
  839. return null;
  840. return function (control) {
  841. return mergeErrors(executeValidators(control, presentValidators));
  842. };
  843. }
  844. /**
  845. * Accepts a list of validators of different possible shapes (`Validator` and `ValidatorFn`),
  846. * normalizes the list (converts everything to `ValidatorFn`) and merges them into a single
  847. * validator function.
  848. */
  849. function composeValidators(validators) {
  850. return validators != null ? compose(normalizeValidators(validators)) : null;
  851. }
  852. /**
  853. * Merges asynchronous validators into a single validator function.
  854. * See `Validators.composeAsync` for additional information.
  855. */
  856. function composeAsync(validators) {
  857. if (!validators)
  858. return null;
  859. const presentValidators = validators.filter(isPresent);
  860. if (presentValidators.length == 0)
  861. return null;
  862. return function (control) {
  863. const observables = executeValidators(control, presentValidators).map(toObservable);
  864. return forkJoin(observables).pipe(map(mergeErrors));
  865. };
  866. }
  867. /**
  868. * Accepts a list of async validators of different possible shapes (`AsyncValidator` and
  869. * `AsyncValidatorFn`), normalizes the list (converts everything to `AsyncValidatorFn`) and merges
  870. * them into a single validator function.
  871. */
  872. function composeAsyncValidators(validators) {
  873. return validators != null
  874. ? composeAsync(normalizeValidators(validators))
  875. : null;
  876. }
  877. /**
  878. * Merges raw control validators with a given directive validator and returns the combined list of
  879. * validators as an array.
  880. */
  881. function mergeValidators(controlValidators, dirValidator) {
  882. if (controlValidators === null)
  883. return [dirValidator];
  884. return Array.isArray(controlValidators)
  885. ? [...controlValidators, dirValidator]
  886. : [controlValidators, dirValidator];
  887. }
  888. /**
  889. * Retrieves the list of raw synchronous validators attached to a given control.
  890. */
  891. function getControlValidators(control) {
  892. return control._rawValidators;
  893. }
  894. /**
  895. * Retrieves the list of raw asynchronous validators attached to a given control.
  896. */
  897. function getControlAsyncValidators(control) {
  898. return control._rawAsyncValidators;
  899. }
  900. /**
  901. * Accepts a singleton validator, an array, or null, and returns an array type with the provided
  902. * validators.
  903. *
  904. * @param validators A validator, validators, or null.
  905. * @returns A validators array.
  906. */
  907. function makeValidatorsArray(validators) {
  908. if (!validators)
  909. return [];
  910. return Array.isArray(validators) ? validators : [validators];
  911. }
  912. /**
  913. * Determines whether a validator or validators array has a given validator.
  914. *
  915. * @param validators The validator or validators to compare against.
  916. * @param validator The validator to check.
  917. * @returns Whether the validator is present.
  918. */
  919. function hasValidator(validators, validator) {
  920. return Array.isArray(validators) ? validators.includes(validator) : validators === validator;
  921. }
  922. /**
  923. * Combines two arrays of validators into one. If duplicates are provided, only one will be added.
  924. *
  925. * @param validators The new validators.
  926. * @param currentValidators The base array of current validators.
  927. * @returns An array of validators.
  928. */
  929. function addValidators(validators, currentValidators) {
  930. const current = makeValidatorsArray(currentValidators);
  931. const validatorsToAdd = makeValidatorsArray(validators);
  932. validatorsToAdd.forEach((v) => {
  933. // Note: if there are duplicate entries in the new validators array,
  934. // only the first one would be added to the current list of validators.
  935. // Duplicate ones would be ignored since `hasValidator` would detect
  936. // the presence of a validator function and we update the current list in place.
  937. if (!hasValidator(current, v)) {
  938. current.push(v);
  939. }
  940. });
  941. return current;
  942. }
  943. function removeValidators(validators, currentValidators) {
  944. return makeValidatorsArray(currentValidators).filter((v) => !hasValidator(validators, v));
  945. }
  946. /**
  947. * @description
  948. * Base class for control directives.
  949. *
  950. * This class is only used internally in the `ReactiveFormsModule` and the `FormsModule`.
  951. *
  952. * @publicApi
  953. */
  954. class AbstractControlDirective {
  955. /**
  956. * @description
  957. * Reports the value of the control if it is present, otherwise null.
  958. */
  959. get value() {
  960. return this.control ? this.control.value : null;
  961. }
  962. /**
  963. * @description
  964. * Reports whether the control is valid. A control is considered valid if no
  965. * validation errors exist with the current value.
  966. * If the control is not present, null is returned.
  967. */
  968. get valid() {
  969. return this.control ? this.control.valid : null;
  970. }
  971. /**
  972. * @description
  973. * Reports whether the control is invalid, meaning that an error exists in the input value.
  974. * If the control is not present, null is returned.
  975. */
  976. get invalid() {
  977. return this.control ? this.control.invalid : null;
  978. }
  979. /**
  980. * @description
  981. * Reports whether a control is pending, meaning that async validation is occurring and
  982. * errors are not yet available for the input value. If the control is not present, null is
  983. * returned.
  984. */
  985. get pending() {
  986. return this.control ? this.control.pending : null;
  987. }
  988. /**
  989. * @description
  990. * Reports whether the control is disabled, meaning that the control is disabled
  991. * in the UI and is exempt from validation checks and excluded from aggregate
  992. * values of ancestor controls. If the control is not present, null is returned.
  993. */
  994. get disabled() {
  995. return this.control ? this.control.disabled : null;
  996. }
  997. /**
  998. * @description
  999. * Reports whether the control is enabled, meaning that the control is included in ancestor
  1000. * calculations of validity or value. If the control is not present, null is returned.
  1001. */
  1002. get enabled() {
  1003. return this.control ? this.control.enabled : null;
  1004. }
  1005. /**
  1006. * @description
  1007. * Reports the control's validation errors. If the control is not present, null is returned.
  1008. */
  1009. get errors() {
  1010. return this.control ? this.control.errors : null;
  1011. }
  1012. /**
  1013. * @description
  1014. * Reports whether the control is pristine, meaning that the user has not yet changed
  1015. * the value in the UI. If the control is not present, null is returned.
  1016. */
  1017. get pristine() {
  1018. return this.control ? this.control.pristine : null;
  1019. }
  1020. /**
  1021. * @description
  1022. * Reports whether the control is dirty, meaning that the user has changed
  1023. * the value in the UI. If the control is not present, null is returned.
  1024. */
  1025. get dirty() {
  1026. return this.control ? this.control.dirty : null;
  1027. }
  1028. /**
  1029. * @description
  1030. * Reports whether the control is touched, meaning that the user has triggered
  1031. * a `blur` event on it. If the control is not present, null is returned.
  1032. */
  1033. get touched() {
  1034. return this.control ? this.control.touched : null;
  1035. }
  1036. /**
  1037. * @description
  1038. * Reports the validation status of the control. Possible values include:
  1039. * 'VALID', 'INVALID', 'DISABLED', and 'PENDING'.
  1040. * If the control is not present, null is returned.
  1041. */
  1042. get status() {
  1043. return this.control ? this.control.status : null;
  1044. }
  1045. /**
  1046. * @description
  1047. * Reports whether the control is untouched, meaning that the user has not yet triggered
  1048. * a `blur` event on it. If the control is not present, null is returned.
  1049. */
  1050. get untouched() {
  1051. return this.control ? this.control.untouched : null;
  1052. }
  1053. /**
  1054. * @description
  1055. * Returns a multicasting observable that emits a validation status whenever it is
  1056. * calculated for the control. If the control is not present, null is returned.
  1057. */
  1058. get statusChanges() {
  1059. return this.control ? this.control.statusChanges : null;
  1060. }
  1061. /**
  1062. * @description
  1063. * Returns a multicasting observable of value changes for the control that emits every time the
  1064. * value of the control changes in the UI or programmatically.
  1065. * If the control is not present, null is returned.
  1066. */
  1067. get valueChanges() {
  1068. return this.control ? this.control.valueChanges : null;
  1069. }
  1070. /**
  1071. * @description
  1072. * Returns an array that represents the path from the top-level form to this control.
  1073. * Each index is the string name of the control on that level.
  1074. */
  1075. get path() {
  1076. return null;
  1077. }
  1078. /**
  1079. * Contains the result of merging synchronous validators into a single validator function
  1080. * (combined using `Validators.compose`).
  1081. */
  1082. _composedValidatorFn;
  1083. /**
  1084. * Contains the result of merging asynchronous validators into a single validator function
  1085. * (combined using `Validators.composeAsync`).
  1086. */
  1087. _composedAsyncValidatorFn;
  1088. /**
  1089. * Set of synchronous validators as they were provided while calling `setValidators` function.
  1090. * @internal
  1091. */
  1092. _rawValidators = [];
  1093. /**
  1094. * Set of asynchronous validators as they were provided while calling `setAsyncValidators`
  1095. * function.
  1096. * @internal
  1097. */
  1098. _rawAsyncValidators = [];
  1099. /**
  1100. * Sets synchronous validators for this directive.
  1101. * @internal
  1102. */
  1103. _setValidators(validators) {
  1104. this._rawValidators = validators || [];
  1105. this._composedValidatorFn = composeValidators(this._rawValidators);
  1106. }
  1107. /**
  1108. * Sets asynchronous validators for this directive.
  1109. * @internal
  1110. */
  1111. _setAsyncValidators(validators) {
  1112. this._rawAsyncValidators = validators || [];
  1113. this._composedAsyncValidatorFn = composeAsyncValidators(this._rawAsyncValidators);
  1114. }
  1115. /**
  1116. * @description
  1117. * Synchronous validator function composed of all the synchronous validators registered with this
  1118. * directive.
  1119. */
  1120. get validator() {
  1121. return this._composedValidatorFn || null;
  1122. }
  1123. /**
  1124. * @description
  1125. * Asynchronous validator function composed of all the asynchronous validators registered with
  1126. * this directive.
  1127. */
  1128. get asyncValidator() {
  1129. return this._composedAsyncValidatorFn || null;
  1130. }
  1131. /*
  1132. * The set of callbacks to be invoked when directive instance is being destroyed.
  1133. */
  1134. _onDestroyCallbacks = [];
  1135. /**
  1136. * Internal function to register callbacks that should be invoked
  1137. * when directive instance is being destroyed.
  1138. * @internal
  1139. */
  1140. _registerOnDestroy(fn) {
  1141. this._onDestroyCallbacks.push(fn);
  1142. }
  1143. /**
  1144. * Internal function to invoke all registered "on destroy" callbacks.
  1145. * Note: calling this function also clears the list of callbacks.
  1146. * @internal
  1147. */
  1148. _invokeOnDestroyCallbacks() {
  1149. this._onDestroyCallbacks.forEach((fn) => fn());
  1150. this._onDestroyCallbacks = [];
  1151. }
  1152. /**
  1153. * @description
  1154. * Resets the control with the provided value if the control is present.
  1155. */
  1156. reset(value = undefined) {
  1157. if (this.control)
  1158. this.control.reset(value);
  1159. }
  1160. /**
  1161. * @description
  1162. * Reports whether the control with the given path has the error specified.
  1163. *
  1164. * @param errorCode The code of the error to check
  1165. * @param path A list of control names that designates how to move from the current control
  1166. * to the control that should be queried for errors.
  1167. *
  1168. * @usageNotes
  1169. * For example, for the following `FormGroup`:
  1170. *
  1171. * ```ts
  1172. * form = new FormGroup({
  1173. * address: new FormGroup({ street: new FormControl() })
  1174. * });
  1175. * ```
  1176. *
  1177. * The path to the 'street' control from the root form would be 'address' -> 'street'.
  1178. *
  1179. * It can be provided to this method in one of two formats:
  1180. *
  1181. * 1. An array of string control names, e.g. `['address', 'street']`
  1182. * 1. A period-delimited list of control names in one string, e.g. `'address.street'`
  1183. *
  1184. * If no path is given, this method checks for the error on the current control.
  1185. *
  1186. * @returns whether the given error is present in the control at the given path.
  1187. *
  1188. * If the control is not present, false is returned.
  1189. */
  1190. hasError(errorCode, path) {
  1191. return this.control ? this.control.hasError(errorCode, path) : false;
  1192. }
  1193. /**
  1194. * @description
  1195. * Reports error data for the control with the given path.
  1196. *
  1197. * @param errorCode The code of the error to check
  1198. * @param path A list of control names that designates how to move from the current control
  1199. * to the control that should be queried for errors.
  1200. *
  1201. * @usageNotes
  1202. * For example, for the following `FormGroup`:
  1203. *
  1204. * ```ts
  1205. * form = new FormGroup({
  1206. * address: new FormGroup({ street: new FormControl() })
  1207. * });
  1208. * ```
  1209. *
  1210. * The path to the 'street' control from the root form would be 'address' -> 'street'.
  1211. *
  1212. * It can be provided to this method in one of two formats:
  1213. *
  1214. * 1. An array of string control names, e.g. `['address', 'street']`
  1215. * 1. A period-delimited list of control names in one string, e.g. `'address.street'`
  1216. *
  1217. * @returns error data for that particular error. If the control or error is not present,
  1218. * null is returned.
  1219. */
  1220. getError(errorCode, path) {
  1221. return this.control ? this.control.getError(errorCode, path) : null;
  1222. }
  1223. }
  1224. /**
  1225. * @description
  1226. * A base class for directives that contain multiple registered instances of `NgControl`.
  1227. * Only used by the forms module.
  1228. *
  1229. * @publicApi
  1230. */
  1231. class ControlContainer extends AbstractControlDirective {
  1232. /**
  1233. * @description
  1234. * The name for the control
  1235. */
  1236. name;
  1237. /**
  1238. * @description
  1239. * The top-level form directive for the control.
  1240. */
  1241. get formDirective() {
  1242. return null;
  1243. }
  1244. /**
  1245. * @description
  1246. * The path to this group.
  1247. */
  1248. get path() {
  1249. return null;
  1250. }
  1251. }
  1252. /**
  1253. * @description
  1254. * A base class that all `FormControl`-based directives extend. It binds a `FormControl`
  1255. * object to a DOM element.
  1256. *
  1257. * @publicApi
  1258. */
  1259. class NgControl extends AbstractControlDirective {
  1260. /**
  1261. * @description
  1262. * The parent form for the control.
  1263. *
  1264. * @internal
  1265. */
  1266. _parent = null;
  1267. /**
  1268. * @description
  1269. * The name for the control
  1270. */
  1271. name = null;
  1272. /**
  1273. * @description
  1274. * The value accessor for the control
  1275. */
  1276. valueAccessor = null;
  1277. }
  1278. // DO NOT REFACTOR!
  1279. // Each status is represented by a separate function to make sure that
  1280. // advanced Closure Compiler optimizations related to property renaming
  1281. // can work correctly.
  1282. class AbstractControlStatus {
  1283. _cd;
  1284. constructor(cd) {
  1285. this._cd = cd;
  1286. }
  1287. get isTouched() {
  1288. // track the touched signal
  1289. this._cd?.control?._touched?.();
  1290. return !!this._cd?.control?.touched;
  1291. }
  1292. get isUntouched() {
  1293. return !!this._cd?.control?.untouched;
  1294. }
  1295. get isPristine() {
  1296. // track the pristine signal
  1297. this._cd?.control?._pristine?.();
  1298. return !!this._cd?.control?.pristine;
  1299. }
  1300. get isDirty() {
  1301. // pristine signal already tracked above
  1302. return !!this._cd?.control?.dirty;
  1303. }
  1304. get isValid() {
  1305. // track the status signal
  1306. this._cd?.control?._status?.();
  1307. return !!this._cd?.control?.valid;
  1308. }
  1309. get isInvalid() {
  1310. // status signal already tracked above
  1311. return !!this._cd?.control?.invalid;
  1312. }
  1313. get isPending() {
  1314. // status signal already tracked above
  1315. return !!this._cd?.control?.pending;
  1316. }
  1317. get isSubmitted() {
  1318. // track the submitted signal
  1319. this._cd?._submitted?.();
  1320. // We check for the `submitted` field from `NgForm` and `FormGroupDirective` classes, but
  1321. // we avoid instanceof checks to prevent non-tree-shakable references to those types.
  1322. return !!this._cd?.submitted;
  1323. }
  1324. }
  1325. const ngControlStatusHost = {
  1326. '[class.ng-untouched]': 'isUntouched',
  1327. '[class.ng-touched]': 'isTouched',
  1328. '[class.ng-pristine]': 'isPristine',
  1329. '[class.ng-dirty]': 'isDirty',
  1330. '[class.ng-valid]': 'isValid',
  1331. '[class.ng-invalid]': 'isInvalid',
  1332. '[class.ng-pending]': 'isPending',
  1333. };
  1334. const ngGroupStatusHost = {
  1335. ...ngControlStatusHost,
  1336. '[class.ng-submitted]': 'isSubmitted',
  1337. };
  1338. /**
  1339. * @description
  1340. * Directive automatically applied to Angular form controls that sets CSS classes
  1341. * based on control status.
  1342. *
  1343. * @usageNotes
  1344. *
  1345. * ### CSS classes applied
  1346. *
  1347. * The following classes are applied as the properties become true:
  1348. *
  1349. * * ng-valid
  1350. * * ng-invalid
  1351. * * ng-pending
  1352. * * ng-pristine
  1353. * * ng-dirty
  1354. * * ng-untouched
  1355. * * ng-touched
  1356. *
  1357. * @ngModule ReactiveFormsModule
  1358. * @ngModule FormsModule
  1359. * @publicApi
  1360. */
  1361. class NgControlStatus extends AbstractControlStatus {
  1362. constructor(cd) {
  1363. super(cd);
  1364. }
  1365. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgControlStatus, deps: [{ token: NgControl, self: true }], target: i0.ɵɵFactoryTarget.Directive });
  1366. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: NgControlStatus, isStandalone: false, selector: "[formControlName],[ngModel],[formControl]", host: { properties: { "class.ng-untouched": "isUntouched", "class.ng-touched": "isTouched", "class.ng-pristine": "isPristine", "class.ng-dirty": "isDirty", "class.ng-valid": "isValid", "class.ng-invalid": "isInvalid", "class.ng-pending": "isPending" } }, usesInheritance: true, ngImport: i0 });
  1367. }
  1368. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgControlStatus, decorators: [{
  1369. type: Directive,
  1370. args: [{
  1371. selector: '[formControlName],[ngModel],[formControl]',
  1372. host: ngControlStatusHost,
  1373. standalone: false,
  1374. }]
  1375. }], ctorParameters: () => [{ type: NgControl, decorators: [{
  1376. type: Self
  1377. }] }] });
  1378. /**
  1379. * @description
  1380. * Directive automatically applied to Angular form groups that sets CSS classes
  1381. * based on control status (valid/invalid/dirty/etc). On groups, this includes the additional
  1382. * class ng-submitted.
  1383. *
  1384. * @see {@link NgControlStatus}
  1385. *
  1386. * @ngModule ReactiveFormsModule
  1387. * @ngModule FormsModule
  1388. * @publicApi
  1389. */
  1390. class NgControlStatusGroup extends AbstractControlStatus {
  1391. constructor(cd) {
  1392. super(cd);
  1393. }
  1394. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgControlStatusGroup, deps: [{ token: ControlContainer, optional: true, self: true }], target: i0.ɵɵFactoryTarget.Directive });
  1395. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: NgControlStatusGroup, isStandalone: false, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]", host: { properties: { "class.ng-untouched": "isUntouched", "class.ng-touched": "isTouched", "class.ng-pristine": "isPristine", "class.ng-dirty": "isDirty", "class.ng-valid": "isValid", "class.ng-invalid": "isInvalid", "class.ng-pending": "isPending", "class.ng-submitted": "isSubmitted" } }, usesInheritance: true, ngImport: i0 });
  1396. }
  1397. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgControlStatusGroup, decorators: [{
  1398. type: Directive,
  1399. args: [{
  1400. selector: '[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]',
  1401. host: ngGroupStatusHost,
  1402. standalone: false,
  1403. }]
  1404. }], ctorParameters: () => [{ type: ControlContainer, decorators: [{
  1405. type: Optional
  1406. }, {
  1407. type: Self
  1408. }] }] });
  1409. const formControlNameExample = `
  1410. <div [formGroup]="myGroup">
  1411. <input formControlName="firstName">
  1412. </div>
  1413. In your class:
  1414. this.myGroup = new FormGroup({
  1415. firstName: new FormControl()
  1416. });`;
  1417. const formGroupNameExample = `
  1418. <div [formGroup]="myGroup">
  1419. <div formGroupName="person">
  1420. <input formControlName="firstName">
  1421. </div>
  1422. </div>
  1423. In your class:
  1424. this.myGroup = new FormGroup({
  1425. person: new FormGroup({ firstName: new FormControl() })
  1426. });`;
  1427. const formArrayNameExample = `
  1428. <div [formGroup]="myGroup">
  1429. <div formArrayName="cities">
  1430. <div *ngFor="let city of cityArray.controls; index as i">
  1431. <input [formControlName]="i">
  1432. </div>
  1433. </div>
  1434. </div>
  1435. In your class:
  1436. this.cityArray = new FormArray([new FormControl('SF')]);
  1437. this.myGroup = new FormGroup({
  1438. cities: this.cityArray
  1439. });`;
  1440. const ngModelGroupExample = `
  1441. <form>
  1442. <div ngModelGroup="person">
  1443. <input [(ngModel)]="person.name" name="firstName">
  1444. </div>
  1445. </form>`;
  1446. const ngModelWithFormGroupExample = `
  1447. <div [formGroup]="myGroup">
  1448. <input formControlName="firstName">
  1449. <input [(ngModel)]="showMoreControls" [ngModelOptions]="{standalone: true}">
  1450. </div>
  1451. `;
  1452. function controlParentException(nameOrIndex) {
  1453. return new _RuntimeError(1050 /* RuntimeErrorCode.FORM_CONTROL_NAME_MISSING_PARENT */, `formControlName must be used with a parent formGroup directive. You'll want to add a formGroup
  1454. directive and pass it an existing FormGroup instance (you can create one in your class).
  1455. ${describeFormControl(nameOrIndex)}
  1456. Example:
  1457. ${formControlNameExample}`);
  1458. }
  1459. function describeFormControl(nameOrIndex) {
  1460. if (nameOrIndex == null || nameOrIndex === '') {
  1461. return '';
  1462. }
  1463. const valueType = typeof nameOrIndex === 'string' ? 'name' : 'index';
  1464. return `Affected Form Control ${valueType}: "${nameOrIndex}"`;
  1465. }
  1466. function ngModelGroupException() {
  1467. return new _RuntimeError(1051 /* RuntimeErrorCode.FORM_CONTROL_NAME_INSIDE_MODEL_GROUP */, `formControlName cannot be used with an ngModelGroup parent. It is only compatible with parents
  1468. that also have a "form" prefix: formGroupName, formArrayName, or formGroup.
  1469. Option 1: Update the parent to be formGroupName (reactive form strategy)
  1470. ${formGroupNameExample}
  1471. Option 2: Use ngModel instead of formControlName (template-driven strategy)
  1472. ${ngModelGroupExample}`);
  1473. }
  1474. function missingFormException() {
  1475. return new _RuntimeError(1052 /* RuntimeErrorCode.FORM_GROUP_MISSING_INSTANCE */, `formGroup expects a FormGroup instance. Please pass one in.
  1476. Example:
  1477. ${formControlNameExample}`);
  1478. }
  1479. function groupParentException() {
  1480. return new _RuntimeError(1053 /* RuntimeErrorCode.FORM_GROUP_NAME_MISSING_PARENT */, `formGroupName must be used with a parent formGroup directive. You'll want to add a formGroup
  1481. directive and pass it an existing FormGroup instance (you can create one in your class).
  1482. Example:
  1483. ${formGroupNameExample}`);
  1484. }
  1485. function arrayParentException() {
  1486. return new _RuntimeError(1054 /* RuntimeErrorCode.FORM_ARRAY_NAME_MISSING_PARENT */, `formArrayName must be used with a parent formGroup directive. You'll want to add a formGroup
  1487. directive and pass it an existing FormGroup instance (you can create one in your class).
  1488. Example:
  1489. ${formArrayNameExample}`);
  1490. }
  1491. const disabledAttrWarning = `
  1492. It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true
  1493. when you set up this control in your component class, the disabled attribute will actually be set in the DOM for
  1494. you. We recommend using this approach to avoid 'changed after checked' errors.
  1495. Example:
  1496. // Specify the \`disabled\` property at control creation time:
  1497. form = new FormGroup({
  1498. first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
  1499. last: new FormControl('Drew', Validators.required)
  1500. });
  1501. // Controls can also be enabled/disabled after creation:
  1502. form.get('first')?.enable();
  1503. form.get('last')?.disable();
  1504. `;
  1505. const asyncValidatorsDroppedWithOptsWarning = `
  1506. It looks like you're constructing using a FormControl with both an options argument and an
  1507. async validators argument. Mixing these arguments will cause your async validators to be dropped.
  1508. You should either put all your validators in the options object, or in separate validators
  1509. arguments. For example:
  1510. // Using validators arguments
  1511. fc = new FormControl(42, Validators.required, myAsyncValidator);
  1512. // Using AbstractControlOptions
  1513. fc = new FormControl(42, {validators: Validators.required, asyncValidators: myAV});
  1514. // Do NOT mix them: async validators will be dropped!
  1515. fc = new FormControl(42, {validators: Validators.required}, /* Oops! */ myAsyncValidator);
  1516. `;
  1517. function ngModelWarning(directiveName) {
  1518. return `
  1519. It looks like you're using ngModel on the same form field as ${directiveName}.
  1520. Support for using the ngModel input property and ngModelChange event with
  1521. reactive form directives has been deprecated in Angular v6 and will be removed
  1522. in a future version of Angular.
  1523. For more information on this, see our API docs here:
  1524. https://angular.io/api/forms/${directiveName === 'formControl' ? 'FormControlDirective' : 'FormControlName'}#use-with-ngmodel
  1525. `;
  1526. }
  1527. function describeKey(isFormGroup, key) {
  1528. return isFormGroup ? `with name: '${key}'` : `at index: ${key}`;
  1529. }
  1530. function noControlsError(isFormGroup) {
  1531. return `
  1532. There are no form controls registered with this ${isFormGroup ? 'group' : 'array'} yet. If you're using ngModel,
  1533. you may want to check next tick (e.g. use setTimeout).
  1534. `;
  1535. }
  1536. function missingControlError(isFormGroup, key) {
  1537. return `Cannot find form control ${describeKey(isFormGroup, key)}`;
  1538. }
  1539. function missingControlValueError(isFormGroup, key) {
  1540. return `Must supply a value for form control ${describeKey(isFormGroup, key)}`;
  1541. }
  1542. /**
  1543. * Reports that a control is valid, meaning that no errors exist in the input value.
  1544. *
  1545. * @see {@link status}
  1546. */
  1547. const VALID = 'VALID';
  1548. /**
  1549. * Reports that a control is invalid, meaning that an error exists in the input value.
  1550. *
  1551. * @see {@link status}
  1552. */
  1553. const INVALID = 'INVALID';
  1554. /**
  1555. * Reports that a control is pending, meaning that async validation is occurring and
  1556. * errors are not yet available for the input value.
  1557. *
  1558. * @see {@link markAsPending}
  1559. * @see {@link status}
  1560. */
  1561. const PENDING = 'PENDING';
  1562. /**
  1563. * Reports that a control is disabled, meaning that the control is exempt from ancestor
  1564. * calculations of validity or value.
  1565. *
  1566. * @see {@link markAsDisabled}
  1567. * @see {@link status}
  1568. */
  1569. const DISABLED = 'DISABLED';
  1570. /**
  1571. * Base class for every event sent by `AbstractControl.events()`
  1572. *
  1573. * @publicApi
  1574. */
  1575. class ControlEvent {
  1576. }
  1577. /**
  1578. * Event fired when the value of a control changes.
  1579. *
  1580. * @publicApi
  1581. */
  1582. class ValueChangeEvent extends ControlEvent {
  1583. value;
  1584. source;
  1585. constructor(value, source) {
  1586. super();
  1587. this.value = value;
  1588. this.source = source;
  1589. }
  1590. }
  1591. /**
  1592. * Event fired when the control's pristine state changes (pristine <=> dirty).
  1593. *
  1594. * @publicApi */
  1595. class PristineChangeEvent extends ControlEvent {
  1596. pristine;
  1597. source;
  1598. constructor(pristine, source) {
  1599. super();
  1600. this.pristine = pristine;
  1601. this.source = source;
  1602. }
  1603. }
  1604. /**
  1605. * Event fired when the control's touched status changes (touched <=> untouched).
  1606. *
  1607. * @publicApi
  1608. */
  1609. class TouchedChangeEvent extends ControlEvent {
  1610. touched;
  1611. source;
  1612. constructor(touched, source) {
  1613. super();
  1614. this.touched = touched;
  1615. this.source = source;
  1616. }
  1617. }
  1618. /**
  1619. * Event fired when the control's status changes.
  1620. *
  1621. * @publicApi
  1622. */
  1623. class StatusChangeEvent extends ControlEvent {
  1624. status;
  1625. source;
  1626. constructor(status, source) {
  1627. super();
  1628. this.status = status;
  1629. this.source = source;
  1630. }
  1631. }
  1632. /**
  1633. * Event fired when a form is submitted
  1634. *
  1635. * @publicApi
  1636. */
  1637. class FormSubmittedEvent extends ControlEvent {
  1638. source;
  1639. constructor(source) {
  1640. super();
  1641. this.source = source;
  1642. }
  1643. }
  1644. /**
  1645. * Event fired when a form is reset.
  1646. *
  1647. * @publicApi
  1648. */
  1649. class FormResetEvent extends ControlEvent {
  1650. source;
  1651. constructor(source) {
  1652. super();
  1653. this.source = source;
  1654. }
  1655. }
  1656. /**
  1657. * Gets validators from either an options object or given validators.
  1658. */
  1659. function pickValidators(validatorOrOpts) {
  1660. return (isOptionsObj(validatorOrOpts) ? validatorOrOpts.validators : validatorOrOpts) || null;
  1661. }
  1662. /**
  1663. * Creates validator function by combining provided validators.
  1664. */
  1665. function coerceToValidator(validator) {
  1666. return Array.isArray(validator) ? composeValidators(validator) : validator || null;
  1667. }
  1668. /**
  1669. * Gets async validators from either an options object or given validators.
  1670. */
  1671. function pickAsyncValidators(asyncValidator, validatorOrOpts) {
  1672. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  1673. if (isOptionsObj(validatorOrOpts) && asyncValidator) {
  1674. console.warn(asyncValidatorsDroppedWithOptsWarning);
  1675. }
  1676. }
  1677. return (isOptionsObj(validatorOrOpts) ? validatorOrOpts.asyncValidators : asyncValidator) || null;
  1678. }
  1679. /**
  1680. * Creates async validator function by combining provided async validators.
  1681. */
  1682. function coerceToAsyncValidator(asyncValidator) {
  1683. return Array.isArray(asyncValidator)
  1684. ? composeAsyncValidators(asyncValidator)
  1685. : asyncValidator || null;
  1686. }
  1687. function isOptionsObj(validatorOrOpts) {
  1688. return (validatorOrOpts != null &&
  1689. !Array.isArray(validatorOrOpts) &&
  1690. typeof validatorOrOpts === 'object');
  1691. }
  1692. function assertControlPresent(parent, isGroup, key) {
  1693. const controls = parent.controls;
  1694. const collection = isGroup ? Object.keys(controls) : controls;
  1695. if (!collection.length) {
  1696. throw new _RuntimeError(1000 /* RuntimeErrorCode.NO_CONTROLS */, typeof ngDevMode === 'undefined' || ngDevMode ? noControlsError(isGroup) : '');
  1697. }
  1698. if (!controls[key]) {
  1699. throw new _RuntimeError(1001 /* RuntimeErrorCode.MISSING_CONTROL */, typeof ngDevMode === 'undefined' || ngDevMode ? missingControlError(isGroup, key) : '');
  1700. }
  1701. }
  1702. function assertAllValuesPresent(control, isGroup, value) {
  1703. control._forEachChild((_, key) => {
  1704. if (value[key] === undefined) {
  1705. throw new _RuntimeError(1002 /* RuntimeErrorCode.MISSING_CONTROL_VALUE */, typeof ngDevMode === 'undefined' || ngDevMode ? missingControlValueError(isGroup, key) : '');
  1706. }
  1707. });
  1708. }
  1709. /**
  1710. * This is the base class for `FormControl`, `FormGroup`, and `FormArray`.
  1711. *
  1712. * It provides some of the shared behavior that all controls and groups of controls have, like
  1713. * running validators, calculating status, and resetting state. It also defines the properties
  1714. * that are shared between all sub-classes, like `value`, `valid`, and `dirty`. It shouldn't be
  1715. * instantiated directly.
  1716. *
  1717. * The first type parameter TValue represents the value type of the control (`control.value`).
  1718. * The optional type parameter TRawValue represents the raw value type (`control.getRawValue()`).
  1719. *
  1720. * @see [Forms Guide](guide/forms)
  1721. * @see [Reactive Forms Guide](guide/forms/reactive-forms)
  1722. * @see [Dynamic Forms Guide](guide/forms/dynamic-forms)
  1723. *
  1724. * @publicApi
  1725. */
  1726. class AbstractControl {
  1727. /** @internal */
  1728. _pendingDirty = false;
  1729. /**
  1730. * Indicates that a control has its own pending asynchronous validation in progress.
  1731. * It also stores if the control should emit events when the validation status changes.
  1732. *
  1733. * @internal
  1734. */
  1735. _hasOwnPendingAsyncValidator = null;
  1736. /** @internal */
  1737. _pendingTouched = false;
  1738. /** @internal */
  1739. _onCollectionChange = () => { };
  1740. /** @internal */
  1741. _updateOn;
  1742. _parent = null;
  1743. _asyncValidationSubscription;
  1744. /**
  1745. * Contains the result of merging synchronous validators into a single validator function
  1746. * (combined using `Validators.compose`).
  1747. *
  1748. * @internal
  1749. */
  1750. _composedValidatorFn;
  1751. /**
  1752. * Contains the result of merging asynchronous validators into a single validator function
  1753. * (combined using `Validators.composeAsync`).
  1754. *
  1755. * @internal
  1756. */
  1757. _composedAsyncValidatorFn;
  1758. /**
  1759. * Synchronous validators as they were provided:
  1760. * - in `AbstractControl` constructor
  1761. * - as an argument while calling `setValidators` function
  1762. * - while calling the setter on the `validator` field (e.g. `control.validator = validatorFn`)
  1763. *
  1764. * @internal
  1765. */
  1766. _rawValidators;
  1767. /**
  1768. * Asynchronous validators as they were provided:
  1769. * - in `AbstractControl` constructor
  1770. * - as an argument while calling `setAsyncValidators` function
  1771. * - while calling the setter on the `asyncValidator` field (e.g. `control.asyncValidator =
  1772. * asyncValidatorFn`)
  1773. *
  1774. * @internal
  1775. */
  1776. _rawAsyncValidators;
  1777. /**
  1778. * The current value of the control.
  1779. *
  1780. * * For a `FormControl`, the current value.
  1781. * * For an enabled `FormGroup`, the values of enabled controls as an object
  1782. * with a key-value pair for each member of the group.
  1783. * * For a disabled `FormGroup`, the values of all controls as an object
  1784. * with a key-value pair for each member of the group.
  1785. * * For a `FormArray`, the values of enabled controls as an array.
  1786. *
  1787. */
  1788. value;
  1789. /**
  1790. * Initialize the AbstractControl instance.
  1791. *
  1792. * @param validators The function or array of functions that is used to determine the validity of
  1793. * this control synchronously.
  1794. * @param asyncValidators The function or array of functions that is used to determine validity of
  1795. * this control asynchronously.
  1796. */
  1797. constructor(validators, asyncValidators) {
  1798. this._assignValidators(validators);
  1799. this._assignAsyncValidators(asyncValidators);
  1800. }
  1801. /**
  1802. * Returns the function that is used to determine the validity of this control synchronously.
  1803. * If multiple validators have been added, this will be a single composed function.
  1804. * See `Validators.compose()` for additional information.
  1805. */
  1806. get validator() {
  1807. return this._composedValidatorFn;
  1808. }
  1809. set validator(validatorFn) {
  1810. this._rawValidators = this._composedValidatorFn = validatorFn;
  1811. }
  1812. /**
  1813. * Returns the function that is used to determine the validity of this control asynchronously.
  1814. * If multiple validators have been added, this will be a single composed function.
  1815. * See `Validators.compose()` for additional information.
  1816. */
  1817. get asyncValidator() {
  1818. return this._composedAsyncValidatorFn;
  1819. }
  1820. set asyncValidator(asyncValidatorFn) {
  1821. this._rawAsyncValidators = this._composedAsyncValidatorFn = asyncValidatorFn;
  1822. }
  1823. /**
  1824. * The parent control.
  1825. */
  1826. get parent() {
  1827. return this._parent;
  1828. }
  1829. /**
  1830. * The validation status of the control.
  1831. *
  1832. * @see {@link FormControlStatus}
  1833. *
  1834. * These status values are mutually exclusive, so a control cannot be
  1835. * both valid AND invalid or invalid AND disabled.
  1836. */
  1837. get status() {
  1838. return untracked(this.statusReactive);
  1839. }
  1840. set status(v) {
  1841. untracked(() => this.statusReactive.set(v));
  1842. }
  1843. /** @internal */
  1844. _status = computed(() => this.statusReactive());
  1845. statusReactive = signal(undefined);
  1846. /**
  1847. * A control is `valid` when its `status` is `VALID`.
  1848. *
  1849. * @see {@link AbstractControl.status}
  1850. *
  1851. * @returns True if the control has passed all of its validation tests,
  1852. * false otherwise.
  1853. */
  1854. get valid() {
  1855. return this.status === VALID;
  1856. }
  1857. /**
  1858. * A control is `invalid` when its `status` is `INVALID`.
  1859. *
  1860. * @see {@link AbstractControl.status}
  1861. *
  1862. * @returns True if this control has failed one or more of its validation checks,
  1863. * false otherwise.
  1864. */
  1865. get invalid() {
  1866. return this.status === INVALID;
  1867. }
  1868. /**
  1869. * A control is `pending` when its `status` is `PENDING`.
  1870. *
  1871. * @see {@link AbstractControl.status}
  1872. *
  1873. * @returns True if this control is in the process of conducting a validation check,
  1874. * false otherwise.
  1875. */
  1876. get pending() {
  1877. return this.status == PENDING;
  1878. }
  1879. /**
  1880. * A control is `disabled` when its `status` is `DISABLED`.
  1881. *
  1882. * Disabled controls are exempt from validation checks and
  1883. * are not included in the aggregate value of their ancestor
  1884. * controls.
  1885. *
  1886. * @see {@link AbstractControl.status}
  1887. *
  1888. * @returns True if the control is disabled, false otherwise.
  1889. */
  1890. get disabled() {
  1891. return this.status === DISABLED;
  1892. }
  1893. /**
  1894. * A control is `enabled` as long as its `status` is not `DISABLED`.
  1895. *
  1896. * @returns True if the control has any status other than 'DISABLED',
  1897. * false if the status is 'DISABLED'.
  1898. *
  1899. * @see {@link AbstractControl.status}
  1900. *
  1901. */
  1902. get enabled() {
  1903. return this.status !== DISABLED;
  1904. }
  1905. /**
  1906. * An object containing any errors generated by failing validation,
  1907. * or null if there are no errors.
  1908. */
  1909. errors;
  1910. /**
  1911. * A control is `pristine` if the user has not yet changed
  1912. * the value in the UI.
  1913. *
  1914. * @returns True if the user has not yet changed the value in the UI; compare `dirty`.
  1915. * Programmatic changes to a control's value do not mark it dirty.
  1916. */
  1917. get pristine() {
  1918. return untracked(this.pristineReactive);
  1919. }
  1920. set pristine(v) {
  1921. untracked(() => this.pristineReactive.set(v));
  1922. }
  1923. /** @internal */
  1924. _pristine = computed(() => this.pristineReactive());
  1925. pristineReactive = signal(true);
  1926. /**
  1927. * A control is `dirty` if the user has changed the value
  1928. * in the UI.
  1929. *
  1930. * @returns True if the user has changed the value of this control in the UI; compare `pristine`.
  1931. * Programmatic changes to a control's value do not mark it dirty.
  1932. */
  1933. get dirty() {
  1934. return !this.pristine;
  1935. }
  1936. /**
  1937. * True if the control is marked as `touched`.
  1938. *
  1939. * A control is marked `touched` once the user has triggered
  1940. * a `blur` event on it.
  1941. */
  1942. get touched() {
  1943. return untracked(this.touchedReactive);
  1944. }
  1945. set touched(v) {
  1946. untracked(() => this.touchedReactive.set(v));
  1947. }
  1948. /** @internal */
  1949. _touched = computed(() => this.touchedReactive());
  1950. touchedReactive = signal(false);
  1951. /**
  1952. * True if the control has not been marked as touched
  1953. *
  1954. * A control is `untouched` if the user has not yet triggered
  1955. * a `blur` event on it.
  1956. */
  1957. get untouched() {
  1958. return !this.touched;
  1959. }
  1960. /**
  1961. * Exposed as observable, see below.
  1962. *
  1963. * @internal
  1964. */
  1965. _events = new Subject();
  1966. /**
  1967. * A multicasting observable that emits an event every time the state of the control changes.
  1968. * It emits for value, status, pristine or touched changes.
  1969. *
  1970. * **Note**: On value change, the emit happens right after a value of this control is updated. The
  1971. * value of a parent control (for example if this FormControl is a part of a FormGroup) is updated
  1972. * later, so accessing a value of a parent control (using the `value` property) from the callback
  1973. * of this event might result in getting a value that has not been updated yet. Subscribe to the
  1974. * `events` of the parent control instead.
  1975. * For other event types, the events are emitted after the parent control has been updated.
  1976. *
  1977. */
  1978. events = this._events.asObservable();
  1979. /**
  1980. * A multicasting observable that emits an event every time the value of the control changes, in
  1981. * the UI or programmatically. It also emits an event each time you call enable() or disable()
  1982. * without passing along {emitEvent: false} as a function argument.
  1983. *
  1984. * **Note**: the emit happens right after a value of this control is updated. The value of a
  1985. * parent control (for example if this FormControl is a part of a FormGroup) is updated later, so
  1986. * accessing a value of a parent control (using the `value` property) from the callback of this
  1987. * event might result in getting a value that has not been updated yet. Subscribe to the
  1988. * `valueChanges` event of the parent control instead.
  1989. */
  1990. valueChanges;
  1991. /**
  1992. * A multicasting observable that emits an event every time the validation `status` of the control
  1993. * recalculates.
  1994. *
  1995. * @see {@link FormControlStatus}
  1996. * @see {@link AbstractControl.status}
  1997. */
  1998. statusChanges;
  1999. /**
  2000. * Reports the update strategy of the `AbstractControl` (meaning
  2001. * the event on which the control updates itself).
  2002. * Possible values: `'change'` | `'blur'` | `'submit'`
  2003. * Default value: `'change'`
  2004. */
  2005. get updateOn() {
  2006. return this._updateOn ? this._updateOn : this.parent ? this.parent.updateOn : 'change';
  2007. }
  2008. /**
  2009. * Sets the synchronous validators that are active on this control. Calling
  2010. * this overwrites any existing synchronous validators.
  2011. *
  2012. * When you add or remove a validator at run time, you must call
  2013. * `updateValueAndValidity()` for the new validation to take effect.
  2014. *
  2015. * If you want to add a new validator without affecting existing ones, consider
  2016. * using `addValidators()` method instead.
  2017. */
  2018. setValidators(validators) {
  2019. this._assignValidators(validators);
  2020. }
  2021. /**
  2022. * Sets the asynchronous validators that are active on this control. Calling this
  2023. * overwrites any existing asynchronous validators.
  2024. *
  2025. * When you add or remove a validator at run time, you must call
  2026. * `updateValueAndValidity()` for the new validation to take effect.
  2027. *
  2028. * If you want to add a new validator without affecting existing ones, consider
  2029. * using `addAsyncValidators()` method instead.
  2030. */
  2031. setAsyncValidators(validators) {
  2032. this._assignAsyncValidators(validators);
  2033. }
  2034. /**
  2035. * Add a synchronous validator or validators to this control, without affecting other validators.
  2036. *
  2037. * When you add or remove a validator at run time, you must call
  2038. * `updateValueAndValidity()` for the new validation to take effect.
  2039. *
  2040. * Adding a validator that already exists will have no effect. If duplicate validator functions
  2041. * are present in the `validators` array, only the first instance would be added to a form
  2042. * control.
  2043. *
  2044. * @param validators The new validator function or functions to add to this control.
  2045. */
  2046. addValidators(validators) {
  2047. this.setValidators(addValidators(validators, this._rawValidators));
  2048. }
  2049. /**
  2050. * Add an asynchronous validator or validators to this control, without affecting other
  2051. * validators.
  2052. *
  2053. * When you add or remove a validator at run time, you must call
  2054. * `updateValueAndValidity()` for the new validation to take effect.
  2055. *
  2056. * Adding a validator that already exists will have no effect.
  2057. *
  2058. * @param validators The new asynchronous validator function or functions to add to this control.
  2059. */
  2060. addAsyncValidators(validators) {
  2061. this.setAsyncValidators(addValidators(validators, this._rawAsyncValidators));
  2062. }
  2063. /**
  2064. * Remove a synchronous validator from this control, without affecting other validators.
  2065. * Validators are compared by function reference; you must pass a reference to the exact same
  2066. * validator function as the one that was originally set. If a provided validator is not found,
  2067. * it is ignored.
  2068. *
  2069. * @usageNotes
  2070. *
  2071. * ### Reference to a ValidatorFn
  2072. *
  2073. * ```
  2074. * // Reference to the RequiredValidator
  2075. * const ctrl = new FormControl<string | null>('', Validators.required);
  2076. * ctrl.removeValidators(Validators.required);
  2077. *
  2078. * // Reference to anonymous function inside MinValidator
  2079. * const minValidator = Validators.min(3);
  2080. * const ctrl = new FormControl<string | null>('', minValidator);
  2081. * expect(ctrl.hasValidator(minValidator)).toEqual(true)
  2082. * expect(ctrl.hasValidator(Validators.min(3))).toEqual(false)
  2083. *
  2084. * ctrl.removeValidators(minValidator);
  2085. * ```
  2086. *
  2087. * When you add or remove a validator at run time, you must call
  2088. * `updateValueAndValidity()` for the new validation to take effect.
  2089. *
  2090. * @param validators The validator or validators to remove.
  2091. */
  2092. removeValidators(validators) {
  2093. this.setValidators(removeValidators(validators, this._rawValidators));
  2094. }
  2095. /**
  2096. * Remove an asynchronous validator from this control, without affecting other validators.
  2097. * Validators are compared by function reference; you must pass a reference to the exact same
  2098. * validator function as the one that was originally set. If a provided validator is not found, it
  2099. * is ignored.
  2100. *
  2101. * When you add or remove a validator at run time, you must call
  2102. * `updateValueAndValidity()` for the new validation to take effect.
  2103. *
  2104. * @param validators The asynchronous validator or validators to remove.
  2105. */
  2106. removeAsyncValidators(validators) {
  2107. this.setAsyncValidators(removeValidators(validators, this._rawAsyncValidators));
  2108. }
  2109. /**
  2110. * Check whether a synchronous validator function is present on this control. The provided
  2111. * validator must be a reference to the exact same function that was provided.
  2112. *
  2113. * @usageNotes
  2114. *
  2115. * ### Reference to a ValidatorFn
  2116. *
  2117. * ```
  2118. * // Reference to the RequiredValidator
  2119. * const ctrl = new FormControl<number | null>(0, Validators.required);
  2120. * expect(ctrl.hasValidator(Validators.required)).toEqual(true)
  2121. *
  2122. * // Reference to anonymous function inside MinValidator
  2123. * const minValidator = Validators.min(3);
  2124. * const ctrl = new FormControl<number | null>(0, minValidator);
  2125. * expect(ctrl.hasValidator(minValidator)).toEqual(true)
  2126. * expect(ctrl.hasValidator(Validators.min(3))).toEqual(false)
  2127. * ```
  2128. *
  2129. * @param validator The validator to check for presence. Compared by function reference.
  2130. * @returns Whether the provided validator was found on this control.
  2131. */
  2132. hasValidator(validator) {
  2133. return hasValidator(this._rawValidators, validator);
  2134. }
  2135. /**
  2136. * Check whether an asynchronous validator function is present on this control. The provided
  2137. * validator must be a reference to the exact same function that was provided.
  2138. *
  2139. * @param validator The asynchronous validator to check for presence. Compared by function
  2140. * reference.
  2141. * @returns Whether the provided asynchronous validator was found on this control.
  2142. */
  2143. hasAsyncValidator(validator) {
  2144. return hasValidator(this._rawAsyncValidators, validator);
  2145. }
  2146. /**
  2147. * Empties out the synchronous validator list.
  2148. *
  2149. * When you add or remove a validator at run time, you must call
  2150. * `updateValueAndValidity()` for the new validation to take effect.
  2151. *
  2152. */
  2153. clearValidators() {
  2154. this.validator = null;
  2155. }
  2156. /**
  2157. * Empties out the async validator list.
  2158. *
  2159. * When you add or remove a validator at run time, you must call
  2160. * `updateValueAndValidity()` for the new validation to take effect.
  2161. *
  2162. */
  2163. clearAsyncValidators() {
  2164. this.asyncValidator = null;
  2165. }
  2166. markAsTouched(opts = {}) {
  2167. const changed = this.touched === false;
  2168. this.touched = true;
  2169. const sourceControl = opts.sourceControl ?? this;
  2170. if (this._parent && !opts.onlySelf) {
  2171. this._parent.markAsTouched({ ...opts, sourceControl });
  2172. }
  2173. if (changed && opts.emitEvent !== false) {
  2174. this._events.next(new TouchedChangeEvent(true, sourceControl));
  2175. }
  2176. }
  2177. /**
  2178. * Marks the control and all its descendant controls as `touched`.
  2179. * @see {@link markAsTouched()}
  2180. *
  2181. * @param opts Configuration options that determine how the control propagates changes
  2182. * and emits events after marking is applied.
  2183. * * `emitEvent`: When true or not supplied (the default), the `events`
  2184. * observable emits a `TouchedChangeEvent` with the `touched` property being `true`.
  2185. * When false, no events are emitted.
  2186. */
  2187. markAllAsTouched(opts = {}) {
  2188. this.markAsTouched({ onlySelf: true, emitEvent: opts.emitEvent, sourceControl: this });
  2189. this._forEachChild((control) => control.markAllAsTouched(opts));
  2190. }
  2191. markAsUntouched(opts = {}) {
  2192. const changed = this.touched === true;
  2193. this.touched = false;
  2194. this._pendingTouched = false;
  2195. const sourceControl = opts.sourceControl ?? this;
  2196. this._forEachChild((control) => {
  2197. control.markAsUntouched({ onlySelf: true, emitEvent: opts.emitEvent, sourceControl });
  2198. });
  2199. if (this._parent && !opts.onlySelf) {
  2200. this._parent._updateTouched(opts, sourceControl);
  2201. }
  2202. if (changed && opts.emitEvent !== false) {
  2203. this._events.next(new TouchedChangeEvent(false, sourceControl));
  2204. }
  2205. }
  2206. markAsDirty(opts = {}) {
  2207. const changed = this.pristine === true;
  2208. this.pristine = false;
  2209. const sourceControl = opts.sourceControl ?? this;
  2210. if (this._parent && !opts.onlySelf) {
  2211. this._parent.markAsDirty({ ...opts, sourceControl });
  2212. }
  2213. if (changed && opts.emitEvent !== false) {
  2214. this._events.next(new PristineChangeEvent(false, sourceControl));
  2215. }
  2216. }
  2217. markAsPristine(opts = {}) {
  2218. const changed = this.pristine === false;
  2219. this.pristine = true;
  2220. this._pendingDirty = false;
  2221. const sourceControl = opts.sourceControl ?? this;
  2222. this._forEachChild((control) => {
  2223. /** We don't propagate the source control downwards */
  2224. control.markAsPristine({ onlySelf: true, emitEvent: opts.emitEvent });
  2225. });
  2226. if (this._parent && !opts.onlySelf) {
  2227. this._parent._updatePristine(opts, sourceControl);
  2228. }
  2229. if (changed && opts.emitEvent !== false) {
  2230. this._events.next(new PristineChangeEvent(true, sourceControl));
  2231. }
  2232. }
  2233. markAsPending(opts = {}) {
  2234. this.status = PENDING;
  2235. const sourceControl = opts.sourceControl ?? this;
  2236. if (opts.emitEvent !== false) {
  2237. this._events.next(new StatusChangeEvent(this.status, sourceControl));
  2238. this.statusChanges.emit(this.status);
  2239. }
  2240. if (this._parent && !opts.onlySelf) {
  2241. this._parent.markAsPending({ ...opts, sourceControl });
  2242. }
  2243. }
  2244. disable(opts = {}) {
  2245. // If parent has been marked artificially dirty we don't want to re-calculate the
  2246. // parent's dirtiness based on the children.
  2247. const skipPristineCheck = this._parentMarkedDirty(opts.onlySelf);
  2248. this.status = DISABLED;
  2249. this.errors = null;
  2250. this._forEachChild((control) => {
  2251. /** We don't propagate the source control downwards */
  2252. control.disable({ ...opts, onlySelf: true });
  2253. });
  2254. this._updateValue();
  2255. const sourceControl = opts.sourceControl ?? this;
  2256. if (opts.emitEvent !== false) {
  2257. this._events.next(new ValueChangeEvent(this.value, sourceControl));
  2258. this._events.next(new StatusChangeEvent(this.status, sourceControl));
  2259. this.valueChanges.emit(this.value);
  2260. this.statusChanges.emit(this.status);
  2261. }
  2262. this._updateAncestors({ ...opts, skipPristineCheck }, this);
  2263. this._onDisabledChange.forEach((changeFn) => changeFn(true));
  2264. }
  2265. /**
  2266. * Enables the control. This means the control is included in validation checks and
  2267. * the aggregate value of its parent. Its status recalculates based on its value and
  2268. * its validators.
  2269. *
  2270. * By default, if the control has children, all children are enabled.
  2271. *
  2272. * @see {@link AbstractControl.status}
  2273. *
  2274. * @param opts Configure options that control how the control propagates changes and
  2275. * emits events when marked as untouched
  2276. * * `onlySelf`: When true, mark only this control. When false or not supplied,
  2277. * marks all direct ancestors. Default is false.
  2278. * * `emitEvent`: When true or not supplied (the default), the `statusChanges`,
  2279. * `valueChanges` and `events`
  2280. * observables emit events with the latest status and value when the control is enabled.
  2281. * When false, no events are emitted.
  2282. */
  2283. enable(opts = {}) {
  2284. // If parent has been marked artificially dirty we don't want to re-calculate the
  2285. // parent's dirtiness based on the children.
  2286. const skipPristineCheck = this._parentMarkedDirty(opts.onlySelf);
  2287. this.status = VALID;
  2288. this._forEachChild((control) => {
  2289. control.enable({ ...opts, onlySelf: true });
  2290. });
  2291. this.updateValueAndValidity({ onlySelf: true, emitEvent: opts.emitEvent });
  2292. this._updateAncestors({ ...opts, skipPristineCheck }, this);
  2293. this._onDisabledChange.forEach((changeFn) => changeFn(false));
  2294. }
  2295. _updateAncestors(opts, sourceControl) {
  2296. if (this._parent && !opts.onlySelf) {
  2297. this._parent.updateValueAndValidity(opts);
  2298. if (!opts.skipPristineCheck) {
  2299. this._parent._updatePristine({}, sourceControl);
  2300. }
  2301. this._parent._updateTouched({}, sourceControl);
  2302. }
  2303. }
  2304. /**
  2305. * Sets the parent of the control
  2306. *
  2307. * @param parent The new parent.
  2308. */
  2309. setParent(parent) {
  2310. this._parent = parent;
  2311. }
  2312. /**
  2313. * The raw value of this control. For most control implementations, the raw value will include
  2314. * disabled children.
  2315. */
  2316. getRawValue() {
  2317. return this.value;
  2318. }
  2319. updateValueAndValidity(opts = {}) {
  2320. this._setInitialStatus();
  2321. this._updateValue();
  2322. if (this.enabled) {
  2323. const shouldHaveEmitted = this._cancelExistingSubscription();
  2324. this.errors = this._runValidator();
  2325. this.status = this._calculateStatus();
  2326. if (this.status === VALID || this.status === PENDING) {
  2327. // If the canceled subscription should have emitted
  2328. // we make sure the async validator emits the status change on completion
  2329. this._runAsyncValidator(shouldHaveEmitted, opts.emitEvent);
  2330. }
  2331. }
  2332. const sourceControl = opts.sourceControl ?? this;
  2333. if (opts.emitEvent !== false) {
  2334. this._events.next(new ValueChangeEvent(this.value, sourceControl));
  2335. this._events.next(new StatusChangeEvent(this.status, sourceControl));
  2336. this.valueChanges.emit(this.value);
  2337. this.statusChanges.emit(this.status);
  2338. }
  2339. if (this._parent && !opts.onlySelf) {
  2340. this._parent.updateValueAndValidity({ ...opts, sourceControl });
  2341. }
  2342. }
  2343. /** @internal */
  2344. _updateTreeValidity(opts = { emitEvent: true }) {
  2345. this._forEachChild((ctrl) => ctrl._updateTreeValidity(opts));
  2346. this.updateValueAndValidity({ onlySelf: true, emitEvent: opts.emitEvent });
  2347. }
  2348. _setInitialStatus() {
  2349. this.status = this._allControlsDisabled() ? DISABLED : VALID;
  2350. }
  2351. _runValidator() {
  2352. return this.validator ? this.validator(this) : null;
  2353. }
  2354. _runAsyncValidator(shouldHaveEmitted, emitEvent) {
  2355. if (this.asyncValidator) {
  2356. this.status = PENDING;
  2357. this._hasOwnPendingAsyncValidator = { emitEvent: emitEvent !== false };
  2358. const obs = toObservable(this.asyncValidator(this));
  2359. this._asyncValidationSubscription = obs.subscribe((errors) => {
  2360. this._hasOwnPendingAsyncValidator = null;
  2361. // This will trigger the recalculation of the validation status, which depends on
  2362. // the state of the asynchronous validation (whether it is in progress or not). So, it is
  2363. // necessary that we have updated the `_hasOwnPendingAsyncValidator` boolean flag first.
  2364. this.setErrors(errors, { emitEvent, shouldHaveEmitted });
  2365. });
  2366. }
  2367. }
  2368. _cancelExistingSubscription() {
  2369. if (this._asyncValidationSubscription) {
  2370. this._asyncValidationSubscription.unsubscribe();
  2371. // we're cancelling the validator subscribtion, we keep if it should have emitted
  2372. // because we want to emit eventually if it was required at least once.
  2373. const shouldHaveEmitted = this._hasOwnPendingAsyncValidator?.emitEvent ?? false;
  2374. this._hasOwnPendingAsyncValidator = null;
  2375. return shouldHaveEmitted;
  2376. }
  2377. return false;
  2378. }
  2379. setErrors(errors, opts = {}) {
  2380. this.errors = errors;
  2381. this._updateControlsErrors(opts.emitEvent !== false, this, opts.shouldHaveEmitted);
  2382. }
  2383. /**
  2384. * Retrieves a child control given the control's name or path.
  2385. *
  2386. * @param path A dot-delimited string or array of string/number values that define the path to the
  2387. * control. If a string is provided, passing it as a string literal will result in improved type
  2388. * information. Likewise, if an array is provided, passing it `as const` will cause improved type
  2389. * information to be available.
  2390. *
  2391. * @usageNotes
  2392. * ### Retrieve a nested control
  2393. *
  2394. * For example, to get a `name` control nested within a `person` sub-group:
  2395. *
  2396. * * `this.form.get('person.name');`
  2397. *
  2398. * -OR-
  2399. *
  2400. * * `this.form.get(['person', 'name'] as const);` // `as const` gives improved typings
  2401. *
  2402. * ### Retrieve a control in a FormArray
  2403. *
  2404. * When accessing an element inside a FormArray, you can use an element index.
  2405. * For example, to get a `price` control from the first element in an `items` array you can use:
  2406. *
  2407. * * `this.form.get('items.0.price');`
  2408. *
  2409. * -OR-
  2410. *
  2411. * * `this.form.get(['items', 0, 'price']);`
  2412. */
  2413. get(path) {
  2414. let currPath = path;
  2415. if (currPath == null)
  2416. return null;
  2417. if (!Array.isArray(currPath))
  2418. currPath = currPath.split('.');
  2419. if (currPath.length === 0)
  2420. return null;
  2421. return currPath.reduce((control, name) => control && control._find(name), this);
  2422. }
  2423. /**
  2424. * @description
  2425. * Reports error data for the control with the given path.
  2426. *
  2427. * @param errorCode The code of the error to check
  2428. * @param path A list of control names that designates how to move from the current control
  2429. * to the control that should be queried for errors.
  2430. *
  2431. * @usageNotes
  2432. * For example, for the following `FormGroup`:
  2433. *
  2434. * ```ts
  2435. * form = new FormGroup({
  2436. * address: new FormGroup({ street: new FormControl() })
  2437. * });
  2438. * ```
  2439. *
  2440. * The path to the 'street' control from the root form would be 'address' -> 'street'.
  2441. *
  2442. * It can be provided to this method in one of two formats:
  2443. *
  2444. * 1. An array of string control names, e.g. `['address', 'street']`
  2445. * 1. A period-delimited list of control names in one string, e.g. `'address.street'`
  2446. *
  2447. * @returns error data for that particular error. If the control or error is not present,
  2448. * null is returned.
  2449. */
  2450. getError(errorCode, path) {
  2451. const control = path ? this.get(path) : this;
  2452. return control && control.errors ? control.errors[errorCode] : null;
  2453. }
  2454. /**
  2455. * @description
  2456. * Reports whether the control with the given path has the error specified.
  2457. *
  2458. * @param errorCode The code of the error to check
  2459. * @param path A list of control names that designates how to move from the current control
  2460. * to the control that should be queried for errors.
  2461. *
  2462. * @usageNotes
  2463. * For example, for the following `FormGroup`:
  2464. *
  2465. * ```ts
  2466. * form = new FormGroup({
  2467. * address: new FormGroup({ street: new FormControl() })
  2468. * });
  2469. * ```
  2470. *
  2471. * The path to the 'street' control from the root form would be 'address' -> 'street'.
  2472. *
  2473. * It can be provided to this method in one of two formats:
  2474. *
  2475. * 1. An array of string control names, e.g. `['address', 'street']`
  2476. * 1. A period-delimited list of control names in one string, e.g. `'address.street'`
  2477. *
  2478. * If no path is given, this method checks for the error on the current control.
  2479. *
  2480. * @returns whether the given error is present in the control at the given path.
  2481. *
  2482. * If the control is not present, false is returned.
  2483. */
  2484. hasError(errorCode, path) {
  2485. return !!this.getError(errorCode, path);
  2486. }
  2487. /**
  2488. * Retrieves the top-level ancestor of this control.
  2489. */
  2490. get root() {
  2491. let x = this;
  2492. while (x._parent) {
  2493. x = x._parent;
  2494. }
  2495. return x;
  2496. }
  2497. /** @internal */
  2498. _updateControlsErrors(emitEvent, changedControl, shouldHaveEmitted) {
  2499. this.status = this._calculateStatus();
  2500. if (emitEvent) {
  2501. this.statusChanges.emit(this.status);
  2502. }
  2503. // The Events Observable expose a slight different bevahior than the statusChanges obs
  2504. // An async validator will still emit a StatusChangeEvent is a previously cancelled
  2505. // async validator has emitEvent set to true
  2506. if (emitEvent || shouldHaveEmitted) {
  2507. this._events.next(new StatusChangeEvent(this.status, changedControl));
  2508. }
  2509. if (this._parent) {
  2510. this._parent._updateControlsErrors(emitEvent, changedControl, shouldHaveEmitted);
  2511. }
  2512. }
  2513. /** @internal */
  2514. _initObservables() {
  2515. // TODO: this should be piped from events() but is breaking in G3
  2516. this.valueChanges = new EventEmitter();
  2517. this.statusChanges = new EventEmitter();
  2518. }
  2519. _calculateStatus() {
  2520. if (this._allControlsDisabled())
  2521. return DISABLED;
  2522. if (this.errors)
  2523. return INVALID;
  2524. if (this._hasOwnPendingAsyncValidator || this._anyControlsHaveStatus(PENDING))
  2525. return PENDING;
  2526. if (this._anyControlsHaveStatus(INVALID))
  2527. return INVALID;
  2528. return VALID;
  2529. }
  2530. /** @internal */
  2531. _anyControlsHaveStatus(status) {
  2532. return this._anyControls((control) => control.status === status);
  2533. }
  2534. /** @internal */
  2535. _anyControlsDirty() {
  2536. return this._anyControls((control) => control.dirty);
  2537. }
  2538. /** @internal */
  2539. _anyControlsTouched() {
  2540. return this._anyControls((control) => control.touched);
  2541. }
  2542. /** @internal */
  2543. _updatePristine(opts, changedControl) {
  2544. const newPristine = !this._anyControlsDirty();
  2545. const changed = this.pristine !== newPristine;
  2546. this.pristine = newPristine;
  2547. if (this._parent && !opts.onlySelf) {
  2548. this._parent._updatePristine(opts, changedControl);
  2549. }
  2550. if (changed) {
  2551. this._events.next(new PristineChangeEvent(this.pristine, changedControl));
  2552. }
  2553. }
  2554. /** @internal */
  2555. _updateTouched(opts = {}, changedControl) {
  2556. this.touched = this._anyControlsTouched();
  2557. this._events.next(new TouchedChangeEvent(this.touched, changedControl));
  2558. if (this._parent && !opts.onlySelf) {
  2559. this._parent._updateTouched(opts, changedControl);
  2560. }
  2561. }
  2562. /** @internal */
  2563. _onDisabledChange = [];
  2564. /** @internal */
  2565. _registerOnCollectionChange(fn) {
  2566. this._onCollectionChange = fn;
  2567. }
  2568. /** @internal */
  2569. _setUpdateStrategy(opts) {
  2570. if (isOptionsObj(opts) && opts.updateOn != null) {
  2571. this._updateOn = opts.updateOn;
  2572. }
  2573. }
  2574. /**
  2575. * Check to see if parent has been marked artificially dirty.
  2576. *
  2577. * @internal
  2578. */
  2579. _parentMarkedDirty(onlySelf) {
  2580. const parentDirty = this._parent && this._parent.dirty;
  2581. return !onlySelf && !!parentDirty && !this._parent._anyControlsDirty();
  2582. }
  2583. /** @internal */
  2584. _find(name) {
  2585. return null;
  2586. }
  2587. /**
  2588. * Internal implementation of the `setValidators` method. Needs to be separated out into a
  2589. * different method, because it is called in the constructor and it can break cases where
  2590. * a control is extended.
  2591. */
  2592. _assignValidators(validators) {
  2593. this._rawValidators = Array.isArray(validators) ? validators.slice() : validators;
  2594. this._composedValidatorFn = coerceToValidator(this._rawValidators);
  2595. }
  2596. /**
  2597. * Internal implementation of the `setAsyncValidators` method. Needs to be separated out into a
  2598. * different method, because it is called in the constructor and it can break cases where
  2599. * a control is extended.
  2600. */
  2601. _assignAsyncValidators(validators) {
  2602. this._rawAsyncValidators = Array.isArray(validators) ? validators.slice() : validators;
  2603. this._composedAsyncValidatorFn = coerceToAsyncValidator(this._rawAsyncValidators);
  2604. }
  2605. }
  2606. /**
  2607. * Tracks the value and validity state of a group of `FormControl` instances.
  2608. *
  2609. * A `FormGroup` aggregates the values of each child `FormControl` into one object,
  2610. * with each control name as the key. It calculates its status by reducing the status values
  2611. * of its children. For example, if one of the controls in a group is invalid, the entire
  2612. * group becomes invalid.
  2613. *
  2614. * `FormGroup` is one of the four fundamental building blocks used to define forms in Angular,
  2615. * along with `FormControl`, `FormArray`, and `FormRecord`.
  2616. *
  2617. * When instantiating a `FormGroup`, pass in a collection of child controls as the first
  2618. * argument. The key for each child registers the name for the control.
  2619. *
  2620. * `FormGroup` is intended for use cases where the keys are known ahead of time.
  2621. * If you need to dynamically add and remove controls, use {@link FormRecord} instead.
  2622. *
  2623. * `FormGroup` accepts an optional type parameter `TControl`, which is an object type with inner
  2624. * control types as values.
  2625. *
  2626. * @usageNotes
  2627. *
  2628. * ### Create a form group with 2 controls
  2629. *
  2630. * ```ts
  2631. * const form = new FormGroup({
  2632. * first: new FormControl('Nancy', Validators.minLength(2)),
  2633. * last: new FormControl('Drew'),
  2634. * });
  2635. *
  2636. * console.log(form.value); // {first: 'Nancy', last; 'Drew'}
  2637. * console.log(form.status); // 'VALID'
  2638. * ```
  2639. *
  2640. * ### The type argument, and optional controls
  2641. *
  2642. * `FormGroup` accepts one generic argument, which is an object containing its inner controls.
  2643. * This type will usually be inferred automatically, but you can always specify it explicitly if you
  2644. * wish.
  2645. *
  2646. * If you have controls that are optional (i.e. they can be removed, you can use the `?` in the
  2647. * type):
  2648. *
  2649. * ```ts
  2650. * const form = new FormGroup<{
  2651. * first: FormControl<string|null>,
  2652. * middle?: FormControl<string|null>, // Middle name is optional.
  2653. * last: FormControl<string|null>,
  2654. * }>({
  2655. * first: new FormControl('Nancy'),
  2656. * last: new FormControl('Drew'),
  2657. * });
  2658. * ```
  2659. *
  2660. * ### Create a form group with a group-level validator
  2661. *
  2662. * You include group-level validators as the second arg, or group-level async
  2663. * validators as the third arg. These come in handy when you want to perform validation
  2664. * that considers the value of more than one child control.
  2665. *
  2666. * ```ts
  2667. * const form = new FormGroup({
  2668. * password: new FormControl('', Validators.minLength(2)),
  2669. * passwordConfirm: new FormControl('', Validators.minLength(2)),
  2670. * }, passwordMatchValidator);
  2671. *
  2672. *
  2673. * function passwordMatchValidator(g: FormGroup) {
  2674. * return g.get('password').value === g.get('passwordConfirm').value
  2675. * ? null : {'mismatch': true};
  2676. * }
  2677. * ```
  2678. *
  2679. * Like `FormControl` instances, you choose to pass in
  2680. * validators and async validators as part of an options object.
  2681. *
  2682. * ```ts
  2683. * const form = new FormGroup({
  2684. * password: new FormControl('')
  2685. * passwordConfirm: new FormControl('')
  2686. * }, { validators: passwordMatchValidator, asyncValidators: otherValidator });
  2687. * ```
  2688. *
  2689. * ### Set the updateOn property for all controls in a form group
  2690. *
  2691. * The options object is used to set a default value for each child
  2692. * control's `updateOn` property. If you set `updateOn` to `'blur'` at the
  2693. * group level, all child controls default to 'blur', unless the child
  2694. * has explicitly specified a different `updateOn` value.
  2695. *
  2696. * ```ts
  2697. * const c = new FormGroup({
  2698. * one: new FormControl()
  2699. * }, { updateOn: 'blur' });
  2700. * ```
  2701. *
  2702. * ### Using a FormGroup with optional controls
  2703. *
  2704. * It is possible to have optional controls in a FormGroup. An optional control can be removed later
  2705. * using `removeControl`, and can be omitted when calling `reset`. Optional controls must be
  2706. * declared optional in the group's type.
  2707. *
  2708. * ```ts
  2709. * const c = new FormGroup<{one?: FormControl<string>}>({
  2710. * one: new FormControl('')
  2711. * });
  2712. * ```
  2713. *
  2714. * Notice that `c.value.one` has type `string|null|undefined`. This is because calling `c.reset({})`
  2715. * without providing the optional key `one` will cause it to become `null`.
  2716. *
  2717. * @publicApi
  2718. */
  2719. class FormGroup extends AbstractControl {
  2720. /**
  2721. * Creates a new `FormGroup` instance.
  2722. *
  2723. * @param controls A collection of child controls. The key for each child is the name
  2724. * under which it is registered.
  2725. *
  2726. * @param validatorOrOpts A synchronous validator function, or an array of
  2727. * such functions, or an `AbstractControlOptions` object that contains validation functions
  2728. * and a validation trigger.
  2729. *
  2730. * @param asyncValidator A single async validator or array of async validator functions
  2731. *
  2732. */
  2733. constructor(controls, validatorOrOpts, asyncValidator) {
  2734. super(pickValidators(validatorOrOpts), pickAsyncValidators(asyncValidator, validatorOrOpts));
  2735. (typeof ngDevMode === 'undefined' || ngDevMode) && validateFormGroupControls(controls);
  2736. this.controls = controls;
  2737. this._initObservables();
  2738. this._setUpdateStrategy(validatorOrOpts);
  2739. this._setUpControls();
  2740. this.updateValueAndValidity({
  2741. onlySelf: true,
  2742. // If `asyncValidator` is present, it will trigger control status change from `PENDING` to
  2743. // `VALID` or `INVALID`. The status should be broadcasted via the `statusChanges` observable,
  2744. // so we set `emitEvent` to `true` to allow that during the control creation process.
  2745. emitEvent: !!this.asyncValidator,
  2746. });
  2747. }
  2748. controls;
  2749. registerControl(name, control) {
  2750. if (this.controls[name])
  2751. return this.controls[name];
  2752. this.controls[name] = control;
  2753. control.setParent(this);
  2754. control._registerOnCollectionChange(this._onCollectionChange);
  2755. return control;
  2756. }
  2757. addControl(name, control, options = {}) {
  2758. this.registerControl(name, control);
  2759. this.updateValueAndValidity({ emitEvent: options.emitEvent });
  2760. this._onCollectionChange();
  2761. }
  2762. /**
  2763. * Remove a control from this group. In a strongly-typed group, required controls cannot be
  2764. * removed.
  2765. *
  2766. * This method also updates the value and validity of the control.
  2767. *
  2768. * @param name The control name to remove from the collection
  2769. * @param options Specifies whether this FormGroup instance should emit events after a
  2770. * control is removed.
  2771. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  2772. * `valueChanges` observables emit events with the latest status and value when the control is
  2773. * removed. When false, no events are emitted.
  2774. */
  2775. removeControl(name, options = {}) {
  2776. if (this.controls[name])
  2777. this.controls[name]._registerOnCollectionChange(() => { });
  2778. delete this.controls[name];
  2779. this.updateValueAndValidity({ emitEvent: options.emitEvent });
  2780. this._onCollectionChange();
  2781. }
  2782. setControl(name, control, options = {}) {
  2783. if (this.controls[name])
  2784. this.controls[name]._registerOnCollectionChange(() => { });
  2785. delete this.controls[name];
  2786. if (control)
  2787. this.registerControl(name, control);
  2788. this.updateValueAndValidity({ emitEvent: options.emitEvent });
  2789. this._onCollectionChange();
  2790. }
  2791. contains(controlName) {
  2792. return this.controls.hasOwnProperty(controlName) && this.controls[controlName].enabled;
  2793. }
  2794. /**
  2795. * Sets the value of the `FormGroup`. It accepts an object that matches
  2796. * the structure of the group, with control names as keys.
  2797. *
  2798. * @usageNotes
  2799. * ### Set the complete value for the form group
  2800. *
  2801. * ```ts
  2802. * const form = new FormGroup({
  2803. * first: new FormControl(),
  2804. * last: new FormControl()
  2805. * });
  2806. *
  2807. * console.log(form.value); // {first: null, last: null}
  2808. *
  2809. * form.setValue({first: 'Nancy', last: 'Drew'});
  2810. * console.log(form.value); // {first: 'Nancy', last: 'Drew'}
  2811. * ```
  2812. *
  2813. * @throws When strict checks fail, such as setting the value of a control
  2814. * that doesn't exist or if you exclude a value of a control that does exist.
  2815. *
  2816. * @param value The new value for the control that matches the structure of the group.
  2817. * @param options Configuration options that determine how the control propagates changes
  2818. * and emits events after the value changes.
  2819. * The configuration options are passed to the {@link AbstractControl#updateValueAndValidity
  2820. * updateValueAndValidity} method.
  2821. *
  2822. * * `onlySelf`: When true, each change only affects this control, and not its parent. Default is
  2823. * false.
  2824. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  2825. * `valueChanges`
  2826. * observables emit events with the latest status and value when the control value is updated.
  2827. * When false, no events are emitted.
  2828. */
  2829. setValue(value, options = {}) {
  2830. assertAllValuesPresent(this, true, value);
  2831. Object.keys(value).forEach((name) => {
  2832. assertControlPresent(this, true, name);
  2833. this.controls[name].setValue(value[name], {
  2834. onlySelf: true,
  2835. emitEvent: options.emitEvent,
  2836. });
  2837. });
  2838. this.updateValueAndValidity(options);
  2839. }
  2840. /**
  2841. * Patches the value of the `FormGroup`. It accepts an object with control
  2842. * names as keys, and does its best to match the values to the correct controls
  2843. * in the group.
  2844. *
  2845. * It accepts both super-sets and sub-sets of the group without throwing an error.
  2846. *
  2847. * @usageNotes
  2848. * ### Patch the value for a form group
  2849. *
  2850. * ```ts
  2851. * const form = new FormGroup({
  2852. * first: new FormControl(),
  2853. * last: new FormControl()
  2854. * });
  2855. * console.log(form.value); // {first: null, last: null}
  2856. *
  2857. * form.patchValue({first: 'Nancy'});
  2858. * console.log(form.value); // {first: 'Nancy', last: null}
  2859. * ```
  2860. *
  2861. * @param value The object that matches the structure of the group.
  2862. * @param options Configuration options that determine how the control propagates changes and
  2863. * emits events after the value is patched.
  2864. * * `onlySelf`: When true, each change only affects this control and not its parent. Default is
  2865. * true.
  2866. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  2867. * `valueChanges` observables emit events with the latest status and value when the control value
  2868. * is updated. When false, no events are emitted. The configuration options are passed to
  2869. * the {@link AbstractControl#updateValueAndValidity updateValueAndValidity} method.
  2870. */
  2871. patchValue(value, options = {}) {
  2872. // Even though the `value` argument type doesn't allow `null` and `undefined` values, the
  2873. // `patchValue` can be called recursively and inner data structures might have these values, so
  2874. // we just ignore such cases when a field containing FormGroup instance receives `null` or
  2875. // `undefined` as a value.
  2876. if (value == null /* both `null` and `undefined` */)
  2877. return;
  2878. Object.keys(value).forEach((name) => {
  2879. // The compiler cannot see through the uninstantiated conditional type of `this.controls`, so
  2880. // `as any` is required.
  2881. const control = this.controls[name];
  2882. if (control) {
  2883. control.patchValue(
  2884. /* Guaranteed to be present, due to the outer forEach. */ value[name], { onlySelf: true, emitEvent: options.emitEvent });
  2885. }
  2886. });
  2887. this.updateValueAndValidity(options);
  2888. }
  2889. /**
  2890. * Resets the `FormGroup`, marks all descendants `pristine` and `untouched` and sets
  2891. * the value of all descendants to their default values, or null if no defaults were provided.
  2892. *
  2893. * You reset to a specific form state by passing in a map of states
  2894. * that matches the structure of your form, with control names as keys. The state
  2895. * is a standalone value or a form state object with both a value and a disabled
  2896. * status.
  2897. *
  2898. * @param value Resets the control with an initial value,
  2899. * or an object that defines the initial value and disabled state.
  2900. *
  2901. * @param options Configuration options that determine how the control propagates changes
  2902. * and emits events when the group is reset.
  2903. * * `onlySelf`: When true, each change only affects this control, and not its parent. Default is
  2904. * false.
  2905. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  2906. * `valueChanges`
  2907. * observables emit events with the latest status and value when the control is reset.
  2908. * When false, no events are emitted.
  2909. * The configuration options are passed to the {@link AbstractControl#updateValueAndValidity
  2910. * updateValueAndValidity} method.
  2911. *
  2912. * @usageNotes
  2913. *
  2914. * ### Reset the form group values
  2915. *
  2916. * ```ts
  2917. * const form = new FormGroup({
  2918. * first: new FormControl('first name'),
  2919. * last: new FormControl('last name')
  2920. * });
  2921. *
  2922. * console.log(form.value); // {first: 'first name', last: 'last name'}
  2923. *
  2924. * form.reset({ first: 'name', last: 'last name' });
  2925. *
  2926. * console.log(form.value); // {first: 'name', last: 'last name'}
  2927. * ```
  2928. *
  2929. * ### Reset the form group values and disabled status
  2930. *
  2931. * ```ts
  2932. * const form = new FormGroup({
  2933. * first: new FormControl('first name'),
  2934. * last: new FormControl('last name')
  2935. * });
  2936. *
  2937. * form.reset({
  2938. * first: {value: 'name', disabled: true},
  2939. * last: 'last'
  2940. * });
  2941. *
  2942. * console.log(form.value); // {last: 'last'}
  2943. * console.log(form.get('first').status); // 'DISABLED'
  2944. * ```
  2945. */
  2946. reset(value = {}, options = {}) {
  2947. this._forEachChild((control, name) => {
  2948. control.reset(value ? value[name] : null, {
  2949. onlySelf: true,
  2950. emitEvent: options.emitEvent,
  2951. });
  2952. });
  2953. this._updatePristine(options, this);
  2954. this._updateTouched(options, this);
  2955. this.updateValueAndValidity(options);
  2956. }
  2957. /**
  2958. * The aggregate value of the `FormGroup`, including any disabled controls.
  2959. *
  2960. * Retrieves all values regardless of disabled status.
  2961. */
  2962. getRawValue() {
  2963. return this._reduceChildren({}, (acc, control, name) => {
  2964. acc[name] = control.getRawValue();
  2965. return acc;
  2966. });
  2967. }
  2968. /** @internal */
  2969. _syncPendingControls() {
  2970. let subtreeUpdated = this._reduceChildren(false, (updated, child) => {
  2971. return child._syncPendingControls() ? true : updated;
  2972. });
  2973. if (subtreeUpdated)
  2974. this.updateValueAndValidity({ onlySelf: true });
  2975. return subtreeUpdated;
  2976. }
  2977. /** @internal */
  2978. _forEachChild(cb) {
  2979. Object.keys(this.controls).forEach((key) => {
  2980. // The list of controls can change (for ex. controls might be removed) while the loop
  2981. // is running (as a result of invoking Forms API in `valueChanges` subscription), so we
  2982. // have to null check before invoking the callback.
  2983. const control = this.controls[key];
  2984. control && cb(control, key);
  2985. });
  2986. }
  2987. /** @internal */
  2988. _setUpControls() {
  2989. this._forEachChild((control) => {
  2990. control.setParent(this);
  2991. control._registerOnCollectionChange(this._onCollectionChange);
  2992. });
  2993. }
  2994. /** @internal */
  2995. _updateValue() {
  2996. this.value = this._reduceValue();
  2997. }
  2998. /** @internal */
  2999. _anyControls(condition) {
  3000. for (const [controlName, control] of Object.entries(this.controls)) {
  3001. if (this.contains(controlName) && condition(control)) {
  3002. return true;
  3003. }
  3004. }
  3005. return false;
  3006. }
  3007. /** @internal */
  3008. _reduceValue() {
  3009. let acc = {};
  3010. return this._reduceChildren(acc, (acc, control, name) => {
  3011. if (control.enabled || this.disabled) {
  3012. acc[name] = control.value;
  3013. }
  3014. return acc;
  3015. });
  3016. }
  3017. /** @internal */
  3018. _reduceChildren(initValue, fn) {
  3019. let res = initValue;
  3020. this._forEachChild((control, name) => {
  3021. res = fn(res, control, name);
  3022. });
  3023. return res;
  3024. }
  3025. /** @internal */
  3026. _allControlsDisabled() {
  3027. for (const controlName of Object.keys(this.controls)) {
  3028. if (this.controls[controlName].enabled) {
  3029. return false;
  3030. }
  3031. }
  3032. return Object.keys(this.controls).length > 0 || this.disabled;
  3033. }
  3034. /** @internal */
  3035. _find(name) {
  3036. return this.controls.hasOwnProperty(name)
  3037. ? this.controls[name]
  3038. : null;
  3039. }
  3040. }
  3041. /**
  3042. * Will validate that none of the controls has a key with a dot
  3043. * Throws other wise
  3044. */
  3045. function validateFormGroupControls(controls) {
  3046. const invalidKeys = Object.keys(controls).filter((key) => key.includes('.'));
  3047. if (invalidKeys.length > 0) {
  3048. // TODO: make this an error once there are no more uses in G3
  3049. console.warn(`FormGroup keys cannot include \`.\`, please replace the keys for: ${invalidKeys.join(',')}.`);
  3050. }
  3051. }
  3052. const UntypedFormGroup = FormGroup;
  3053. /**
  3054. * @description
  3055. * Asserts that the given control is an instance of `FormGroup`
  3056. *
  3057. * @publicApi
  3058. */
  3059. const isFormGroup = (control) => control instanceof FormGroup;
  3060. /**
  3061. * Tracks the value and validity state of a collection of `FormControl` instances, each of which has
  3062. * the same value type.
  3063. *
  3064. * `FormRecord` is very similar to {@link FormGroup}, except it can be used with a dynamic keys,
  3065. * with controls added and removed as needed.
  3066. *
  3067. * `FormRecord` accepts one generic argument, which describes the type of the controls it contains.
  3068. *
  3069. * @usageNotes
  3070. *
  3071. * ```ts
  3072. * let numbers = new FormRecord({bill: new FormControl('415-123-456')});
  3073. * numbers.addControl('bob', new FormControl('415-234-567'));
  3074. * numbers.removeControl('bill');
  3075. * ```
  3076. *
  3077. * @publicApi
  3078. */
  3079. class FormRecord extends FormGroup {
  3080. }
  3081. /**
  3082. * @description
  3083. * Asserts that the given control is an instance of `FormRecord`
  3084. *
  3085. * @publicApi
  3086. */
  3087. const isFormRecord = (control) => control instanceof FormRecord;
  3088. /**
  3089. * Token to provide to allow SetDisabledState to always be called when a CVA is added, regardless of
  3090. * whether the control is disabled or enabled.
  3091. *
  3092. * @see {@link FormsModule#withconfig}
  3093. */
  3094. const CALL_SET_DISABLED_STATE = new InjectionToken(typeof ngDevMode === 'undefined' || ngDevMode ? 'CallSetDisabledState' : '', {
  3095. providedIn: 'root',
  3096. factory: () => setDisabledStateDefault,
  3097. });
  3098. /**
  3099. * Whether to use the fixed setDisabledState behavior by default.
  3100. */
  3101. const setDisabledStateDefault = 'always';
  3102. function controlPath(name, parent) {
  3103. return [...parent.path, name];
  3104. }
  3105. /**
  3106. * Links a Form control and a Form directive by setting up callbacks (such as `onChange`) on both
  3107. * instances. This function is typically invoked when form directive is being initialized.
  3108. *
  3109. * @param control Form control instance that should be linked.
  3110. * @param dir Directive that should be linked with a given control.
  3111. */
  3112. function setUpControl(control, dir, callSetDisabledState = setDisabledStateDefault) {
  3113. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  3114. if (!control)
  3115. _throwError(dir, 'Cannot find control with');
  3116. if (!dir.valueAccessor)
  3117. _throwMissingValueAccessorError(dir);
  3118. }
  3119. setUpValidators(control, dir);
  3120. dir.valueAccessor.writeValue(control.value);
  3121. // The legacy behavior only calls the CVA's `setDisabledState` if the control is disabled.
  3122. // If the `callSetDisabledState` option is set to `always`, then this bug is fixed and
  3123. // the method is always called.
  3124. if (control.disabled || callSetDisabledState === 'always') {
  3125. dir.valueAccessor.setDisabledState?.(control.disabled);
  3126. }
  3127. setUpViewChangePipeline(control, dir);
  3128. setUpModelChangePipeline(control, dir);
  3129. setUpBlurPipeline(control, dir);
  3130. setUpDisabledChangeHandler(control, dir);
  3131. }
  3132. /**
  3133. * Reverts configuration performed by the `setUpControl` control function.
  3134. * Effectively disconnects form control with a given form directive.
  3135. * This function is typically invoked when corresponding form directive is being destroyed.
  3136. *
  3137. * @param control Form control which should be cleaned up.
  3138. * @param dir Directive that should be disconnected from a given control.
  3139. * @param validateControlPresenceOnChange Flag that indicates whether onChange handler should
  3140. * contain asserts to verify that it's not called once directive is destroyed. We need this flag
  3141. * to avoid potentially breaking changes caused by better control cleanup introduced in #39235.
  3142. */
  3143. function cleanUpControl(control, dir, validateControlPresenceOnChange = true) {
  3144. const noop = () => {
  3145. if (validateControlPresenceOnChange && (typeof ngDevMode === 'undefined' || ngDevMode)) {
  3146. _noControlError(dir);
  3147. }
  3148. };
  3149. // The `valueAccessor` field is typically defined on FromControl and FormControlName directive
  3150. // instances and there is a logic in `selectValueAccessor` function that throws if it's not the
  3151. // case. We still check the presence of `valueAccessor` before invoking its methods to make sure
  3152. // that cleanup works correctly if app code or tests are setup to ignore the error thrown from
  3153. // `selectValueAccessor`. See https://github.com/angular/angular/issues/40521.
  3154. if (dir.valueAccessor) {
  3155. dir.valueAccessor.registerOnChange(noop);
  3156. dir.valueAccessor.registerOnTouched(noop);
  3157. }
  3158. cleanUpValidators(control, dir);
  3159. if (control) {
  3160. dir._invokeOnDestroyCallbacks();
  3161. control._registerOnCollectionChange(() => { });
  3162. }
  3163. }
  3164. function registerOnValidatorChange(validators, onChange) {
  3165. validators.forEach((validator) => {
  3166. if (validator.registerOnValidatorChange)
  3167. validator.registerOnValidatorChange(onChange);
  3168. });
  3169. }
  3170. /**
  3171. * Sets up disabled change handler function on a given form control if ControlValueAccessor
  3172. * associated with a given directive instance supports the `setDisabledState` call.
  3173. *
  3174. * @param control Form control where disabled change handler should be setup.
  3175. * @param dir Corresponding directive instance associated with this control.
  3176. */
  3177. function setUpDisabledChangeHandler(control, dir) {
  3178. if (dir.valueAccessor.setDisabledState) {
  3179. const onDisabledChange = (isDisabled) => {
  3180. dir.valueAccessor.setDisabledState(isDisabled);
  3181. };
  3182. control.registerOnDisabledChange(onDisabledChange);
  3183. // Register a callback function to cleanup disabled change handler
  3184. // from a control instance when a directive is destroyed.
  3185. dir._registerOnDestroy(() => {
  3186. control._unregisterOnDisabledChange(onDisabledChange);
  3187. });
  3188. }
  3189. }
  3190. /**
  3191. * Sets up sync and async directive validators on provided form control.
  3192. * This function merges validators from the directive into the validators of the control.
  3193. *
  3194. * @param control Form control where directive validators should be setup.
  3195. * @param dir Directive instance that contains validators to be setup.
  3196. */
  3197. function setUpValidators(control, dir) {
  3198. const validators = getControlValidators(control);
  3199. if (dir.validator !== null) {
  3200. control.setValidators(mergeValidators(validators, dir.validator));
  3201. }
  3202. else if (typeof validators === 'function') {
  3203. // If sync validators are represented by a single validator function, we force the
  3204. // `Validators.compose` call to happen by executing the `setValidators` function with
  3205. // an array that contains that function. We need this to avoid possible discrepancies in
  3206. // validators behavior, so sync validators are always processed by the `Validators.compose`.
  3207. // Note: we should consider moving this logic inside the `setValidators` function itself, so we
  3208. // have consistent behavior on AbstractControl API level. The same applies to the async
  3209. // validators logic below.
  3210. control.setValidators([validators]);
  3211. }
  3212. const asyncValidators = getControlAsyncValidators(control);
  3213. if (dir.asyncValidator !== null) {
  3214. control.setAsyncValidators(mergeValidators(asyncValidators, dir.asyncValidator));
  3215. }
  3216. else if (typeof asyncValidators === 'function') {
  3217. control.setAsyncValidators([asyncValidators]);
  3218. }
  3219. // Re-run validation when validator binding changes, e.g. minlength=3 -> minlength=4
  3220. const onValidatorChange = () => control.updateValueAndValidity();
  3221. registerOnValidatorChange(dir._rawValidators, onValidatorChange);
  3222. registerOnValidatorChange(dir._rawAsyncValidators, onValidatorChange);
  3223. }
  3224. /**
  3225. * Cleans up sync and async directive validators on provided form control.
  3226. * This function reverts the setup performed by the `setUpValidators` function, i.e.
  3227. * removes directive-specific validators from a given control instance.
  3228. *
  3229. * @param control Form control from where directive validators should be removed.
  3230. * @param dir Directive instance that contains validators to be removed.
  3231. * @returns true if a control was updated as a result of this action.
  3232. */
  3233. function cleanUpValidators(control, dir) {
  3234. let isControlUpdated = false;
  3235. if (control !== null) {
  3236. if (dir.validator !== null) {
  3237. const validators = getControlValidators(control);
  3238. if (Array.isArray(validators) && validators.length > 0) {
  3239. // Filter out directive validator function.
  3240. const updatedValidators = validators.filter((validator) => validator !== dir.validator);
  3241. if (updatedValidators.length !== validators.length) {
  3242. isControlUpdated = true;
  3243. control.setValidators(updatedValidators);
  3244. }
  3245. }
  3246. }
  3247. if (dir.asyncValidator !== null) {
  3248. const asyncValidators = getControlAsyncValidators(control);
  3249. if (Array.isArray(asyncValidators) && asyncValidators.length > 0) {
  3250. // Filter out directive async validator function.
  3251. const updatedAsyncValidators = asyncValidators.filter((asyncValidator) => asyncValidator !== dir.asyncValidator);
  3252. if (updatedAsyncValidators.length !== asyncValidators.length) {
  3253. isControlUpdated = true;
  3254. control.setAsyncValidators(updatedAsyncValidators);
  3255. }
  3256. }
  3257. }
  3258. }
  3259. // Clear onValidatorChange callbacks by providing a noop function.
  3260. const noop = () => { };
  3261. registerOnValidatorChange(dir._rawValidators, noop);
  3262. registerOnValidatorChange(dir._rawAsyncValidators, noop);
  3263. return isControlUpdated;
  3264. }
  3265. function setUpViewChangePipeline(control, dir) {
  3266. dir.valueAccessor.registerOnChange((newValue) => {
  3267. control._pendingValue = newValue;
  3268. control._pendingChange = true;
  3269. control._pendingDirty = true;
  3270. if (control.updateOn === 'change')
  3271. updateControl(control, dir);
  3272. });
  3273. }
  3274. function setUpBlurPipeline(control, dir) {
  3275. dir.valueAccessor.registerOnTouched(() => {
  3276. control._pendingTouched = true;
  3277. if (control.updateOn === 'blur' && control._pendingChange)
  3278. updateControl(control, dir);
  3279. if (control.updateOn !== 'submit')
  3280. control.markAsTouched();
  3281. });
  3282. }
  3283. function updateControl(control, dir) {
  3284. if (control._pendingDirty)
  3285. control.markAsDirty();
  3286. control.setValue(control._pendingValue, { emitModelToViewChange: false });
  3287. dir.viewToModelUpdate(control._pendingValue);
  3288. control._pendingChange = false;
  3289. }
  3290. function setUpModelChangePipeline(control, dir) {
  3291. const onChange = (newValue, emitModelEvent) => {
  3292. // control -> view
  3293. dir.valueAccessor.writeValue(newValue);
  3294. // control -> ngModel
  3295. if (emitModelEvent)
  3296. dir.viewToModelUpdate(newValue);
  3297. };
  3298. control.registerOnChange(onChange);
  3299. // Register a callback function to cleanup onChange handler
  3300. // from a control instance when a directive is destroyed.
  3301. dir._registerOnDestroy(() => {
  3302. control._unregisterOnChange(onChange);
  3303. });
  3304. }
  3305. /**
  3306. * Links a FormGroup or FormArray instance and corresponding Form directive by setting up validators
  3307. * present in the view.
  3308. *
  3309. * @param control FormGroup or FormArray instance that should be linked.
  3310. * @param dir Directive that provides view validators.
  3311. */
  3312. function setUpFormContainer(control, dir) {
  3313. if (control == null && (typeof ngDevMode === 'undefined' || ngDevMode))
  3314. _throwError(dir, 'Cannot find control with');
  3315. setUpValidators(control, dir);
  3316. }
  3317. /**
  3318. * Reverts the setup performed by the `setUpFormContainer` function.
  3319. *
  3320. * @param control FormGroup or FormArray instance that should be cleaned up.
  3321. * @param dir Directive that provided view validators.
  3322. * @returns true if a control was updated as a result of this action.
  3323. */
  3324. function cleanUpFormContainer(control, dir) {
  3325. return cleanUpValidators(control, dir);
  3326. }
  3327. function _noControlError(dir) {
  3328. return _throwError(dir, 'There is no FormControl instance attached to form control element with');
  3329. }
  3330. function _throwError(dir, message) {
  3331. const messageEnd = _describeControlLocation(dir);
  3332. throw new Error(`${message} ${messageEnd}`);
  3333. }
  3334. function _describeControlLocation(dir) {
  3335. const path = dir.path;
  3336. if (path && path.length > 1)
  3337. return `path: '${path.join(' -> ')}'`;
  3338. if (path?.[0])
  3339. return `name: '${path}'`;
  3340. return 'unspecified name attribute';
  3341. }
  3342. function _throwMissingValueAccessorError(dir) {
  3343. const loc = _describeControlLocation(dir);
  3344. throw new _RuntimeError(-1203 /* RuntimeErrorCode.NG_MISSING_VALUE_ACCESSOR */, `No value accessor for form control ${loc}.`);
  3345. }
  3346. function _throwInvalidValueAccessorError(dir) {
  3347. const loc = _describeControlLocation(dir);
  3348. throw new _RuntimeError(1200 /* RuntimeErrorCode.NG_VALUE_ACCESSOR_NOT_PROVIDED */, `Value accessor was not provided as an array for form control with ${loc}. ` +
  3349. `Check that the \`NG_VALUE_ACCESSOR\` token is configured as a \`multi: true\` provider.`);
  3350. }
  3351. function isPropertyUpdated(changes, viewModel) {
  3352. if (!changes.hasOwnProperty('model'))
  3353. return false;
  3354. const change = changes['model'];
  3355. if (change.isFirstChange())
  3356. return true;
  3357. return !Object.is(viewModel, change.currentValue);
  3358. }
  3359. function isBuiltInAccessor(valueAccessor) {
  3360. // Check if a given value accessor is an instance of a class that directly extends
  3361. // `BuiltInControlValueAccessor` one.
  3362. return Object.getPrototypeOf(valueAccessor.constructor) === BuiltInControlValueAccessor;
  3363. }
  3364. function syncPendingControls(form, directives) {
  3365. form._syncPendingControls();
  3366. directives.forEach((dir) => {
  3367. const control = dir.control;
  3368. if (control.updateOn === 'submit' && control._pendingChange) {
  3369. dir.viewToModelUpdate(control._pendingValue);
  3370. control._pendingChange = false;
  3371. }
  3372. });
  3373. }
  3374. // TODO: vsavkin remove it once https://github.com/angular/angular/issues/3011 is implemented
  3375. function selectValueAccessor(dir, valueAccessors) {
  3376. if (!valueAccessors)
  3377. return null;
  3378. if (!Array.isArray(valueAccessors) && (typeof ngDevMode === 'undefined' || ngDevMode))
  3379. _throwInvalidValueAccessorError(dir);
  3380. let defaultAccessor = undefined;
  3381. let builtinAccessor = undefined;
  3382. let customAccessor = undefined;
  3383. valueAccessors.forEach((v) => {
  3384. if (v.constructor === DefaultValueAccessor) {
  3385. defaultAccessor = v;
  3386. }
  3387. else if (isBuiltInAccessor(v)) {
  3388. if (builtinAccessor && (typeof ngDevMode === 'undefined' || ngDevMode))
  3389. _throwError(dir, 'More than one built-in value accessor matches form control with');
  3390. builtinAccessor = v;
  3391. }
  3392. else {
  3393. if (customAccessor && (typeof ngDevMode === 'undefined' || ngDevMode))
  3394. _throwError(dir, 'More than one custom value accessor matches form control with');
  3395. customAccessor = v;
  3396. }
  3397. });
  3398. if (customAccessor)
  3399. return customAccessor;
  3400. if (builtinAccessor)
  3401. return builtinAccessor;
  3402. if (defaultAccessor)
  3403. return defaultAccessor;
  3404. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  3405. _throwError(dir, 'No valid value accessor for form control with');
  3406. }
  3407. return null;
  3408. }
  3409. function removeListItem$1(list, el) {
  3410. const index = list.indexOf(el);
  3411. if (index > -1)
  3412. list.splice(index, 1);
  3413. }
  3414. // TODO(kara): remove after deprecation period
  3415. function _ngModelWarning(name, type, instance, warningConfig) {
  3416. if (warningConfig === 'never')
  3417. return;
  3418. if (((warningConfig === null || warningConfig === 'once') && !type._ngModelWarningSentOnce) ||
  3419. (warningConfig === 'always' && !instance._ngModelWarningSent)) {
  3420. console.warn(ngModelWarning(name));
  3421. type._ngModelWarningSentOnce = true;
  3422. instance._ngModelWarningSent = true;
  3423. }
  3424. }
  3425. const formDirectiveProvider$1 = {
  3426. provide: ControlContainer,
  3427. useExisting: forwardRef(() => NgForm),
  3428. };
  3429. const resolvedPromise$1 = (() => Promise.resolve())();
  3430. /**
  3431. * @description
  3432. * Creates a top-level `FormGroup` instance and binds it to a form
  3433. * to track aggregate form value and validation status.
  3434. *
  3435. * As soon as you import the `FormsModule`, this directive becomes active by default on
  3436. * all `<form>` tags. You don't need to add a special selector.
  3437. *
  3438. * You optionally export the directive into a local template variable using `ngForm` as the key
  3439. * (ex: `#myForm="ngForm"`). This is optional, but useful. Many properties from the underlying
  3440. * `FormGroup` instance are duplicated on the directive itself, so a reference to it
  3441. * gives you access to the aggregate value and validity status of the form, as well as
  3442. * user interaction properties like `dirty` and `touched`.
  3443. *
  3444. * To register child controls with the form, use `NgModel` with a `name`
  3445. * attribute. You may use `NgModelGroup` to create sub-groups within the form.
  3446. *
  3447. * If necessary, listen to the directive's `ngSubmit` event to be notified when the user has
  3448. * triggered a form submission. The `ngSubmit` event emits the original form
  3449. * submission event.
  3450. *
  3451. * In template driven forms, all `<form>` tags are automatically tagged as `NgForm`.
  3452. * To import the `FormsModule` but skip its usage in some forms,
  3453. * for example, to use native HTML5 validation, add the `ngNoForm` and the `<form>`
  3454. * tags won't create an `NgForm` directive. In reactive forms, using `ngNoForm` is
  3455. * unnecessary because the `<form>` tags are inert. In that case, you would
  3456. * refrain from using the `formGroup` directive.
  3457. *
  3458. * @usageNotes
  3459. *
  3460. * ### Listening for form submission
  3461. *
  3462. * The following example shows how to capture the form values from the "ngSubmit" event.
  3463. *
  3464. * {@example forms/ts/simpleForm/simple_form_example.ts region='Component'}
  3465. *
  3466. * ### Setting the update options
  3467. *
  3468. * The following example shows you how to change the "updateOn" option from its default using
  3469. * ngFormOptions.
  3470. *
  3471. * ```html
  3472. * <form [ngFormOptions]="{updateOn: 'blur'}">
  3473. * <input name="one" ngModel> <!-- this ngModel will update on blur -->
  3474. * </form>
  3475. * ```
  3476. *
  3477. * ### Native DOM validation UI
  3478. *
  3479. * In order to prevent the native DOM form validation UI from interfering with Angular's form
  3480. * validation, Angular automatically adds the `novalidate` attribute on any `<form>` whenever
  3481. * `FormModule` or `ReactiveFormModule` are imported into the application.
  3482. * If you want to explicitly enable native DOM validation UI with Angular forms, you can add the
  3483. * `ngNativeValidate` attribute to the `<form>` element:
  3484. *
  3485. * ```html
  3486. * <form ngNativeValidate>
  3487. * ...
  3488. * </form>
  3489. * ```
  3490. *
  3491. * @ngModule FormsModule
  3492. * @publicApi
  3493. */
  3494. class NgForm extends ControlContainer {
  3495. callSetDisabledState;
  3496. /**
  3497. * @description
  3498. * Returns whether the form submission has been triggered.
  3499. */
  3500. get submitted() {
  3501. return untracked(this.submittedReactive);
  3502. }
  3503. /** @internal */
  3504. _submitted = computed(() => this.submittedReactive());
  3505. submittedReactive = signal(false);
  3506. _directives = new Set();
  3507. /**
  3508. * @description
  3509. * The `FormGroup` instance created for this form.
  3510. */
  3511. form;
  3512. /**
  3513. * @description
  3514. * Event emitter for the "ngSubmit" event
  3515. */
  3516. ngSubmit = new EventEmitter();
  3517. /**
  3518. * @description
  3519. * Tracks options for the `NgForm` instance.
  3520. *
  3521. * **updateOn**: Sets the default `updateOn` value for all child `NgModels` below it
  3522. * unless explicitly set by a child `NgModel` using `ngModelOptions`). Defaults to 'change'.
  3523. * Possible values: `'change'` | `'blur'` | `'submit'`.
  3524. *
  3525. */
  3526. options;
  3527. constructor(validators, asyncValidators, callSetDisabledState) {
  3528. super();
  3529. this.callSetDisabledState = callSetDisabledState;
  3530. this.form = new FormGroup({}, composeValidators(validators), composeAsyncValidators(asyncValidators));
  3531. }
  3532. /** @docs-private */
  3533. ngAfterViewInit() {
  3534. this._setUpdateStrategy();
  3535. }
  3536. /**
  3537. * @description
  3538. * The directive instance.
  3539. */
  3540. get formDirective() {
  3541. return this;
  3542. }
  3543. /**
  3544. * @description
  3545. * The internal `FormGroup` instance.
  3546. */
  3547. get control() {
  3548. return this.form;
  3549. }
  3550. /**
  3551. * @description
  3552. * Returns an array representing the path to this group. Because this directive
  3553. * always lives at the top level of a form, it is always an empty array.
  3554. */
  3555. get path() {
  3556. return [];
  3557. }
  3558. /**
  3559. * @description
  3560. * Returns a map of the controls in this group.
  3561. */
  3562. get controls() {
  3563. return this.form.controls;
  3564. }
  3565. /**
  3566. * @description
  3567. * Method that sets up the control directive in this group, re-calculates its value
  3568. * and validity, and adds the instance to the internal list of directives.
  3569. *
  3570. * @param dir The `NgModel` directive instance.
  3571. */
  3572. addControl(dir) {
  3573. resolvedPromise$1.then(() => {
  3574. const container = this._findContainer(dir.path);
  3575. dir.control = (container.registerControl(dir.name, dir.control));
  3576. setUpControl(dir.control, dir, this.callSetDisabledState);
  3577. dir.control.updateValueAndValidity({ emitEvent: false });
  3578. this._directives.add(dir);
  3579. });
  3580. }
  3581. /**
  3582. * @description
  3583. * Retrieves the `FormControl` instance from the provided `NgModel` directive.
  3584. *
  3585. * @param dir The `NgModel` directive instance.
  3586. */
  3587. getControl(dir) {
  3588. return this.form.get(dir.path);
  3589. }
  3590. /**
  3591. * @description
  3592. * Removes the `NgModel` instance from the internal list of directives
  3593. *
  3594. * @param dir The `NgModel` directive instance.
  3595. */
  3596. removeControl(dir) {
  3597. resolvedPromise$1.then(() => {
  3598. const container = this._findContainer(dir.path);
  3599. if (container) {
  3600. container.removeControl(dir.name);
  3601. }
  3602. this._directives.delete(dir);
  3603. });
  3604. }
  3605. /**
  3606. * @description
  3607. * Adds a new `NgModelGroup` directive instance to the form.
  3608. *
  3609. * @param dir The `NgModelGroup` directive instance.
  3610. */
  3611. addFormGroup(dir) {
  3612. resolvedPromise$1.then(() => {
  3613. const container = this._findContainer(dir.path);
  3614. const group = new FormGroup({});
  3615. setUpFormContainer(group, dir);
  3616. container.registerControl(dir.name, group);
  3617. group.updateValueAndValidity({ emitEvent: false });
  3618. });
  3619. }
  3620. /**
  3621. * @description
  3622. * Removes the `NgModelGroup` directive instance from the form.
  3623. *
  3624. * @param dir The `NgModelGroup` directive instance.
  3625. */
  3626. removeFormGroup(dir) {
  3627. resolvedPromise$1.then(() => {
  3628. const container = this._findContainer(dir.path);
  3629. if (container) {
  3630. container.removeControl(dir.name);
  3631. }
  3632. });
  3633. }
  3634. /**
  3635. * @description
  3636. * Retrieves the `FormGroup` for a provided `NgModelGroup` directive instance
  3637. *
  3638. * @param dir The `NgModelGroup` directive instance.
  3639. */
  3640. getFormGroup(dir) {
  3641. return this.form.get(dir.path);
  3642. }
  3643. /**
  3644. * Sets the new value for the provided `NgControl` directive.
  3645. *
  3646. * @param dir The `NgControl` directive instance.
  3647. * @param value The new value for the directive's control.
  3648. */
  3649. updateModel(dir, value) {
  3650. resolvedPromise$1.then(() => {
  3651. const ctrl = this.form.get(dir.path);
  3652. ctrl.setValue(value);
  3653. });
  3654. }
  3655. /**
  3656. * @description
  3657. * Sets the value for this `FormGroup`.
  3658. *
  3659. * @param value The new value
  3660. */
  3661. setValue(value) {
  3662. this.control.setValue(value);
  3663. }
  3664. /**
  3665. * @description
  3666. * Method called when the "submit" event is triggered on the form.
  3667. * Triggers the `ngSubmit` emitter to emit the "submit" event as its payload.
  3668. *
  3669. * @param $event The "submit" event object
  3670. */
  3671. onSubmit($event) {
  3672. this.submittedReactive.set(true);
  3673. syncPendingControls(this.form, this._directives);
  3674. this.ngSubmit.emit($event);
  3675. this.form._events.next(new FormSubmittedEvent(this.control));
  3676. // Forms with `method="dialog"` have some special behavior
  3677. // that won't reload the page and that shouldn't be prevented.
  3678. return $event?.target?.method === 'dialog';
  3679. }
  3680. /**
  3681. * @description
  3682. * Method called when the "reset" event is triggered on the form.
  3683. */
  3684. onReset() {
  3685. this.resetForm();
  3686. }
  3687. /**
  3688. * @description
  3689. * Resets the form to an initial value and resets its submitted status.
  3690. *
  3691. * @param value The new value for the form.
  3692. */
  3693. resetForm(value = undefined) {
  3694. this.form.reset(value);
  3695. this.submittedReactive.set(false);
  3696. this.form._events.next(new FormResetEvent(this.form));
  3697. }
  3698. _setUpdateStrategy() {
  3699. if (this.options && this.options.updateOn != null) {
  3700. this.form._updateOn = this.options.updateOn;
  3701. }
  3702. }
  3703. _findContainer(path) {
  3704. path.pop();
  3705. return path.length ? this.form.get(path) : this.form;
  3706. }
  3707. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgForm, deps: [{ token: NG_VALIDATORS, optional: true, self: true }, { token: NG_ASYNC_VALIDATORS, optional: true, self: true }, { token: CALL_SET_DISABLED_STATE, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
  3708. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: NgForm, isStandalone: false, selector: "form:not([ngNoForm]):not([formGroup]),ng-form,[ngForm]", inputs: { options: ["ngFormOptions", "options"] }, outputs: { ngSubmit: "ngSubmit" }, host: { listeners: { "submit": "onSubmit($event)", "reset": "onReset()" } }, providers: [formDirectiveProvider$1], exportAs: ["ngForm"], usesInheritance: true, ngImport: i0 });
  3709. }
  3710. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgForm, decorators: [{
  3711. type: Directive,
  3712. args: [{
  3713. selector: 'form:not([ngNoForm]):not([formGroup]),ng-form,[ngForm]',
  3714. providers: [formDirectiveProvider$1],
  3715. host: { '(submit)': 'onSubmit($event)', '(reset)': 'onReset()' },
  3716. outputs: ['ngSubmit'],
  3717. exportAs: 'ngForm',
  3718. standalone: false,
  3719. }]
  3720. }], ctorParameters: () => [{ type: undefined, decorators: [{
  3721. type: Optional
  3722. }, {
  3723. type: Self
  3724. }, {
  3725. type: Inject,
  3726. args: [NG_VALIDATORS]
  3727. }] }, { type: undefined, decorators: [{
  3728. type: Optional
  3729. }, {
  3730. type: Self
  3731. }, {
  3732. type: Inject,
  3733. args: [NG_ASYNC_VALIDATORS]
  3734. }] }, { type: undefined, decorators: [{
  3735. type: Optional
  3736. }, {
  3737. type: Inject,
  3738. args: [CALL_SET_DISABLED_STATE]
  3739. }] }], propDecorators: { options: [{
  3740. type: Input,
  3741. args: ['ngFormOptions']
  3742. }] } });
  3743. function removeListItem(list, el) {
  3744. const index = list.indexOf(el);
  3745. if (index > -1)
  3746. list.splice(index, 1);
  3747. }
  3748. function isFormControlState(formState) {
  3749. return (typeof formState === 'object' &&
  3750. formState !== null &&
  3751. Object.keys(formState).length === 2 &&
  3752. 'value' in formState &&
  3753. 'disabled' in formState);
  3754. }
  3755. const FormControl = class FormControl extends AbstractControl {
  3756. /** @publicApi */
  3757. defaultValue = null;
  3758. /** @internal */
  3759. _onChange = [];
  3760. /** @internal */
  3761. _pendingValue;
  3762. /** @internal */
  3763. _pendingChange = false;
  3764. constructor(
  3765. // formState and defaultValue will only be null if T is nullable
  3766. formState = null, validatorOrOpts, asyncValidator) {
  3767. super(pickValidators(validatorOrOpts), pickAsyncValidators(asyncValidator, validatorOrOpts));
  3768. this._applyFormState(formState);
  3769. this._setUpdateStrategy(validatorOrOpts);
  3770. this._initObservables();
  3771. this.updateValueAndValidity({
  3772. onlySelf: true,
  3773. // If `asyncValidator` is present, it will trigger control status change from `PENDING` to
  3774. // `VALID` or `INVALID`.
  3775. // The status should be broadcasted via the `statusChanges` observable, so we set
  3776. // `emitEvent` to `true` to allow that during the control creation process.
  3777. emitEvent: !!this.asyncValidator,
  3778. });
  3779. if (isOptionsObj(validatorOrOpts) &&
  3780. (validatorOrOpts.nonNullable || validatorOrOpts.initialValueIsDefault)) {
  3781. if (isFormControlState(formState)) {
  3782. this.defaultValue = formState.value;
  3783. }
  3784. else {
  3785. this.defaultValue = formState;
  3786. }
  3787. }
  3788. }
  3789. setValue(value, options = {}) {
  3790. this.value = this._pendingValue = value;
  3791. if (this._onChange.length && options.emitModelToViewChange !== false) {
  3792. this._onChange.forEach((changeFn) => changeFn(this.value, options.emitViewToModelChange !== false));
  3793. }
  3794. this.updateValueAndValidity(options);
  3795. }
  3796. patchValue(value, options = {}) {
  3797. this.setValue(value, options);
  3798. }
  3799. reset(formState = this.defaultValue, options = {}) {
  3800. this._applyFormState(formState);
  3801. this.markAsPristine(options);
  3802. this.markAsUntouched(options);
  3803. this.setValue(this.value, options);
  3804. this._pendingChange = false;
  3805. }
  3806. /** @internal */
  3807. _updateValue() { }
  3808. /** @internal */
  3809. _anyControls(condition) {
  3810. return false;
  3811. }
  3812. /** @internal */
  3813. _allControlsDisabled() {
  3814. return this.disabled;
  3815. }
  3816. registerOnChange(fn) {
  3817. this._onChange.push(fn);
  3818. }
  3819. /** @internal */
  3820. _unregisterOnChange(fn) {
  3821. removeListItem(this._onChange, fn);
  3822. }
  3823. registerOnDisabledChange(fn) {
  3824. this._onDisabledChange.push(fn);
  3825. }
  3826. /** @internal */
  3827. _unregisterOnDisabledChange(fn) {
  3828. removeListItem(this._onDisabledChange, fn);
  3829. }
  3830. /** @internal */
  3831. _forEachChild(cb) { }
  3832. /** @internal */
  3833. _syncPendingControls() {
  3834. if (this.updateOn === 'submit') {
  3835. if (this._pendingDirty)
  3836. this.markAsDirty();
  3837. if (this._pendingTouched)
  3838. this.markAsTouched();
  3839. if (this._pendingChange) {
  3840. this.setValue(this._pendingValue, { onlySelf: true, emitModelToViewChange: false });
  3841. return true;
  3842. }
  3843. }
  3844. return false;
  3845. }
  3846. _applyFormState(formState) {
  3847. if (isFormControlState(formState)) {
  3848. this.value = this._pendingValue = formState.value;
  3849. formState.disabled
  3850. ? this.disable({ onlySelf: true, emitEvent: false })
  3851. : this.enable({ onlySelf: true, emitEvent: false });
  3852. }
  3853. else {
  3854. this.value = this._pendingValue = formState;
  3855. }
  3856. }
  3857. };
  3858. const UntypedFormControl = FormControl;
  3859. /**
  3860. * @description
  3861. * Asserts that the given control is an instance of `FormControl`
  3862. *
  3863. * @publicApi
  3864. */
  3865. const isFormControl = (control) => control instanceof FormControl;
  3866. /**
  3867. * @description
  3868. * A base class for code shared between the `NgModelGroup` and `FormGroupName` directives.
  3869. *
  3870. * @publicApi
  3871. */
  3872. class AbstractFormGroupDirective extends ControlContainer {
  3873. /**
  3874. * @description
  3875. * The parent control for the group
  3876. *
  3877. * @internal
  3878. */
  3879. _parent;
  3880. /** @docs-private */
  3881. ngOnInit() {
  3882. this._checkParentType();
  3883. // Register the group with its parent group.
  3884. this.formDirective.addFormGroup(this);
  3885. }
  3886. /** @docs-private */
  3887. ngOnDestroy() {
  3888. if (this.formDirective) {
  3889. // Remove the group from its parent group.
  3890. this.formDirective.removeFormGroup(this);
  3891. }
  3892. }
  3893. /**
  3894. * @description
  3895. * The `FormGroup` bound to this directive.
  3896. */
  3897. get control() {
  3898. return this.formDirective.getFormGroup(this);
  3899. }
  3900. /**
  3901. * @description
  3902. * The path to this group from the top-level directive.
  3903. */
  3904. get path() {
  3905. return controlPath(this.name == null ? this.name : this.name.toString(), this._parent);
  3906. }
  3907. /**
  3908. * @description
  3909. * The top-level directive for this group if present, otherwise null.
  3910. */
  3911. get formDirective() {
  3912. return this._parent ? this._parent.formDirective : null;
  3913. }
  3914. /** @internal */
  3915. _checkParentType() { }
  3916. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: AbstractFormGroupDirective, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  3917. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: AbstractFormGroupDirective, isStandalone: false, usesInheritance: true, ngImport: i0 });
  3918. }
  3919. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: AbstractFormGroupDirective, decorators: [{
  3920. type: Directive,
  3921. args: [{
  3922. standalone: false,
  3923. }]
  3924. }] });
  3925. function modelParentException() {
  3926. return new _RuntimeError(1350 /* RuntimeErrorCode.NGMODEL_IN_FORM_GROUP */, `
  3927. ngModel cannot be used to register form controls with a parent formGroup directive. Try using
  3928. formGroup's partner directive "formControlName" instead. Example:
  3929. ${formControlNameExample}
  3930. Or, if you'd like to avoid registering this form control, indicate that it's standalone in ngModelOptions:
  3931. Example:
  3932. ${ngModelWithFormGroupExample}`);
  3933. }
  3934. function formGroupNameException() {
  3935. return new _RuntimeError(1351 /* RuntimeErrorCode.NGMODEL_IN_FORM_GROUP_NAME */, `
  3936. ngModel cannot be used to register form controls with a parent formGroupName or formArrayName directive.
  3937. Option 1: Use formControlName instead of ngModel (reactive strategy):
  3938. ${formGroupNameExample}
  3939. Option 2: Update ngModel's parent be ngModelGroup (template-driven strategy):
  3940. ${ngModelGroupExample}`);
  3941. }
  3942. function missingNameException() {
  3943. return new _RuntimeError(1352 /* RuntimeErrorCode.NGMODEL_WITHOUT_NAME */, `If ngModel is used within a form tag, either the name attribute must be set or the form
  3944. control must be defined as 'standalone' in ngModelOptions.
  3945. Example 1: <input [(ngModel)]="person.firstName" name="first">
  3946. Example 2: <input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}">`);
  3947. }
  3948. function modelGroupParentException() {
  3949. return new _RuntimeError(1353 /* RuntimeErrorCode.NGMODELGROUP_IN_FORM_GROUP */, `
  3950. ngModelGroup cannot be used with a parent formGroup directive.
  3951. Option 1: Use formGroupName instead of ngModelGroup (reactive strategy):
  3952. ${formGroupNameExample}
  3953. Option 2: Use a regular form tag instead of the formGroup directive (template-driven strategy):
  3954. ${ngModelGroupExample}`);
  3955. }
  3956. const modelGroupProvider = {
  3957. provide: ControlContainer,
  3958. useExisting: forwardRef(() => NgModelGroup),
  3959. };
  3960. /**
  3961. * @description
  3962. * Creates and binds a `FormGroup` instance to a DOM element.
  3963. *
  3964. * This directive can only be used as a child of `NgForm` (within `<form>` tags).
  3965. *
  3966. * Use this directive to validate a sub-group of your form separately from the
  3967. * rest of your form, or if some values in your domain model make more sense
  3968. * to consume together in a nested object.
  3969. *
  3970. * Provide a name for the sub-group and it will become the key
  3971. * for the sub-group in the form's full value. If you need direct access, export the directive into
  3972. * a local template variable using `ngModelGroup` (ex: `#myGroup="ngModelGroup"`).
  3973. *
  3974. * @usageNotes
  3975. *
  3976. * ### Consuming controls in a grouping
  3977. *
  3978. * The following example shows you how to combine controls together in a sub-group
  3979. * of the form.
  3980. *
  3981. * {@example forms/ts/ngModelGroup/ng_model_group_example.ts region='Component'}
  3982. *
  3983. * @ngModule FormsModule
  3984. * @publicApi
  3985. */
  3986. class NgModelGroup extends AbstractFormGroupDirective {
  3987. /**
  3988. * @description
  3989. * Tracks the name of the `NgModelGroup` bound to the directive. The name corresponds
  3990. * to a key in the parent `NgForm`.
  3991. */
  3992. name = '';
  3993. constructor(parent, validators, asyncValidators) {
  3994. super();
  3995. this._parent = parent;
  3996. this._setValidators(validators);
  3997. this._setAsyncValidators(asyncValidators);
  3998. }
  3999. /** @internal */
  4000. _checkParentType() {
  4001. if (!(this._parent instanceof NgModelGroup) &&
  4002. !(this._parent instanceof NgForm) &&
  4003. (typeof ngDevMode === 'undefined' || ngDevMode)) {
  4004. throw modelGroupParentException();
  4005. }
  4006. }
  4007. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgModelGroup, deps: [{ token: ControlContainer, host: true, skipSelf: true }, { token: NG_VALIDATORS, optional: true, self: true }, { token: NG_ASYNC_VALIDATORS, optional: true, self: true }], target: i0.ɵɵFactoryTarget.Directive });
  4008. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: NgModelGroup, isStandalone: false, selector: "[ngModelGroup]", inputs: { name: ["ngModelGroup", "name"] }, providers: [modelGroupProvider], exportAs: ["ngModelGroup"], usesInheritance: true, ngImport: i0 });
  4009. }
  4010. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgModelGroup, decorators: [{
  4011. type: Directive,
  4012. args: [{
  4013. selector: '[ngModelGroup]',
  4014. providers: [modelGroupProvider],
  4015. exportAs: 'ngModelGroup',
  4016. standalone: false,
  4017. }]
  4018. }], ctorParameters: () => [{ type: ControlContainer, decorators: [{
  4019. type: Host
  4020. }, {
  4021. type: SkipSelf
  4022. }] }, { type: undefined, decorators: [{
  4023. type: Optional
  4024. }, {
  4025. type: Self
  4026. }, {
  4027. type: Inject,
  4028. args: [NG_VALIDATORS]
  4029. }] }, { type: undefined, decorators: [{
  4030. type: Optional
  4031. }, {
  4032. type: Self
  4033. }, {
  4034. type: Inject,
  4035. args: [NG_ASYNC_VALIDATORS]
  4036. }] }], propDecorators: { name: [{
  4037. type: Input,
  4038. args: ['ngModelGroup']
  4039. }] } });
  4040. const formControlBinding$1 = {
  4041. provide: NgControl,
  4042. useExisting: forwardRef(() => NgModel),
  4043. };
  4044. /**
  4045. * `ngModel` forces an additional change detection run when its inputs change:
  4046. * E.g.:
  4047. * ```html
  4048. * <div>{{myModel.valid}}</div>
  4049. * <input [(ngModel)]="myValue" #myModel="ngModel">
  4050. * ```
  4051. * I.e. `ngModel` can export itself on the element and then be used in the template.
  4052. * Normally, this would result in expressions before the `input` that use the exported directive
  4053. * to have an old value as they have been
  4054. * dirty checked before. As this is a very common case for `ngModel`, we added this second change
  4055. * detection run.
  4056. *
  4057. * Notes:
  4058. * - this is just one extra run no matter how many `ngModel`s have been changed.
  4059. * - this is a general problem when using `exportAs` for directives!
  4060. */
  4061. const resolvedPromise = (() => Promise.resolve())();
  4062. /**
  4063. * @description
  4064. * Creates a `FormControl` instance from a [domain
  4065. * model](https://en.wikipedia.org/wiki/Domain_model) and binds it to a form control element.
  4066. *
  4067. * The `FormControl` instance tracks the value, user interaction, and
  4068. * validation status of the control and keeps the view synced with the model. If used
  4069. * within a parent form, the directive also registers itself with the form as a child
  4070. * control.
  4071. *
  4072. * This directive is used by itself or as part of a larger form. Use the
  4073. * `ngModel` selector to activate it.
  4074. *
  4075. * It accepts a domain model as an optional `Input`. If you have a one-way binding
  4076. * to `ngModel` with `[]` syntax, changing the domain model's value in the component
  4077. * class sets the value in the view. If you have a two-way binding with `[()]` syntax
  4078. * (also known as 'banana-in-a-box syntax'), the value in the UI always syncs back to
  4079. * the domain model in your class.
  4080. *
  4081. * To inspect the properties of the associated `FormControl` (like the validity state),
  4082. * export the directive into a local template variable using `ngModel` as the key (ex:
  4083. * `#myVar="ngModel"`). You can then access the control using the directive's `control` property.
  4084. * However, the most commonly used properties (like `valid` and `dirty`) also exist on the control
  4085. * for direct access. See a full list of properties directly available in
  4086. * `AbstractControlDirective`.
  4087. *
  4088. * @see {@link RadioControlValueAccessor}
  4089. * @see {@link SelectControlValueAccessor}
  4090. *
  4091. * @usageNotes
  4092. *
  4093. * ### Using ngModel on a standalone control
  4094. *
  4095. * The following examples show a simple standalone control using `ngModel`:
  4096. *
  4097. * {@example forms/ts/simpleNgModel/simple_ng_model_example.ts region='Component'}
  4098. *
  4099. * When using the `ngModel` within `<form>` tags, you'll also need to supply a `name` attribute
  4100. * so that the control can be registered with the parent form under that name.
  4101. *
  4102. * In the context of a parent form, it's often unnecessary to include one-way or two-way binding,
  4103. * as the parent form syncs the value for you. You access its properties by exporting it into a
  4104. * local template variable using `ngForm` such as (`#f="ngForm"`). Use the variable where
  4105. * needed on form submission.
  4106. *
  4107. * If you do need to populate initial values into your form, using a one-way binding for
  4108. * `ngModel` tends to be sufficient as long as you use the exported form's value rather
  4109. * than the domain model's value on submit.
  4110. *
  4111. * ### Using ngModel within a form
  4112. *
  4113. * The following example shows controls using `ngModel` within a form:
  4114. *
  4115. * {@example forms/ts/simpleForm/simple_form_example.ts region='Component'}
  4116. *
  4117. * ### Using a standalone ngModel within a group
  4118. *
  4119. * The following example shows you how to use a standalone ngModel control
  4120. * within a form. This controls the display of the form, but doesn't contain form data.
  4121. *
  4122. * ```html
  4123. * <form>
  4124. * <input name="login" ngModel placeholder="Login">
  4125. * <input type="checkbox" ngModel [ngModelOptions]="{standalone: true}"> Show more options?
  4126. * </form>
  4127. * <!-- form value: {login: ''} -->
  4128. * ```
  4129. *
  4130. * ### Setting the ngModel `name` attribute through options
  4131. *
  4132. * The following example shows you an alternate way to set the name attribute. Here,
  4133. * an attribute identified as name is used within a custom form control component. To still be able
  4134. * to specify the NgModel's name, you must specify it using the `ngModelOptions` input instead.
  4135. *
  4136. * ```html
  4137. * <form>
  4138. * <my-custom-form-control name="Nancy" ngModel [ngModelOptions]="{name: 'user'}">
  4139. * </my-custom-form-control>
  4140. * </form>
  4141. * <!-- form value: {user: ''} -->
  4142. * ```
  4143. *
  4144. * @ngModule FormsModule
  4145. * @publicApi
  4146. */
  4147. class NgModel extends NgControl {
  4148. _changeDetectorRef;
  4149. callSetDisabledState;
  4150. control = new FormControl();
  4151. // At runtime we coerce arbitrary values assigned to the "disabled" input to a "boolean".
  4152. // This is not reflected in the type of the property because outside of templates, consumers
  4153. // should only deal with booleans. In templates, a string is allowed for convenience and to
  4154. // match the native "disabled attribute" semantics which can be observed on input elements.
  4155. // This static member tells the compiler that values of type "string" can also be assigned
  4156. // to the input in a template.
  4157. /** @docs-private */
  4158. static ngAcceptInputType_isDisabled;
  4159. /** @internal */
  4160. _registered = false;
  4161. /**
  4162. * Internal reference to the view model value.
  4163. * @docs-private
  4164. */
  4165. viewModel;
  4166. /**
  4167. * @description
  4168. * Tracks the name bound to the directive. If a parent form exists, it
  4169. * uses this name as a key to retrieve this control's value.
  4170. */
  4171. name = '';
  4172. /**
  4173. * @description
  4174. * Tracks whether the control is disabled.
  4175. */
  4176. isDisabled;
  4177. /**
  4178. * @description
  4179. * Tracks the value bound to this directive.
  4180. */
  4181. model;
  4182. /**
  4183. * @description
  4184. * Tracks the configuration options for this `ngModel` instance.
  4185. *
  4186. * **name**: An alternative to setting the name attribute on the form control element. See
  4187. * the [example](api/forms/NgModel#using-ngmodel-on-a-standalone-control) for using `NgModel`
  4188. * as a standalone control.
  4189. *
  4190. * **standalone**: When set to true, the `ngModel` will not register itself with its parent form,
  4191. * and acts as if it's not in the form. Defaults to false. If no parent form exists, this option
  4192. * has no effect.
  4193. *
  4194. * **updateOn**: Defines the event upon which the form control value and validity update.
  4195. * Defaults to 'change'. Possible values: `'change'` | `'blur'` | `'submit'`.
  4196. *
  4197. */
  4198. options;
  4199. /**
  4200. * @description
  4201. * Event emitter for producing the `ngModelChange` event after
  4202. * the view model updates.
  4203. */
  4204. update = new EventEmitter();
  4205. constructor(parent, validators, asyncValidators, valueAccessors, _changeDetectorRef, callSetDisabledState) {
  4206. super();
  4207. this._changeDetectorRef = _changeDetectorRef;
  4208. this.callSetDisabledState = callSetDisabledState;
  4209. this._parent = parent;
  4210. this._setValidators(validators);
  4211. this._setAsyncValidators(asyncValidators);
  4212. this.valueAccessor = selectValueAccessor(this, valueAccessors);
  4213. }
  4214. /** @docs-private */
  4215. ngOnChanges(changes) {
  4216. this._checkForErrors();
  4217. if (!this._registered || 'name' in changes) {
  4218. if (this._registered) {
  4219. this._checkName();
  4220. if (this.formDirective) {
  4221. // We can't call `formDirective.removeControl(this)`, because the `name` has already been
  4222. // changed. We also can't reset the name temporarily since the logic in `removeControl`
  4223. // is inside a promise and it won't run immediately. We work around it by giving it an
  4224. // object with the same shape instead.
  4225. const oldName = changes['name'].previousValue;
  4226. this.formDirective.removeControl({ name: oldName, path: this._getPath(oldName) });
  4227. }
  4228. }
  4229. this._setUpControl();
  4230. }
  4231. if ('isDisabled' in changes) {
  4232. this._updateDisabled(changes);
  4233. }
  4234. if (isPropertyUpdated(changes, this.viewModel)) {
  4235. this._updateValue(this.model);
  4236. this.viewModel = this.model;
  4237. }
  4238. }
  4239. /** @docs-private */
  4240. ngOnDestroy() {
  4241. this.formDirective && this.formDirective.removeControl(this);
  4242. }
  4243. /**
  4244. * @description
  4245. * Returns an array that represents the path from the top-level form to this control.
  4246. * Each index is the string name of the control on that level.
  4247. */
  4248. get path() {
  4249. return this._getPath(this.name);
  4250. }
  4251. /**
  4252. * @description
  4253. * The top-level directive for this control if present, otherwise null.
  4254. */
  4255. get formDirective() {
  4256. return this._parent ? this._parent.formDirective : null;
  4257. }
  4258. /**
  4259. * @description
  4260. * Sets the new value for the view model and emits an `ngModelChange` event.
  4261. *
  4262. * @param newValue The new value emitted by `ngModelChange`.
  4263. */
  4264. viewToModelUpdate(newValue) {
  4265. this.viewModel = newValue;
  4266. this.update.emit(newValue);
  4267. }
  4268. _setUpControl() {
  4269. this._setUpdateStrategy();
  4270. this._isStandalone() ? this._setUpStandalone() : this.formDirective.addControl(this);
  4271. this._registered = true;
  4272. }
  4273. _setUpdateStrategy() {
  4274. if (this.options && this.options.updateOn != null) {
  4275. this.control._updateOn = this.options.updateOn;
  4276. }
  4277. }
  4278. _isStandalone() {
  4279. return !this._parent || !!(this.options && this.options.standalone);
  4280. }
  4281. _setUpStandalone() {
  4282. setUpControl(this.control, this, this.callSetDisabledState);
  4283. this.control.updateValueAndValidity({ emitEvent: false });
  4284. }
  4285. _checkForErrors() {
  4286. if ((typeof ngDevMode === 'undefined' || ngDevMode) && !this._isStandalone()) {
  4287. checkParentType$1(this._parent);
  4288. }
  4289. this._checkName();
  4290. }
  4291. _checkName() {
  4292. if (this.options && this.options.name)
  4293. this.name = this.options.name;
  4294. if (!this._isStandalone() && !this.name && (typeof ngDevMode === 'undefined' || ngDevMode)) {
  4295. throw missingNameException();
  4296. }
  4297. }
  4298. _updateValue(value) {
  4299. resolvedPromise.then(() => {
  4300. this.control.setValue(value, { emitViewToModelChange: false });
  4301. this._changeDetectorRef?.markForCheck();
  4302. });
  4303. }
  4304. _updateDisabled(changes) {
  4305. const disabledValue = changes['isDisabled'].currentValue;
  4306. // checking for 0 to avoid breaking change
  4307. const isDisabled = disabledValue !== 0 && booleanAttribute(disabledValue);
  4308. resolvedPromise.then(() => {
  4309. if (isDisabled && !this.control.disabled) {
  4310. this.control.disable();
  4311. }
  4312. else if (!isDisabled && this.control.disabled) {
  4313. this.control.enable();
  4314. }
  4315. this._changeDetectorRef?.markForCheck();
  4316. });
  4317. }
  4318. _getPath(controlName) {
  4319. return this._parent ? controlPath(controlName, this._parent) : [controlName];
  4320. }
  4321. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgModel, deps: [{ token: ControlContainer, host: true, optional: true }, { token: NG_VALIDATORS, optional: true, self: true }, { token: NG_ASYNC_VALIDATORS, optional: true, self: true }, { token: NG_VALUE_ACCESSOR, optional: true, self: true }, { token: ChangeDetectorRef, optional: true }, { token: CALL_SET_DISABLED_STATE, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
  4322. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: NgModel, isStandalone: false, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: { name: "name", isDisabled: ["disabled", "isDisabled"], model: ["ngModel", "model"], options: ["ngModelOptions", "options"] }, outputs: { update: "ngModelChange" }, providers: [formControlBinding$1], exportAs: ["ngModel"], usesInheritance: true, usesOnChanges: true, ngImport: i0 });
  4323. }
  4324. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgModel, decorators: [{
  4325. type: Directive,
  4326. args: [{
  4327. selector: '[ngModel]:not([formControlName]):not([formControl])',
  4328. providers: [formControlBinding$1],
  4329. exportAs: 'ngModel',
  4330. standalone: false,
  4331. }]
  4332. }], ctorParameters: () => [{ type: ControlContainer, decorators: [{
  4333. type: Optional
  4334. }, {
  4335. type: Host
  4336. }] }, { type: undefined, decorators: [{
  4337. type: Optional
  4338. }, {
  4339. type: Self
  4340. }, {
  4341. type: Inject,
  4342. args: [NG_VALIDATORS]
  4343. }] }, { type: undefined, decorators: [{
  4344. type: Optional
  4345. }, {
  4346. type: Self
  4347. }, {
  4348. type: Inject,
  4349. args: [NG_ASYNC_VALIDATORS]
  4350. }] }, { type: undefined, decorators: [{
  4351. type: Optional
  4352. }, {
  4353. type: Self
  4354. }, {
  4355. type: Inject,
  4356. args: [NG_VALUE_ACCESSOR]
  4357. }] }, { type: i0.ChangeDetectorRef, decorators: [{
  4358. type: Optional
  4359. }, {
  4360. type: Inject,
  4361. args: [ChangeDetectorRef]
  4362. }] }, { type: undefined, decorators: [{
  4363. type: Optional
  4364. }, {
  4365. type: Inject,
  4366. args: [CALL_SET_DISABLED_STATE]
  4367. }] }], propDecorators: { name: [{
  4368. type: Input
  4369. }], isDisabled: [{
  4370. type: Input,
  4371. args: ['disabled']
  4372. }], model: [{
  4373. type: Input,
  4374. args: ['ngModel']
  4375. }], options: [{
  4376. type: Input,
  4377. args: ['ngModelOptions']
  4378. }], update: [{
  4379. type: Output,
  4380. args: ['ngModelChange']
  4381. }] } });
  4382. function checkParentType$1(parent) {
  4383. if (!(parent instanceof NgModelGroup) && parent instanceof AbstractFormGroupDirective) {
  4384. throw formGroupNameException();
  4385. }
  4386. else if (!(parent instanceof NgModelGroup) && !(parent instanceof NgForm)) {
  4387. throw modelParentException();
  4388. }
  4389. }
  4390. /**
  4391. * @description
  4392. *
  4393. * Adds `novalidate` attribute to all forms by default.
  4394. *
  4395. * `novalidate` is used to disable browser's native form validation.
  4396. *
  4397. * If you want to use native validation with Angular forms, just add `ngNativeValidate` attribute:
  4398. *
  4399. * ```html
  4400. * <form ngNativeValidate></form>
  4401. * ```
  4402. *
  4403. * @publicApi
  4404. * @ngModule ReactiveFormsModule
  4405. * @ngModule FormsModule
  4406. */
  4407. class ɵNgNoValidate {
  4408. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: ɵNgNoValidate, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  4409. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: ɵNgNoValidate, isStandalone: false, selector: "form:not([ngNoForm]):not([ngNativeValidate])", host: { attributes: { "novalidate": "" } }, ngImport: i0 });
  4410. }
  4411. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: ɵNgNoValidate, decorators: [{
  4412. type: Directive,
  4413. args: [{
  4414. selector: 'form:not([ngNoForm]):not([ngNativeValidate])',
  4415. host: { 'novalidate': '' },
  4416. standalone: false,
  4417. }]
  4418. }] });
  4419. const NUMBER_VALUE_ACCESSOR = {
  4420. provide: NG_VALUE_ACCESSOR,
  4421. useExisting: forwardRef(() => NumberValueAccessor),
  4422. multi: true,
  4423. };
  4424. /**
  4425. * @description
  4426. * The `ControlValueAccessor` for writing a number value and listening to number input changes.
  4427. * The value accessor is used by the `FormControlDirective`, `FormControlName`, and `NgModel`
  4428. * directives.
  4429. *
  4430. * @usageNotes
  4431. *
  4432. * ### Using a number input with a reactive form.
  4433. *
  4434. * The following example shows how to use a number input with a reactive form.
  4435. *
  4436. * ```ts
  4437. * const totalCountControl = new FormControl();
  4438. * ```
  4439. *
  4440. * ```html
  4441. * <input type="number" [formControl]="totalCountControl">
  4442. * ```
  4443. *
  4444. * @ngModule ReactiveFormsModule
  4445. * @ngModule FormsModule
  4446. * @publicApi
  4447. */
  4448. class NumberValueAccessor extends BuiltInControlValueAccessor {
  4449. /**
  4450. * Sets the "value" property on the input element.
  4451. * @docs-private
  4452. */
  4453. writeValue(value) {
  4454. // The value needs to be normalized for IE9, otherwise it is set to 'null' when null
  4455. const normalizedValue = value == null ? '' : value;
  4456. this.setProperty('value', normalizedValue);
  4457. }
  4458. /**
  4459. * Registers a function called when the control value changes.
  4460. * @docs-private
  4461. */
  4462. registerOnChange(fn) {
  4463. this.onChange = (value) => {
  4464. fn(value == '' ? null : parseFloat(value));
  4465. };
  4466. }
  4467. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NumberValueAccessor, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  4468. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: NumberValueAccessor, isStandalone: false, selector: "input[type=number][formControlName],input[type=number][formControl],input[type=number][ngModel]", host: { listeners: { "input": "onChange($event.target.value)", "blur": "onTouched()" } }, providers: [NUMBER_VALUE_ACCESSOR], usesInheritance: true, ngImport: i0 });
  4469. }
  4470. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NumberValueAccessor, decorators: [{
  4471. type: Directive,
  4472. args: [{
  4473. selector: 'input[type=number][formControlName],input[type=number][formControl],input[type=number][ngModel]',
  4474. host: { '(input)': 'onChange($event.target.value)', '(blur)': 'onTouched()' },
  4475. providers: [NUMBER_VALUE_ACCESSOR],
  4476. standalone: false,
  4477. }]
  4478. }] });
  4479. const RADIO_VALUE_ACCESSOR = {
  4480. provide: NG_VALUE_ACCESSOR,
  4481. useExisting: forwardRef(() => RadioControlValueAccessor),
  4482. multi: true,
  4483. };
  4484. function throwNameError() {
  4485. throw new _RuntimeError(1202 /* RuntimeErrorCode.NAME_AND_FORM_CONTROL_NAME_MUST_MATCH */, `
  4486. If you define both a name and a formControlName attribute on your radio button, their values
  4487. must match. Ex: <input type="radio" formControlName="food" name="food">
  4488. `);
  4489. }
  4490. /**
  4491. * @description
  4492. * Class used by Angular to track radio buttons. For internal use only.
  4493. */
  4494. class RadioControlRegistry {
  4495. _accessors = [];
  4496. /**
  4497. * @description
  4498. * Adds a control to the internal registry. For internal use only.
  4499. */
  4500. add(control, accessor) {
  4501. this._accessors.push([control, accessor]);
  4502. }
  4503. /**
  4504. * @description
  4505. * Removes a control from the internal registry. For internal use only.
  4506. */
  4507. remove(accessor) {
  4508. for (let i = this._accessors.length - 1; i >= 0; --i) {
  4509. if (this._accessors[i][1] === accessor) {
  4510. this._accessors.splice(i, 1);
  4511. return;
  4512. }
  4513. }
  4514. }
  4515. /**
  4516. * @description
  4517. * Selects a radio button. For internal use only.
  4518. */
  4519. select(accessor) {
  4520. this._accessors.forEach((c) => {
  4521. if (this._isSameGroup(c, accessor) && c[1] !== accessor) {
  4522. c[1].fireUncheck(accessor.value);
  4523. }
  4524. });
  4525. }
  4526. _isSameGroup(controlPair, accessor) {
  4527. if (!controlPair[0].control)
  4528. return false;
  4529. return (controlPair[0]._parent === accessor._control._parent && controlPair[1].name === accessor.name);
  4530. }
  4531. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: RadioControlRegistry, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  4532. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: RadioControlRegistry, providedIn: 'root' });
  4533. }
  4534. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: RadioControlRegistry, decorators: [{
  4535. type: Injectable,
  4536. args: [{ providedIn: 'root' }]
  4537. }] });
  4538. /**
  4539. * @description
  4540. * The `ControlValueAccessor` for writing radio control values and listening to radio control
  4541. * changes. The value accessor is used by the `FormControlDirective`, `FormControlName`, and
  4542. * `NgModel` directives.
  4543. *
  4544. * @usageNotes
  4545. *
  4546. * ### Using radio buttons with reactive form directives
  4547. *
  4548. * The follow example shows how to use radio buttons in a reactive form. When using radio buttons in
  4549. * a reactive form, radio buttons in the same group should have the same `formControlName`.
  4550. * Providing a `name` attribute is optional.
  4551. *
  4552. * {@example forms/ts/reactiveRadioButtons/reactive_radio_button_example.ts region='Reactive'}
  4553. *
  4554. * @ngModule ReactiveFormsModule
  4555. * @ngModule FormsModule
  4556. * @publicApi
  4557. */
  4558. class RadioControlValueAccessor extends BuiltInControlValueAccessor {
  4559. _registry;
  4560. _injector;
  4561. /** @internal */
  4562. _state;
  4563. /** @internal */
  4564. _control;
  4565. /** @internal */
  4566. _fn;
  4567. setDisabledStateFired = false;
  4568. /**
  4569. * The registered callback function called when a change event occurs on the input element.
  4570. * Note: we declare `onChange` here (also used as host listener) as a function with no arguments
  4571. * to override the `onChange` function (which expects 1 argument) in the parent
  4572. * `BaseControlValueAccessor` class.
  4573. * @docs-private
  4574. */
  4575. onChange = () => { };
  4576. /**
  4577. * @description
  4578. * Tracks the name of the radio input element.
  4579. */
  4580. name;
  4581. /**
  4582. * @description
  4583. * Tracks the name of the `FormControl` bound to the directive. The name corresponds
  4584. * to a key in the parent `FormGroup` or `FormArray`.
  4585. */
  4586. formControlName;
  4587. /**
  4588. * @description
  4589. * Tracks the value of the radio input element
  4590. */
  4591. value;
  4592. callSetDisabledState = inject(CALL_SET_DISABLED_STATE, { optional: true }) ?? setDisabledStateDefault;
  4593. constructor(renderer, elementRef, _registry, _injector) {
  4594. super(renderer, elementRef);
  4595. this._registry = _registry;
  4596. this._injector = _injector;
  4597. }
  4598. /** @docs-private */
  4599. ngOnInit() {
  4600. this._control = this._injector.get(NgControl);
  4601. this._checkName();
  4602. this._registry.add(this._control, this);
  4603. }
  4604. /** @docs-private */
  4605. ngOnDestroy() {
  4606. this._registry.remove(this);
  4607. }
  4608. /**
  4609. * Sets the "checked" property value on the radio input element.
  4610. * @docs-private
  4611. */
  4612. writeValue(value) {
  4613. this._state = value === this.value;
  4614. this.setProperty('checked', this._state);
  4615. }
  4616. /**
  4617. * Registers a function called when the control value changes.
  4618. * @docs-private
  4619. */
  4620. registerOnChange(fn) {
  4621. this._fn = fn;
  4622. this.onChange = () => {
  4623. fn(this.value);
  4624. this._registry.select(this);
  4625. };
  4626. }
  4627. /** @docs-private */
  4628. setDisabledState(isDisabled) {
  4629. /**
  4630. * `setDisabledState` is supposed to be called whenever the disabled state of a control changes,
  4631. * including upon control creation. However, a longstanding bug caused the method to not fire
  4632. * when an *enabled* control was attached. This bug was fixed in v15 in #47576.
  4633. *
  4634. * This had a side effect: previously, it was possible to instantiate a reactive form control
  4635. * with `[attr.disabled]=true`, even though the corresponding control was enabled in the
  4636. * model. This resulted in a mismatch between the model and the DOM. Now, because
  4637. * `setDisabledState` is always called, the value in the DOM will be immediately overwritten
  4638. * with the "correct" enabled value.
  4639. *
  4640. * However, the fix also created an exceptional case: radio buttons. Because Reactive Forms
  4641. * models the entire group of radio buttons as a single `FormControl`, there is no way to
  4642. * control the disabled state for individual radios, so they can no longer be configured as
  4643. * disabled. Thus, we keep the old behavior for radio buttons, so that `[attr.disabled]`
  4644. * continues to work. Specifically, we drop the first call to `setDisabledState` if `disabled`
  4645. * is `false`, and we are not in legacy mode.
  4646. */
  4647. if (this.setDisabledStateFired ||
  4648. isDisabled ||
  4649. this.callSetDisabledState === 'whenDisabledForLegacyCode') {
  4650. this.setProperty('disabled', isDisabled);
  4651. }
  4652. this.setDisabledStateFired = true;
  4653. }
  4654. /**
  4655. * Sets the "value" on the radio input element and unchecks it.
  4656. *
  4657. * @param value
  4658. */
  4659. fireUncheck(value) {
  4660. this.writeValue(value);
  4661. }
  4662. _checkName() {
  4663. if (this.name &&
  4664. this.formControlName &&
  4665. this.name !== this.formControlName &&
  4666. (typeof ngDevMode === 'undefined' || ngDevMode)) {
  4667. throwNameError();
  4668. }
  4669. if (!this.name && this.formControlName)
  4670. this.name = this.formControlName;
  4671. }
  4672. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: RadioControlValueAccessor, deps: [{ token: i0.Renderer2 }, { token: i0.ElementRef }, { token: RadioControlRegistry }, { token: i0.Injector }], target: i0.ɵɵFactoryTarget.Directive });
  4673. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: RadioControlValueAccessor, isStandalone: false, selector: "input[type=radio][formControlName],input[type=radio][formControl],input[type=radio][ngModel]", inputs: { name: "name", formControlName: "formControlName", value: "value" }, host: { listeners: { "change": "onChange()", "blur": "onTouched()" } }, providers: [RADIO_VALUE_ACCESSOR], usesInheritance: true, ngImport: i0 });
  4674. }
  4675. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: RadioControlValueAccessor, decorators: [{
  4676. type: Directive,
  4677. args: [{
  4678. selector: 'input[type=radio][formControlName],input[type=radio][formControl],input[type=radio][ngModel]',
  4679. host: { '(change)': 'onChange()', '(blur)': 'onTouched()' },
  4680. providers: [RADIO_VALUE_ACCESSOR],
  4681. standalone: false,
  4682. }]
  4683. }], ctorParameters: () => [{ type: i0.Renderer2 }, { type: i0.ElementRef }, { type: RadioControlRegistry }, { type: i0.Injector }], propDecorators: { name: [{
  4684. type: Input
  4685. }], formControlName: [{
  4686. type: Input
  4687. }], value: [{
  4688. type: Input
  4689. }] } });
  4690. const RANGE_VALUE_ACCESSOR = {
  4691. provide: NG_VALUE_ACCESSOR,
  4692. useExisting: forwardRef(() => RangeValueAccessor),
  4693. multi: true,
  4694. };
  4695. /**
  4696. * @description
  4697. * The `ControlValueAccessor` for writing a range value and listening to range input changes.
  4698. * The value accessor is used by the `FormControlDirective`, `FormControlName`, and `NgModel`
  4699. * directives.
  4700. *
  4701. * @usageNotes
  4702. *
  4703. * ### Using a range input with a reactive form
  4704. *
  4705. * The following example shows how to use a range input with a reactive form.
  4706. *
  4707. * ```ts
  4708. * const ageControl = new FormControl();
  4709. * ```
  4710. *
  4711. * ```html
  4712. * <input type="range" [formControl]="ageControl">
  4713. * ```
  4714. *
  4715. * @ngModule ReactiveFormsModule
  4716. * @ngModule FormsModule
  4717. * @publicApi
  4718. */
  4719. class RangeValueAccessor extends BuiltInControlValueAccessor {
  4720. /**
  4721. * Sets the "value" property on the input element.
  4722. * @docs-private
  4723. */
  4724. writeValue(value) {
  4725. this.setProperty('value', parseFloat(value));
  4726. }
  4727. /**
  4728. * Registers a function called when the control value changes.
  4729. * @docs-private
  4730. */
  4731. registerOnChange(fn) {
  4732. this.onChange = (value) => {
  4733. fn(value == '' ? null : parseFloat(value));
  4734. };
  4735. }
  4736. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: RangeValueAccessor, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  4737. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: RangeValueAccessor, isStandalone: false, selector: "input[type=range][formControlName],input[type=range][formControl],input[type=range][ngModel]", host: { listeners: { "change": "onChange($event.target.value)", "input": "onChange($event.target.value)", "blur": "onTouched()" } }, providers: [RANGE_VALUE_ACCESSOR], usesInheritance: true, ngImport: i0 });
  4738. }
  4739. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: RangeValueAccessor, decorators: [{
  4740. type: Directive,
  4741. args: [{
  4742. selector: 'input[type=range][formControlName],input[type=range][formControl],input[type=range][ngModel]',
  4743. host: {
  4744. '(change)': 'onChange($event.target.value)',
  4745. '(input)': 'onChange($event.target.value)',
  4746. '(blur)': 'onTouched()',
  4747. },
  4748. providers: [RANGE_VALUE_ACCESSOR],
  4749. standalone: false,
  4750. }]
  4751. }] });
  4752. /**
  4753. * Token to provide to turn off the ngModel warning on formControl and formControlName.
  4754. */
  4755. const NG_MODEL_WITH_FORM_CONTROL_WARNING = new InjectionToken(ngDevMode ? 'NgModelWithFormControlWarning' : '');
  4756. const formControlBinding = {
  4757. provide: NgControl,
  4758. useExisting: forwardRef(() => FormControlDirective),
  4759. };
  4760. /**
  4761. * @description
  4762. * Synchronizes a standalone `FormControl` instance to a form control element.
  4763. *
  4764. * Note that support for using the `ngModel` input property and `ngModelChange` event with reactive
  4765. * form directives was deprecated in Angular v6 and is scheduled for removal in
  4766. * a future version of Angular.
  4767. *
  4768. * @see [Reactive Forms Guide](guide/forms/reactive-forms)
  4769. * @see {@link FormControl}
  4770. * @see {@link AbstractControl}
  4771. *
  4772. * @usageNotes
  4773. *
  4774. * The following example shows how to register a standalone control and set its value.
  4775. *
  4776. * {@example forms/ts/simpleFormControl/simple_form_control_example.ts region='Component'}
  4777. *
  4778. * @ngModule ReactiveFormsModule
  4779. * @publicApi
  4780. */
  4781. class FormControlDirective extends NgControl {
  4782. _ngModelWarningConfig;
  4783. callSetDisabledState;
  4784. /**
  4785. * Internal reference to the view model value.
  4786. * @docs-private
  4787. */
  4788. viewModel;
  4789. /**
  4790. * @description
  4791. * Tracks the `FormControl` instance bound to the directive.
  4792. */
  4793. form;
  4794. /**
  4795. * @description
  4796. * Triggers a warning in dev mode that this input should not be used with reactive forms.
  4797. */
  4798. set isDisabled(isDisabled) {
  4799. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  4800. console.warn(disabledAttrWarning);
  4801. }
  4802. }
  4803. // TODO(kara): remove next 4 properties once deprecation period is over
  4804. /** @deprecated as of v6 */
  4805. model;
  4806. /** @deprecated as of v6 */
  4807. update = new EventEmitter();
  4808. /**
  4809. * @description
  4810. * Static property used to track whether any ngModel warnings have been sent across
  4811. * all instances of FormControlDirective. Used to support warning config of "once".
  4812. *
  4813. * @internal
  4814. */
  4815. static _ngModelWarningSentOnce = false;
  4816. /**
  4817. * @description
  4818. * Instance property used to track whether an ngModel warning has been sent out for this
  4819. * particular `FormControlDirective` instance. Used to support warning config of "always".
  4820. *
  4821. * @internal
  4822. */
  4823. _ngModelWarningSent = false;
  4824. constructor(validators, asyncValidators, valueAccessors, _ngModelWarningConfig, callSetDisabledState) {
  4825. super();
  4826. this._ngModelWarningConfig = _ngModelWarningConfig;
  4827. this.callSetDisabledState = callSetDisabledState;
  4828. this._setValidators(validators);
  4829. this._setAsyncValidators(asyncValidators);
  4830. this.valueAccessor = selectValueAccessor(this, valueAccessors);
  4831. }
  4832. /** @docs-private */
  4833. ngOnChanges(changes) {
  4834. if (this._isControlChanged(changes)) {
  4835. const previousForm = changes['form'].previousValue;
  4836. if (previousForm) {
  4837. cleanUpControl(previousForm, this, /* validateControlPresenceOnChange */ false);
  4838. }
  4839. setUpControl(this.form, this, this.callSetDisabledState);
  4840. this.form.updateValueAndValidity({ emitEvent: false });
  4841. }
  4842. if (isPropertyUpdated(changes, this.viewModel)) {
  4843. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  4844. _ngModelWarning('formControl', FormControlDirective, this, this._ngModelWarningConfig);
  4845. }
  4846. this.form.setValue(this.model);
  4847. this.viewModel = this.model;
  4848. }
  4849. }
  4850. /** @docs-private */
  4851. ngOnDestroy() {
  4852. if (this.form) {
  4853. cleanUpControl(this.form, this, /* validateControlPresenceOnChange */ false);
  4854. }
  4855. }
  4856. /**
  4857. * @description
  4858. * Returns an array that represents the path from the top-level form to this control.
  4859. * Each index is the string name of the control on that level.
  4860. */
  4861. get path() {
  4862. return [];
  4863. }
  4864. /**
  4865. * @description
  4866. * The `FormControl` bound to this directive.
  4867. */
  4868. get control() {
  4869. return this.form;
  4870. }
  4871. /**
  4872. * @description
  4873. * Sets the new value for the view model and emits an `ngModelChange` event.
  4874. *
  4875. * @param newValue The new value for the view model.
  4876. */
  4877. viewToModelUpdate(newValue) {
  4878. this.viewModel = newValue;
  4879. this.update.emit(newValue);
  4880. }
  4881. _isControlChanged(changes) {
  4882. return changes.hasOwnProperty('form');
  4883. }
  4884. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormControlDirective, deps: [{ token: NG_VALIDATORS, optional: true, self: true }, { token: NG_ASYNC_VALIDATORS, optional: true, self: true }, { token: NG_VALUE_ACCESSOR, optional: true, self: true }, { token: NG_MODEL_WITH_FORM_CONTROL_WARNING, optional: true }, { token: CALL_SET_DISABLED_STATE, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
  4885. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: FormControlDirective, isStandalone: false, selector: "[formControl]", inputs: { form: ["formControl", "form"], isDisabled: ["disabled", "isDisabled"], model: ["ngModel", "model"] }, outputs: { update: "ngModelChange" }, providers: [formControlBinding], exportAs: ["ngForm"], usesInheritance: true, usesOnChanges: true, ngImport: i0 });
  4886. }
  4887. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormControlDirective, decorators: [{
  4888. type: Directive,
  4889. args: [{
  4890. selector: '[formControl]',
  4891. providers: [formControlBinding],
  4892. exportAs: 'ngForm',
  4893. standalone: false,
  4894. }]
  4895. }], ctorParameters: () => [{ type: undefined, decorators: [{
  4896. type: Optional
  4897. }, {
  4898. type: Self
  4899. }, {
  4900. type: Inject,
  4901. args: [NG_VALIDATORS]
  4902. }] }, { type: undefined, decorators: [{
  4903. type: Optional
  4904. }, {
  4905. type: Self
  4906. }, {
  4907. type: Inject,
  4908. args: [NG_ASYNC_VALIDATORS]
  4909. }] }, { type: undefined, decorators: [{
  4910. type: Optional
  4911. }, {
  4912. type: Self
  4913. }, {
  4914. type: Inject,
  4915. args: [NG_VALUE_ACCESSOR]
  4916. }] }, { type: undefined, decorators: [{
  4917. type: Optional
  4918. }, {
  4919. type: Inject,
  4920. args: [NG_MODEL_WITH_FORM_CONTROL_WARNING]
  4921. }] }, { type: undefined, decorators: [{
  4922. type: Optional
  4923. }, {
  4924. type: Inject,
  4925. args: [CALL_SET_DISABLED_STATE]
  4926. }] }], propDecorators: { form: [{
  4927. type: Input,
  4928. args: ['formControl']
  4929. }], isDisabled: [{
  4930. type: Input,
  4931. args: ['disabled']
  4932. }], model: [{
  4933. type: Input,
  4934. args: ['ngModel']
  4935. }], update: [{
  4936. type: Output,
  4937. args: ['ngModelChange']
  4938. }] } });
  4939. const formDirectiveProvider = {
  4940. provide: ControlContainer,
  4941. useExisting: forwardRef(() => FormGroupDirective),
  4942. };
  4943. /**
  4944. * @description
  4945. *
  4946. * Binds an existing `FormGroup` or `FormRecord` to a DOM element.
  4947. *
  4948. * This directive accepts an existing `FormGroup` instance. It will then use this
  4949. * `FormGroup` instance to match any child `FormControl`, `FormGroup`/`FormRecord`,
  4950. * and `FormArray` instances to child `FormControlName`, `FormGroupName`,
  4951. * and `FormArrayName` directives.
  4952. *
  4953. * @see [Reactive Forms Guide](guide/forms/reactive-forms)
  4954. * @see {@link AbstractControl}
  4955. *
  4956. * @usageNotes
  4957. * ### Register Form Group
  4958. *
  4959. * The following example registers a `FormGroup` with first name and last name controls,
  4960. * and listens for the *ngSubmit* event when the button is clicked.
  4961. *
  4962. * {@example forms/ts/simpleFormGroup/simple_form_group_example.ts region='Component'}
  4963. *
  4964. * @ngModule ReactiveFormsModule
  4965. * @publicApi
  4966. */
  4967. class FormGroupDirective extends ControlContainer {
  4968. callSetDisabledState;
  4969. /**
  4970. * @description
  4971. * Reports whether the form submission has been triggered.
  4972. */
  4973. get submitted() {
  4974. return untracked(this._submittedReactive);
  4975. }
  4976. // TODO(atscott): Remove once invalid API usage is cleaned up internally
  4977. set submitted(value) {
  4978. this._submittedReactive.set(value);
  4979. }
  4980. /** @internal */
  4981. _submitted = computed(() => this._submittedReactive());
  4982. _submittedReactive = signal(false);
  4983. /**
  4984. * Reference to an old form group input value, which is needed to cleanup
  4985. * old instance in case it was replaced with a new one.
  4986. */
  4987. _oldForm;
  4988. /**
  4989. * Callback that should be invoked when controls in FormGroup or FormArray collection change
  4990. * (added or removed). This callback triggers corresponding DOM updates.
  4991. */
  4992. _onCollectionChange = () => this._updateDomValue();
  4993. /**
  4994. * @description
  4995. * Tracks the list of added `FormControlName` instances
  4996. */
  4997. directives = [];
  4998. /**
  4999. * @description
  5000. * Tracks the `FormGroup` bound to this directive.
  5001. */
  5002. form = null;
  5003. /**
  5004. * @description
  5005. * Emits an event when the form submission has been triggered.
  5006. */
  5007. ngSubmit = new EventEmitter();
  5008. constructor(validators, asyncValidators, callSetDisabledState) {
  5009. super();
  5010. this.callSetDisabledState = callSetDisabledState;
  5011. this._setValidators(validators);
  5012. this._setAsyncValidators(asyncValidators);
  5013. }
  5014. /** @docs-private */
  5015. ngOnChanges(changes) {
  5016. if ((typeof ngDevMode === 'undefined' || ngDevMode) && !this.form) {
  5017. throw missingFormException();
  5018. }
  5019. if (changes.hasOwnProperty('form')) {
  5020. this._updateValidators();
  5021. this._updateDomValue();
  5022. this._updateRegistrations();
  5023. this._oldForm = this.form;
  5024. }
  5025. }
  5026. /** @docs-private */
  5027. ngOnDestroy() {
  5028. if (this.form) {
  5029. cleanUpValidators(this.form, this);
  5030. // Currently the `onCollectionChange` callback is rewritten each time the
  5031. // `_registerOnCollectionChange` function is invoked. The implication is that cleanup should
  5032. // happen *only* when the `onCollectionChange` callback was set by this directive instance.
  5033. // Otherwise it might cause overriding a callback of some other directive instances. We should
  5034. // consider updating this logic later to make it similar to how `onChange` callbacks are
  5035. // handled, see https://github.com/angular/angular/issues/39732 for additional info.
  5036. if (this.form._onCollectionChange === this._onCollectionChange) {
  5037. this.form._registerOnCollectionChange(() => { });
  5038. }
  5039. }
  5040. }
  5041. /**
  5042. * @description
  5043. * Returns this directive's instance.
  5044. */
  5045. get formDirective() {
  5046. return this;
  5047. }
  5048. /**
  5049. * @description
  5050. * Returns the `FormGroup` bound to this directive.
  5051. */
  5052. get control() {
  5053. return this.form;
  5054. }
  5055. /**
  5056. * @description
  5057. * Returns an array representing the path to this group. Because this directive
  5058. * always lives at the top level of a form, it always an empty array.
  5059. */
  5060. get path() {
  5061. return [];
  5062. }
  5063. /**
  5064. * @description
  5065. * Method that sets up the control directive in this group, re-calculates its value
  5066. * and validity, and adds the instance to the internal list of directives.
  5067. *
  5068. * @param dir The `FormControlName` directive instance.
  5069. */
  5070. addControl(dir) {
  5071. const ctrl = this.form.get(dir.path);
  5072. setUpControl(ctrl, dir, this.callSetDisabledState);
  5073. ctrl.updateValueAndValidity({ emitEvent: false });
  5074. this.directives.push(dir);
  5075. return ctrl;
  5076. }
  5077. /**
  5078. * @description
  5079. * Retrieves the `FormControl` instance from the provided `FormControlName` directive
  5080. *
  5081. * @param dir The `FormControlName` directive instance.
  5082. */
  5083. getControl(dir) {
  5084. return this.form.get(dir.path);
  5085. }
  5086. /**
  5087. * @description
  5088. * Removes the `FormControlName` instance from the internal list of directives
  5089. *
  5090. * @param dir The `FormControlName` directive instance.
  5091. */
  5092. removeControl(dir) {
  5093. cleanUpControl(dir.control || null, dir, /* validateControlPresenceOnChange */ false);
  5094. removeListItem$1(this.directives, dir);
  5095. }
  5096. /**
  5097. * Adds a new `FormGroupName` directive instance to the form.
  5098. *
  5099. * @param dir The `FormGroupName` directive instance.
  5100. */
  5101. addFormGroup(dir) {
  5102. this._setUpFormContainer(dir);
  5103. }
  5104. /**
  5105. * Performs the necessary cleanup when a `FormGroupName` directive instance is removed from the
  5106. * view.
  5107. *
  5108. * @param dir The `FormGroupName` directive instance.
  5109. */
  5110. removeFormGroup(dir) {
  5111. this._cleanUpFormContainer(dir);
  5112. }
  5113. /**
  5114. * @description
  5115. * Retrieves the `FormGroup` for a provided `FormGroupName` directive instance
  5116. *
  5117. * @param dir The `FormGroupName` directive instance.
  5118. */
  5119. getFormGroup(dir) {
  5120. return this.form.get(dir.path);
  5121. }
  5122. /**
  5123. * Performs the necessary setup when a `FormArrayName` directive instance is added to the view.
  5124. *
  5125. * @param dir The `FormArrayName` directive instance.
  5126. */
  5127. addFormArray(dir) {
  5128. this._setUpFormContainer(dir);
  5129. }
  5130. /**
  5131. * Performs the necessary cleanup when a `FormArrayName` directive instance is removed from the
  5132. * view.
  5133. *
  5134. * @param dir The `FormArrayName` directive instance.
  5135. */
  5136. removeFormArray(dir) {
  5137. this._cleanUpFormContainer(dir);
  5138. }
  5139. /**
  5140. * @description
  5141. * Retrieves the `FormArray` for a provided `FormArrayName` directive instance.
  5142. *
  5143. * @param dir The `FormArrayName` directive instance.
  5144. */
  5145. getFormArray(dir) {
  5146. return this.form.get(dir.path);
  5147. }
  5148. /**
  5149. * Sets the new value for the provided `FormControlName` directive.
  5150. *
  5151. * @param dir The `FormControlName` directive instance.
  5152. * @param value The new value for the directive's control.
  5153. */
  5154. updateModel(dir, value) {
  5155. const ctrl = this.form.get(dir.path);
  5156. ctrl.setValue(value);
  5157. }
  5158. /**
  5159. * @description
  5160. * Method called with the "submit" event is triggered on the form.
  5161. * Triggers the `ngSubmit` emitter to emit the "submit" event as its payload.
  5162. *
  5163. * @param $event The "submit" event object
  5164. */
  5165. onSubmit($event) {
  5166. this._submittedReactive.set(true);
  5167. syncPendingControls(this.form, this.directives);
  5168. this.ngSubmit.emit($event);
  5169. this.form._events.next(new FormSubmittedEvent(this.control));
  5170. // Forms with `method="dialog"` have some special behavior that won't reload the page and that
  5171. // shouldn't be prevented. Note that we need to null check the `event` and the `target`, because
  5172. // some internal apps call this method directly with the wrong arguments.
  5173. return $event?.target?.method === 'dialog';
  5174. }
  5175. /**
  5176. * @description
  5177. * Method called when the "reset" event is triggered on the form.
  5178. */
  5179. onReset() {
  5180. this.resetForm();
  5181. }
  5182. /**
  5183. * @description
  5184. * Resets the form to an initial value and resets its submitted status.
  5185. *
  5186. * @param value The new value for the form.
  5187. */
  5188. resetForm(value = undefined) {
  5189. this.form.reset(value);
  5190. this._submittedReactive.set(false);
  5191. this.form._events.next(new FormResetEvent(this.form));
  5192. }
  5193. /** @internal */
  5194. _updateDomValue() {
  5195. this.directives.forEach((dir) => {
  5196. const oldCtrl = dir.control;
  5197. const newCtrl = this.form.get(dir.path);
  5198. if (oldCtrl !== newCtrl) {
  5199. // Note: the value of the `dir.control` may not be defined, for example when it's a first
  5200. // `FormControl` that is added to a `FormGroup` instance (via `addControl` call).
  5201. cleanUpControl(oldCtrl || null, dir);
  5202. // Check whether new control at the same location inside the corresponding `FormGroup` is an
  5203. // instance of `FormControl` and perform control setup only if that's the case.
  5204. // Note: we don't need to clear the list of directives (`this.directives`) here, it would be
  5205. // taken care of in the `removeControl` method invoked when corresponding `formControlName`
  5206. // directive instance is being removed (invoked from `FormControlName.ngOnDestroy`).
  5207. if (isFormControl(newCtrl)) {
  5208. setUpControl(newCtrl, dir, this.callSetDisabledState);
  5209. dir.control = newCtrl;
  5210. }
  5211. }
  5212. });
  5213. this.form._updateTreeValidity({ emitEvent: false });
  5214. }
  5215. _setUpFormContainer(dir) {
  5216. const ctrl = this.form.get(dir.path);
  5217. setUpFormContainer(ctrl, dir);
  5218. // NOTE: this operation looks unnecessary in case no new validators were added in
  5219. // `setUpFormContainer` call. Consider updating this code to match the logic in
  5220. // `_cleanUpFormContainer` function.
  5221. ctrl.updateValueAndValidity({ emitEvent: false });
  5222. }
  5223. _cleanUpFormContainer(dir) {
  5224. if (this.form) {
  5225. const ctrl = this.form.get(dir.path);
  5226. if (ctrl) {
  5227. const isControlUpdated = cleanUpFormContainer(ctrl, dir);
  5228. if (isControlUpdated) {
  5229. // Run validity check only in case a control was updated (i.e. view validators were
  5230. // removed) as removing view validators might cause validity to change.
  5231. ctrl.updateValueAndValidity({ emitEvent: false });
  5232. }
  5233. }
  5234. }
  5235. }
  5236. _updateRegistrations() {
  5237. this.form._registerOnCollectionChange(this._onCollectionChange);
  5238. if (this._oldForm) {
  5239. this._oldForm._registerOnCollectionChange(() => { });
  5240. }
  5241. }
  5242. _updateValidators() {
  5243. setUpValidators(this.form, this);
  5244. if (this._oldForm) {
  5245. cleanUpValidators(this._oldForm, this);
  5246. }
  5247. }
  5248. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormGroupDirective, deps: [{ token: NG_VALIDATORS, optional: true, self: true }, { token: NG_ASYNC_VALIDATORS, optional: true, self: true }, { token: CALL_SET_DISABLED_STATE, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
  5249. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: FormGroupDirective, isStandalone: false, selector: "[formGroup]", inputs: { form: ["formGroup", "form"] }, outputs: { ngSubmit: "ngSubmit" }, host: { listeners: { "submit": "onSubmit($event)", "reset": "onReset()" } }, providers: [formDirectiveProvider], exportAs: ["ngForm"], usesInheritance: true, usesOnChanges: true, ngImport: i0 });
  5250. }
  5251. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormGroupDirective, decorators: [{
  5252. type: Directive,
  5253. args: [{
  5254. selector: '[formGroup]',
  5255. providers: [formDirectiveProvider],
  5256. host: { '(submit)': 'onSubmit($event)', '(reset)': 'onReset()' },
  5257. exportAs: 'ngForm',
  5258. standalone: false,
  5259. }]
  5260. }], ctorParameters: () => [{ type: undefined, decorators: [{
  5261. type: Optional
  5262. }, {
  5263. type: Self
  5264. }, {
  5265. type: Inject,
  5266. args: [NG_VALIDATORS]
  5267. }] }, { type: undefined, decorators: [{
  5268. type: Optional
  5269. }, {
  5270. type: Self
  5271. }, {
  5272. type: Inject,
  5273. args: [NG_ASYNC_VALIDATORS]
  5274. }] }, { type: undefined, decorators: [{
  5275. type: Optional
  5276. }, {
  5277. type: Inject,
  5278. args: [CALL_SET_DISABLED_STATE]
  5279. }] }], propDecorators: { form: [{
  5280. type: Input,
  5281. args: ['formGroup']
  5282. }], ngSubmit: [{
  5283. type: Output
  5284. }] } });
  5285. const formGroupNameProvider = {
  5286. provide: ControlContainer,
  5287. useExisting: forwardRef(() => FormGroupName),
  5288. };
  5289. /**
  5290. * @description
  5291. *
  5292. * Syncs a nested `FormGroup` or `FormRecord` to a DOM element.
  5293. *
  5294. * This directive can only be used with a parent `FormGroupDirective`.
  5295. *
  5296. * It accepts the string name of the nested `FormGroup` or `FormRecord` to link, and
  5297. * looks for a `FormGroup` or `FormRecord` registered with that name in the parent
  5298. * `FormGroup` instance you passed into `FormGroupDirective`.
  5299. *
  5300. * Use nested form groups to validate a sub-group of a
  5301. * form separately from the rest or to group the values of certain
  5302. * controls into their own nested object.
  5303. *
  5304. * @see [Reactive Forms Guide](guide/forms/reactive-forms)
  5305. *
  5306. * @usageNotes
  5307. *
  5308. * ### Access the group by name
  5309. *
  5310. * The following example uses the `AbstractControl.get` method to access the
  5311. * associated `FormGroup`
  5312. *
  5313. * ```ts
  5314. * this.form.get('name');
  5315. * ```
  5316. *
  5317. * ### Access individual controls in the group
  5318. *
  5319. * The following example uses the `AbstractControl.get` method to access
  5320. * individual controls within the group using dot syntax.
  5321. *
  5322. * ```ts
  5323. * this.form.get('name.first');
  5324. * ```
  5325. *
  5326. * ### Register a nested `FormGroup`.
  5327. *
  5328. * The following example registers a nested *name* `FormGroup` within an existing `FormGroup`,
  5329. * and provides methods to retrieve the nested `FormGroup` and individual controls.
  5330. *
  5331. * {@example forms/ts/nestedFormGroup/nested_form_group_example.ts region='Component'}
  5332. *
  5333. * @ngModule ReactiveFormsModule
  5334. * @publicApi
  5335. */
  5336. class FormGroupName extends AbstractFormGroupDirective {
  5337. /**
  5338. * @description
  5339. * Tracks the name of the `FormGroup` bound to the directive. The name corresponds
  5340. * to a key in the parent `FormGroup` or `FormArray`.
  5341. * Accepts a name as a string or a number.
  5342. * The name in the form of a string is useful for individual forms,
  5343. * while the numerical form allows for form groups to be bound
  5344. * to indices when iterating over groups in a `FormArray`.
  5345. */
  5346. name = null;
  5347. constructor(parent, validators, asyncValidators) {
  5348. super();
  5349. this._parent = parent;
  5350. this._setValidators(validators);
  5351. this._setAsyncValidators(asyncValidators);
  5352. }
  5353. /** @internal */
  5354. _checkParentType() {
  5355. if (hasInvalidParent(this._parent) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
  5356. throw groupParentException();
  5357. }
  5358. }
  5359. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormGroupName, deps: [{ token: ControlContainer, host: true, optional: true, skipSelf: true }, { token: NG_VALIDATORS, optional: true, self: true }, { token: NG_ASYNC_VALIDATORS, optional: true, self: true }], target: i0.ɵɵFactoryTarget.Directive });
  5360. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: FormGroupName, isStandalone: false, selector: "[formGroupName]", inputs: { name: ["formGroupName", "name"] }, providers: [formGroupNameProvider], usesInheritance: true, ngImport: i0 });
  5361. }
  5362. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormGroupName, decorators: [{
  5363. type: Directive,
  5364. args: [{
  5365. selector: '[formGroupName]',
  5366. providers: [formGroupNameProvider],
  5367. standalone: false,
  5368. }]
  5369. }], ctorParameters: () => [{ type: ControlContainer, decorators: [{
  5370. type: Optional
  5371. }, {
  5372. type: Host
  5373. }, {
  5374. type: SkipSelf
  5375. }] }, { type: undefined, decorators: [{
  5376. type: Optional
  5377. }, {
  5378. type: Self
  5379. }, {
  5380. type: Inject,
  5381. args: [NG_VALIDATORS]
  5382. }] }, { type: undefined, decorators: [{
  5383. type: Optional
  5384. }, {
  5385. type: Self
  5386. }, {
  5387. type: Inject,
  5388. args: [NG_ASYNC_VALIDATORS]
  5389. }] }], propDecorators: { name: [{
  5390. type: Input,
  5391. args: ['formGroupName']
  5392. }] } });
  5393. const formArrayNameProvider = {
  5394. provide: ControlContainer,
  5395. useExisting: forwardRef(() => FormArrayName),
  5396. };
  5397. /**
  5398. * @description
  5399. *
  5400. * Syncs a nested `FormArray` to a DOM element.
  5401. *
  5402. * This directive is designed to be used with a parent `FormGroupDirective` (selector:
  5403. * `[formGroup]`).
  5404. *
  5405. * It accepts the string name of the nested `FormArray` you want to link, and
  5406. * will look for a `FormArray` registered with that name in the parent
  5407. * `FormGroup` instance you passed into `FormGroupDirective`.
  5408. *
  5409. * @see [Reactive Forms Guide](guide/forms/reactive-forms)
  5410. * @see {@link AbstractControl}
  5411. *
  5412. * @usageNotes
  5413. *
  5414. * ### Example
  5415. *
  5416. * {@example forms/ts/nestedFormArray/nested_form_array_example.ts region='Component'}
  5417. *
  5418. * @ngModule ReactiveFormsModule
  5419. * @publicApi
  5420. */
  5421. class FormArrayName extends ControlContainer {
  5422. /** @internal */
  5423. _parent;
  5424. /**
  5425. * @description
  5426. * Tracks the name of the `FormArray` bound to the directive. The name corresponds
  5427. * to a key in the parent `FormGroup` or `FormArray`.
  5428. * Accepts a name as a string or a number.
  5429. * The name in the form of a string is useful for individual forms,
  5430. * while the numerical form allows for form arrays to be bound
  5431. * to indices when iterating over arrays in a `FormArray`.
  5432. */
  5433. name = null;
  5434. constructor(parent, validators, asyncValidators) {
  5435. super();
  5436. this._parent = parent;
  5437. this._setValidators(validators);
  5438. this._setAsyncValidators(asyncValidators);
  5439. }
  5440. /**
  5441. * A lifecycle method called when the directive's inputs are initialized. For internal use only.
  5442. * @throws If the directive does not have a valid parent.
  5443. * @docs-private
  5444. */
  5445. ngOnInit() {
  5446. if (hasInvalidParent(this._parent) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
  5447. throw arrayParentException();
  5448. }
  5449. this.formDirective.addFormArray(this);
  5450. }
  5451. /**
  5452. * A lifecycle method called before the directive's instance is destroyed. For internal use only.
  5453. * @docs-private
  5454. */
  5455. ngOnDestroy() {
  5456. this.formDirective?.removeFormArray(this);
  5457. }
  5458. /**
  5459. * @description
  5460. * The `FormArray` bound to this directive.
  5461. */
  5462. get control() {
  5463. return this.formDirective.getFormArray(this);
  5464. }
  5465. /**
  5466. * @description
  5467. * The top-level directive for this group if present, otherwise null.
  5468. */
  5469. get formDirective() {
  5470. return this._parent ? this._parent.formDirective : null;
  5471. }
  5472. /**
  5473. * @description
  5474. * Returns an array that represents the path from the top-level form to this control.
  5475. * Each index is the string name of the control on that level.
  5476. */
  5477. get path() {
  5478. return controlPath(this.name == null ? this.name : this.name.toString(), this._parent);
  5479. }
  5480. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormArrayName, deps: [{ token: ControlContainer, host: true, optional: true, skipSelf: true }, { token: NG_VALIDATORS, optional: true, self: true }, { token: NG_ASYNC_VALIDATORS, optional: true, self: true }], target: i0.ɵɵFactoryTarget.Directive });
  5481. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: FormArrayName, isStandalone: false, selector: "[formArrayName]", inputs: { name: ["formArrayName", "name"] }, providers: [formArrayNameProvider], usesInheritance: true, ngImport: i0 });
  5482. }
  5483. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormArrayName, decorators: [{
  5484. type: Directive,
  5485. args: [{
  5486. selector: '[formArrayName]',
  5487. providers: [formArrayNameProvider],
  5488. standalone: false,
  5489. }]
  5490. }], ctorParameters: () => [{ type: ControlContainer, decorators: [{
  5491. type: Optional
  5492. }, {
  5493. type: Host
  5494. }, {
  5495. type: SkipSelf
  5496. }] }, { type: undefined, decorators: [{
  5497. type: Optional
  5498. }, {
  5499. type: Self
  5500. }, {
  5501. type: Inject,
  5502. args: [NG_VALIDATORS]
  5503. }] }, { type: undefined, decorators: [{
  5504. type: Optional
  5505. }, {
  5506. type: Self
  5507. }, {
  5508. type: Inject,
  5509. args: [NG_ASYNC_VALIDATORS]
  5510. }] }], propDecorators: { name: [{
  5511. type: Input,
  5512. args: ['formArrayName']
  5513. }] } });
  5514. function hasInvalidParent(parent) {
  5515. return (!(parent instanceof FormGroupName) &&
  5516. !(parent instanceof FormGroupDirective) &&
  5517. !(parent instanceof FormArrayName));
  5518. }
  5519. const controlNameBinding = {
  5520. provide: NgControl,
  5521. useExisting: forwardRef(() => FormControlName),
  5522. };
  5523. /**
  5524. * @description
  5525. * Syncs a `FormControl` in an existing `FormGroup` to a form control
  5526. * element by name.
  5527. *
  5528. * @see [Reactive Forms Guide](guide/forms/reactive-forms)
  5529. * @see {@link FormControl}
  5530. * @see {@link AbstractControl}
  5531. *
  5532. * @usageNotes
  5533. *
  5534. * ### Register `FormControl` within a group
  5535. *
  5536. * The following example shows how to register multiple form controls within a form group
  5537. * and set their value.
  5538. *
  5539. * {@example forms/ts/simpleFormGroup/simple_form_group_example.ts region='Component'}
  5540. *
  5541. * To see `formControlName` examples with different form control types, see:
  5542. *
  5543. * * Radio buttons: `RadioControlValueAccessor`
  5544. * * Selects: `SelectControlValueAccessor`
  5545. *
  5546. * ### Use with ngModel is deprecated
  5547. *
  5548. * Support for using the `ngModel` input property and `ngModelChange` event with reactive
  5549. * form directives has been deprecated in Angular v6 and is scheduled for removal in
  5550. * a future version of Angular.
  5551. *
  5552. * @ngModule ReactiveFormsModule
  5553. * @publicApi
  5554. */
  5555. class FormControlName extends NgControl {
  5556. _ngModelWarningConfig;
  5557. _added = false;
  5558. /**
  5559. * Internal reference to the view model value.
  5560. * @internal
  5561. */
  5562. viewModel;
  5563. /**
  5564. * @description
  5565. * Tracks the `FormControl` instance bound to the directive.
  5566. */
  5567. control;
  5568. /**
  5569. * @description
  5570. * Tracks the name of the `FormControl` bound to the directive. The name corresponds
  5571. * to a key in the parent `FormGroup` or `FormArray`.
  5572. * Accepts a name as a string or a number.
  5573. * The name in the form of a string is useful for individual forms,
  5574. * while the numerical form allows for form controls to be bound
  5575. * to indices when iterating over controls in a `FormArray`.
  5576. */
  5577. name = null;
  5578. /**
  5579. * @description
  5580. * Triggers a warning in dev mode that this input should not be used with reactive forms.
  5581. */
  5582. set isDisabled(isDisabled) {
  5583. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  5584. console.warn(disabledAttrWarning);
  5585. }
  5586. }
  5587. // TODO(kara): remove next 4 properties once deprecation period is over
  5588. /** @deprecated as of v6 */
  5589. model;
  5590. /** @deprecated as of v6 */
  5591. update = new EventEmitter();
  5592. /**
  5593. * @description
  5594. * Static property used to track whether any ngModel warnings have been sent across
  5595. * all instances of FormControlName. Used to support warning config of "once".
  5596. *
  5597. * @internal
  5598. */
  5599. static _ngModelWarningSentOnce = false;
  5600. /**
  5601. * @description
  5602. * Instance property used to track whether an ngModel warning has been sent out for this
  5603. * particular FormControlName instance. Used to support warning config of "always".
  5604. *
  5605. * @internal
  5606. */
  5607. _ngModelWarningSent = false;
  5608. constructor(parent, validators, asyncValidators, valueAccessors, _ngModelWarningConfig) {
  5609. super();
  5610. this._ngModelWarningConfig = _ngModelWarningConfig;
  5611. this._parent = parent;
  5612. this._setValidators(validators);
  5613. this._setAsyncValidators(asyncValidators);
  5614. this.valueAccessor = selectValueAccessor(this, valueAccessors);
  5615. }
  5616. /** @docs-private */
  5617. ngOnChanges(changes) {
  5618. if (!this._added)
  5619. this._setUpControl();
  5620. if (isPropertyUpdated(changes, this.viewModel)) {
  5621. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  5622. _ngModelWarning('formControlName', FormControlName, this, this._ngModelWarningConfig);
  5623. }
  5624. this.viewModel = this.model;
  5625. this.formDirective.updateModel(this, this.model);
  5626. }
  5627. }
  5628. /** @docs-private */
  5629. ngOnDestroy() {
  5630. if (this.formDirective) {
  5631. this.formDirective.removeControl(this);
  5632. }
  5633. }
  5634. /**
  5635. * @description
  5636. * Sets the new value for the view model and emits an `ngModelChange` event.
  5637. *
  5638. * @param newValue The new value for the view model.
  5639. */
  5640. viewToModelUpdate(newValue) {
  5641. this.viewModel = newValue;
  5642. this.update.emit(newValue);
  5643. }
  5644. /**
  5645. * @description
  5646. * Returns an array that represents the path from the top-level form to this control.
  5647. * Each index is the string name of the control on that level.
  5648. */
  5649. get path() {
  5650. return controlPath(this.name == null ? this.name : this.name.toString(), this._parent);
  5651. }
  5652. /**
  5653. * @description
  5654. * The top-level directive for this group if present, otherwise null.
  5655. */
  5656. get formDirective() {
  5657. return this._parent ? this._parent.formDirective : null;
  5658. }
  5659. _setUpControl() {
  5660. if (typeof ngDevMode === 'undefined' || ngDevMode) {
  5661. checkParentType(this._parent, this.name);
  5662. }
  5663. this.control = this.formDirective.addControl(this);
  5664. this._added = true;
  5665. }
  5666. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormControlName, deps: [{ token: ControlContainer, host: true, optional: true, skipSelf: true }, { token: NG_VALIDATORS, optional: true, self: true }, { token: NG_ASYNC_VALIDATORS, optional: true, self: true }, { token: NG_VALUE_ACCESSOR, optional: true, self: true }, { token: NG_MODEL_WITH_FORM_CONTROL_WARNING, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
  5667. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: FormControlName, isStandalone: false, selector: "[formControlName]", inputs: { name: ["formControlName", "name"], isDisabled: ["disabled", "isDisabled"], model: ["ngModel", "model"] }, outputs: { update: "ngModelChange" }, providers: [controlNameBinding], usesInheritance: true, usesOnChanges: true, ngImport: i0 });
  5668. }
  5669. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormControlName, decorators: [{
  5670. type: Directive,
  5671. args: [{
  5672. selector: '[formControlName]',
  5673. providers: [controlNameBinding],
  5674. standalone: false,
  5675. }]
  5676. }], ctorParameters: () => [{ type: ControlContainer, decorators: [{
  5677. type: Optional
  5678. }, {
  5679. type: Host
  5680. }, {
  5681. type: SkipSelf
  5682. }] }, { type: undefined, decorators: [{
  5683. type: Optional
  5684. }, {
  5685. type: Self
  5686. }, {
  5687. type: Inject,
  5688. args: [NG_VALIDATORS]
  5689. }] }, { type: undefined, decorators: [{
  5690. type: Optional
  5691. }, {
  5692. type: Self
  5693. }, {
  5694. type: Inject,
  5695. args: [NG_ASYNC_VALIDATORS]
  5696. }] }, { type: undefined, decorators: [{
  5697. type: Optional
  5698. }, {
  5699. type: Self
  5700. }, {
  5701. type: Inject,
  5702. args: [NG_VALUE_ACCESSOR]
  5703. }] }, { type: undefined, decorators: [{
  5704. type: Optional
  5705. }, {
  5706. type: Inject,
  5707. args: [NG_MODEL_WITH_FORM_CONTROL_WARNING]
  5708. }] }], propDecorators: { name: [{
  5709. type: Input,
  5710. args: ['formControlName']
  5711. }], isDisabled: [{
  5712. type: Input,
  5713. args: ['disabled']
  5714. }], model: [{
  5715. type: Input,
  5716. args: ['ngModel']
  5717. }], update: [{
  5718. type: Output,
  5719. args: ['ngModelChange']
  5720. }] } });
  5721. function checkParentType(parent, name) {
  5722. if (!(parent instanceof FormGroupName) && parent instanceof AbstractFormGroupDirective) {
  5723. throw ngModelGroupException();
  5724. }
  5725. else if (!(parent instanceof FormGroupName) &&
  5726. !(parent instanceof FormGroupDirective) &&
  5727. !(parent instanceof FormArrayName)) {
  5728. throw controlParentException(name);
  5729. }
  5730. }
  5731. const SELECT_VALUE_ACCESSOR = {
  5732. provide: NG_VALUE_ACCESSOR,
  5733. useExisting: forwardRef(() => SelectControlValueAccessor),
  5734. multi: true,
  5735. };
  5736. function _buildValueString$1(id, value) {
  5737. if (id == null)
  5738. return `${value}`;
  5739. if (value && typeof value === 'object')
  5740. value = 'Object';
  5741. return `${id}: ${value}`.slice(0, 50);
  5742. }
  5743. function _extractId$1(valueString) {
  5744. return valueString.split(':')[0];
  5745. }
  5746. /**
  5747. * @description
  5748. * The `ControlValueAccessor` for writing select control values and listening to select control
  5749. * changes. The value accessor is used by the `FormControlDirective`, `FormControlName`, and
  5750. * `NgModel` directives.
  5751. *
  5752. * @usageNotes
  5753. *
  5754. * ### Using select controls in a reactive form
  5755. *
  5756. * The following examples show how to use a select control in a reactive form.
  5757. *
  5758. * {@example forms/ts/reactiveSelectControl/reactive_select_control_example.ts region='Component'}
  5759. *
  5760. * ### Using select controls in a template-driven form
  5761. *
  5762. * To use a select in a template-driven form, simply add an `ngModel` and a `name`
  5763. * attribute to the main `<select>` tag.
  5764. *
  5765. * {@example forms/ts/selectControl/select_control_example.ts region='Component'}
  5766. *
  5767. * ### Customizing option selection
  5768. *
  5769. * Angular uses object identity to select option. It's possible for the identities of items
  5770. * to change while the data does not. This can happen, for example, if the items are produced
  5771. * from an RPC to the server, and that RPC is re-run. Even if the data hasn't changed, the
  5772. * second response will produce objects with different identities.
  5773. *
  5774. * To customize the default option comparison algorithm, `<select>` supports `compareWith` input.
  5775. * `compareWith` takes a **function** which has two arguments: `option1` and `option2`.
  5776. * If `compareWith` is given, Angular selects option by the return value of the function.
  5777. *
  5778. * ```ts
  5779. * const selectedCountriesControl = new FormControl();
  5780. * ```
  5781. *
  5782. * ```html
  5783. * <select [compareWith]="compareFn" [formControl]="selectedCountriesControl">
  5784. * <option *ngFor="let country of countries" [ngValue]="country">
  5785. * {{country.name}}
  5786. * </option>
  5787. * </select>
  5788. *
  5789. * compareFn(c1: Country, c2: Country): boolean {
  5790. * return c1 && c2 ? c1.id === c2.id : c1 === c2;
  5791. * }
  5792. * ```
  5793. *
  5794. * **Note:** We listen to the 'change' event because 'input' events aren't fired
  5795. * for selects in IE, see:
  5796. * https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event#browser_compatibility
  5797. *
  5798. * @ngModule ReactiveFormsModule
  5799. * @ngModule FormsModule
  5800. * @publicApi
  5801. */
  5802. class SelectControlValueAccessor extends BuiltInControlValueAccessor {
  5803. /** @docs-private */
  5804. value;
  5805. /** @internal */
  5806. _optionMap = new Map();
  5807. /** @internal */
  5808. _idCounter = 0;
  5809. /**
  5810. * @description
  5811. * Tracks the option comparison algorithm for tracking identities when
  5812. * checking for changes.
  5813. */
  5814. set compareWith(fn) {
  5815. if (typeof fn !== 'function' && (typeof ngDevMode === 'undefined' || ngDevMode)) {
  5816. throw new _RuntimeError(1201 /* RuntimeErrorCode.COMPAREWITH_NOT_A_FN */, `compareWith must be a function, but received ${JSON.stringify(fn)}`);
  5817. }
  5818. this._compareWith = fn;
  5819. }
  5820. _compareWith = Object.is;
  5821. /**
  5822. * Sets the "value" property on the select element.
  5823. * @docs-private
  5824. */
  5825. writeValue(value) {
  5826. this.value = value;
  5827. const id = this._getOptionId(value);
  5828. const valueString = _buildValueString$1(id, value);
  5829. this.setProperty('value', valueString);
  5830. }
  5831. /**
  5832. * Registers a function called when the control value changes.
  5833. * @docs-private
  5834. */
  5835. registerOnChange(fn) {
  5836. this.onChange = (valueString) => {
  5837. this.value = this._getOptionValue(valueString);
  5838. fn(this.value);
  5839. };
  5840. }
  5841. /** @internal */
  5842. _registerOption() {
  5843. return (this._idCounter++).toString();
  5844. }
  5845. /** @internal */
  5846. _getOptionId(value) {
  5847. for (const id of this._optionMap.keys()) {
  5848. if (this._compareWith(this._optionMap.get(id), value))
  5849. return id;
  5850. }
  5851. return null;
  5852. }
  5853. /** @internal */
  5854. _getOptionValue(valueString) {
  5855. const id = _extractId$1(valueString);
  5856. return this._optionMap.has(id) ? this._optionMap.get(id) : valueString;
  5857. }
  5858. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: SelectControlValueAccessor, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  5859. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: SelectControlValueAccessor, isStandalone: false, selector: "select:not([multiple])[formControlName],select:not([multiple])[formControl],select:not([multiple])[ngModel]", inputs: { compareWith: "compareWith" }, host: { listeners: { "change": "onChange($event.target.value)", "blur": "onTouched()" } }, providers: [SELECT_VALUE_ACCESSOR], usesInheritance: true, ngImport: i0 });
  5860. }
  5861. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: SelectControlValueAccessor, decorators: [{
  5862. type: Directive,
  5863. args: [{
  5864. selector: 'select:not([multiple])[formControlName],select:not([multiple])[formControl],select:not([multiple])[ngModel]',
  5865. host: { '(change)': 'onChange($event.target.value)', '(blur)': 'onTouched()' },
  5866. providers: [SELECT_VALUE_ACCESSOR],
  5867. standalone: false,
  5868. }]
  5869. }], propDecorators: { compareWith: [{
  5870. type: Input
  5871. }] } });
  5872. /**
  5873. * @description
  5874. * Marks `<option>` as dynamic, so Angular can be notified when options change.
  5875. *
  5876. * @see {@link SelectControlValueAccessor}
  5877. *
  5878. * @ngModule ReactiveFormsModule
  5879. * @ngModule FormsModule
  5880. * @publicApi
  5881. */
  5882. class NgSelectOption {
  5883. _element;
  5884. _renderer;
  5885. _select;
  5886. /**
  5887. * @description
  5888. * ID of the option element
  5889. */
  5890. id;
  5891. constructor(_element, _renderer, _select) {
  5892. this._element = _element;
  5893. this._renderer = _renderer;
  5894. this._select = _select;
  5895. if (this._select)
  5896. this.id = this._select._registerOption();
  5897. }
  5898. /**
  5899. * @description
  5900. * Tracks the value bound to the option element. Unlike the value binding,
  5901. * ngValue supports binding to objects.
  5902. */
  5903. set ngValue(value) {
  5904. if (this._select == null)
  5905. return;
  5906. this._select._optionMap.set(this.id, value);
  5907. this._setElementValue(_buildValueString$1(this.id, value));
  5908. this._select.writeValue(this._select.value);
  5909. }
  5910. /**
  5911. * @description
  5912. * Tracks simple string values bound to the option element.
  5913. * For objects, use the `ngValue` input binding.
  5914. */
  5915. set value(value) {
  5916. this._setElementValue(value);
  5917. if (this._select)
  5918. this._select.writeValue(this._select.value);
  5919. }
  5920. /** @internal */
  5921. _setElementValue(value) {
  5922. this._renderer.setProperty(this._element.nativeElement, 'value', value);
  5923. }
  5924. /** @docs-private */
  5925. ngOnDestroy() {
  5926. if (this._select) {
  5927. this._select._optionMap.delete(this.id);
  5928. this._select.writeValue(this._select.value);
  5929. }
  5930. }
  5931. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgSelectOption, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }, { token: SelectControlValueAccessor, host: true, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
  5932. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: NgSelectOption, isStandalone: false, selector: "option", inputs: { ngValue: "ngValue", value: "value" }, ngImport: i0 });
  5933. }
  5934. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NgSelectOption, decorators: [{
  5935. type: Directive,
  5936. args: [{
  5937. selector: 'option',
  5938. standalone: false,
  5939. }]
  5940. }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.Renderer2 }, { type: SelectControlValueAccessor, decorators: [{
  5941. type: Optional
  5942. }, {
  5943. type: Host
  5944. }] }], propDecorators: { ngValue: [{
  5945. type: Input,
  5946. args: ['ngValue']
  5947. }], value: [{
  5948. type: Input,
  5949. args: ['value']
  5950. }] } });
  5951. const SELECT_MULTIPLE_VALUE_ACCESSOR = {
  5952. provide: NG_VALUE_ACCESSOR,
  5953. useExisting: forwardRef(() => SelectMultipleControlValueAccessor),
  5954. multi: true,
  5955. };
  5956. function _buildValueString(id, value) {
  5957. if (id == null)
  5958. return `${value}`;
  5959. if (typeof value === 'string')
  5960. value = `'${value}'`;
  5961. if (value && typeof value === 'object')
  5962. value = 'Object';
  5963. return `${id}: ${value}`.slice(0, 50);
  5964. }
  5965. function _extractId(valueString) {
  5966. return valueString.split(':')[0];
  5967. }
  5968. /**
  5969. * @description
  5970. * The `ControlValueAccessor` for writing multi-select control values and listening to multi-select
  5971. * control changes. The value accessor is used by the `FormControlDirective`, `FormControlName`, and
  5972. * `NgModel` directives.
  5973. *
  5974. * @see {@link SelectControlValueAccessor}
  5975. *
  5976. * @usageNotes
  5977. *
  5978. * ### Using a multi-select control
  5979. *
  5980. * The follow example shows you how to use a multi-select control with a reactive form.
  5981. *
  5982. * ```ts
  5983. * const countryControl = new FormControl();
  5984. * ```
  5985. *
  5986. * ```html
  5987. * <select multiple name="countries" [formControl]="countryControl">
  5988. * <option *ngFor="let country of countries" [ngValue]="country">
  5989. * {{ country.name }}
  5990. * </option>
  5991. * </select>
  5992. * ```
  5993. *
  5994. * ### Customizing option selection
  5995. *
  5996. * To customize the default option comparison algorithm, `<select>` supports `compareWith` input.
  5997. * See the `SelectControlValueAccessor` for usage.
  5998. *
  5999. * @ngModule ReactiveFormsModule
  6000. * @ngModule FormsModule
  6001. * @publicApi
  6002. */
  6003. class SelectMultipleControlValueAccessor extends BuiltInControlValueAccessor {
  6004. /**
  6005. * The current value.
  6006. * @docs-private
  6007. */
  6008. value;
  6009. /** @internal */
  6010. _optionMap = new Map();
  6011. /** @internal */
  6012. _idCounter = 0;
  6013. /**
  6014. * @description
  6015. * Tracks the option comparison algorithm for tracking identities when
  6016. * checking for changes.
  6017. */
  6018. set compareWith(fn) {
  6019. if (typeof fn !== 'function' && (typeof ngDevMode === 'undefined' || ngDevMode)) {
  6020. throw new _RuntimeError(1201 /* RuntimeErrorCode.COMPAREWITH_NOT_A_FN */, `compareWith must be a function, but received ${JSON.stringify(fn)}`);
  6021. }
  6022. this._compareWith = fn;
  6023. }
  6024. _compareWith = Object.is;
  6025. /**
  6026. * Sets the "value" property on one or of more of the select's options.
  6027. * @docs-private
  6028. */
  6029. writeValue(value) {
  6030. this.value = value;
  6031. let optionSelectedStateSetter;
  6032. if (Array.isArray(value)) {
  6033. // convert values to ids
  6034. const ids = value.map((v) => this._getOptionId(v));
  6035. optionSelectedStateSetter = (opt, o) => {
  6036. opt._setSelected(ids.indexOf(o.toString()) > -1);
  6037. };
  6038. }
  6039. else {
  6040. optionSelectedStateSetter = (opt, o) => {
  6041. opt._setSelected(false);
  6042. };
  6043. }
  6044. this._optionMap.forEach(optionSelectedStateSetter);
  6045. }
  6046. /**
  6047. * Registers a function called when the control value changes
  6048. * and writes an array of the selected options.
  6049. * @docs-private
  6050. */
  6051. registerOnChange(fn) {
  6052. this.onChange = (element) => {
  6053. const selected = [];
  6054. const selectedOptions = element.selectedOptions;
  6055. if (selectedOptions !== undefined) {
  6056. const options = selectedOptions;
  6057. for (let i = 0; i < options.length; i++) {
  6058. const opt = options[i];
  6059. const val = this._getOptionValue(opt.value);
  6060. selected.push(val);
  6061. }
  6062. }
  6063. // Degrade to use `options` when `selectedOptions` property is not available.
  6064. // Note: the `selectedOptions` is available in all supported browsers, but the Domino lib
  6065. // doesn't have it currently, see https://github.com/fgnass/domino/issues/177.
  6066. else {
  6067. const options = element.options;
  6068. for (let i = 0; i < options.length; i++) {
  6069. const opt = options[i];
  6070. if (opt.selected) {
  6071. const val = this._getOptionValue(opt.value);
  6072. selected.push(val);
  6073. }
  6074. }
  6075. }
  6076. this.value = selected;
  6077. fn(selected);
  6078. };
  6079. }
  6080. /** @internal */
  6081. _registerOption(value) {
  6082. const id = (this._idCounter++).toString();
  6083. this._optionMap.set(id, value);
  6084. return id;
  6085. }
  6086. /** @internal */
  6087. _getOptionId(value) {
  6088. for (const id of this._optionMap.keys()) {
  6089. if (this._compareWith(this._optionMap.get(id)._value, value))
  6090. return id;
  6091. }
  6092. return null;
  6093. }
  6094. /** @internal */
  6095. _getOptionValue(valueString) {
  6096. const id = _extractId(valueString);
  6097. return this._optionMap.has(id) ? this._optionMap.get(id)._value : valueString;
  6098. }
  6099. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: SelectMultipleControlValueAccessor, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  6100. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: SelectMultipleControlValueAccessor, isStandalone: false, selector: "select[multiple][formControlName],select[multiple][formControl],select[multiple][ngModel]", inputs: { compareWith: "compareWith" }, host: { listeners: { "change": "onChange($event.target)", "blur": "onTouched()" } }, providers: [SELECT_MULTIPLE_VALUE_ACCESSOR], usesInheritance: true, ngImport: i0 });
  6101. }
  6102. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: SelectMultipleControlValueAccessor, decorators: [{
  6103. type: Directive,
  6104. args: [{
  6105. selector: 'select[multiple][formControlName],select[multiple][formControl],select[multiple][ngModel]',
  6106. host: { '(change)': 'onChange($event.target)', '(blur)': 'onTouched()' },
  6107. providers: [SELECT_MULTIPLE_VALUE_ACCESSOR],
  6108. standalone: false,
  6109. }]
  6110. }], propDecorators: { compareWith: [{
  6111. type: Input
  6112. }] } });
  6113. /**
  6114. * @description
  6115. * Marks `<option>` as dynamic, so Angular can be notified when options change.
  6116. *
  6117. * @see {@link SelectMultipleControlValueAccessor}
  6118. *
  6119. * @ngModule ReactiveFormsModule
  6120. * @ngModule FormsModule
  6121. * @publicApi
  6122. */
  6123. class ɵNgSelectMultipleOption {
  6124. _element;
  6125. _renderer;
  6126. _select;
  6127. id;
  6128. /** @internal */
  6129. _value;
  6130. constructor(_element, _renderer, _select) {
  6131. this._element = _element;
  6132. this._renderer = _renderer;
  6133. this._select = _select;
  6134. if (this._select) {
  6135. this.id = this._select._registerOption(this);
  6136. }
  6137. }
  6138. /**
  6139. * @description
  6140. * Tracks the value bound to the option element. Unlike the value binding,
  6141. * ngValue supports binding to objects.
  6142. */
  6143. set ngValue(value) {
  6144. if (this._select == null)
  6145. return;
  6146. this._value = value;
  6147. this._setElementValue(_buildValueString(this.id, value));
  6148. this._select.writeValue(this._select.value);
  6149. }
  6150. /**
  6151. * @description
  6152. * Tracks simple string values bound to the option element.
  6153. * For objects, use the `ngValue` input binding.
  6154. */
  6155. set value(value) {
  6156. if (this._select) {
  6157. this._value = value;
  6158. this._setElementValue(_buildValueString(this.id, value));
  6159. this._select.writeValue(this._select.value);
  6160. }
  6161. else {
  6162. this._setElementValue(value);
  6163. }
  6164. }
  6165. /** @internal */
  6166. _setElementValue(value) {
  6167. this._renderer.setProperty(this._element.nativeElement, 'value', value);
  6168. }
  6169. /** @internal */
  6170. _setSelected(selected) {
  6171. this._renderer.setProperty(this._element.nativeElement, 'selected', selected);
  6172. }
  6173. /** @docs-private */
  6174. ngOnDestroy() {
  6175. if (this._select) {
  6176. this._select._optionMap.delete(this.id);
  6177. this._select.writeValue(this._select.value);
  6178. }
  6179. }
  6180. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: ɵNgSelectMultipleOption, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }, { token: SelectMultipleControlValueAccessor, host: true, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
  6181. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: ɵNgSelectMultipleOption, isStandalone: false, selector: "option", inputs: { ngValue: "ngValue", value: "value" }, ngImport: i0 });
  6182. }
  6183. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: ɵNgSelectMultipleOption, decorators: [{
  6184. type: Directive,
  6185. args: [{
  6186. selector: 'option',
  6187. standalone: false,
  6188. }]
  6189. }], ctorParameters: () => [{ type: i0.ElementRef }, { type: i0.Renderer2 }, { type: SelectMultipleControlValueAccessor, decorators: [{
  6190. type: Optional
  6191. }, {
  6192. type: Host
  6193. }] }], propDecorators: { ngValue: [{
  6194. type: Input,
  6195. args: ['ngValue']
  6196. }], value: [{
  6197. type: Input,
  6198. args: ['value']
  6199. }] } });
  6200. /**
  6201. * Method that updates string to integer if not already a number
  6202. *
  6203. * @param value The value to convert to integer.
  6204. * @returns value of parameter converted to number or integer.
  6205. */
  6206. function toInteger(value) {
  6207. return typeof value === 'number' ? value : parseInt(value, 10);
  6208. }
  6209. /**
  6210. * Method that ensures that provided value is a float (and converts it to float if needed).
  6211. *
  6212. * @param value The value to convert to float.
  6213. * @returns value of parameter converted to number or float.
  6214. */
  6215. function toFloat(value) {
  6216. return typeof value === 'number' ? value : parseFloat(value);
  6217. }
  6218. /**
  6219. * A base class for Validator-based Directives. The class contains common logic shared across such
  6220. * Directives.
  6221. *
  6222. * For internal use only, this class is not intended for use outside of the Forms package.
  6223. */
  6224. class AbstractValidatorDirective {
  6225. _validator = nullValidator;
  6226. _onChange;
  6227. /**
  6228. * A flag that tracks whether this validator is enabled.
  6229. *
  6230. * Marking it `internal` (vs `protected`), so that this flag can be used in host bindings of
  6231. * directive classes that extend this base class.
  6232. * @internal
  6233. */
  6234. _enabled;
  6235. /** @docs-private */
  6236. ngOnChanges(changes) {
  6237. if (this.inputName in changes) {
  6238. const input = this.normalizeInput(changes[this.inputName].currentValue);
  6239. this._enabled = this.enabled(input);
  6240. this._validator = this._enabled ? this.createValidator(input) : nullValidator;
  6241. if (this._onChange) {
  6242. this._onChange();
  6243. }
  6244. }
  6245. }
  6246. /** @docs-private */
  6247. validate(control) {
  6248. return this._validator(control);
  6249. }
  6250. /** @docs-private */
  6251. registerOnValidatorChange(fn) {
  6252. this._onChange = fn;
  6253. }
  6254. /**
  6255. * @description
  6256. * Determines whether this validator should be active or not based on an input.
  6257. * Base class implementation checks whether an input is defined (if the value is different from
  6258. * `null` and `undefined`). Validator classes that extend this base class can override this
  6259. * function with the logic specific to a particular validator directive.
  6260. */
  6261. enabled(input) {
  6262. return input != null /* both `null` and `undefined` */;
  6263. }
  6264. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: AbstractValidatorDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
  6265. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: AbstractValidatorDirective, isStandalone: true, usesOnChanges: true, ngImport: i0 });
  6266. }
  6267. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: AbstractValidatorDirective, decorators: [{
  6268. type: Directive
  6269. }] });
  6270. /**
  6271. * @description
  6272. * Provider which adds `MaxValidator` to the `NG_VALIDATORS` multi-provider list.
  6273. */
  6274. const MAX_VALIDATOR = {
  6275. provide: NG_VALIDATORS,
  6276. useExisting: forwardRef(() => MaxValidator),
  6277. multi: true,
  6278. };
  6279. /**
  6280. * A directive which installs the {@link MaxValidator} for any `formControlName`,
  6281. * `formControl`, or control with `ngModel` that also has a `max` attribute.
  6282. *
  6283. * @see [Form Validation](guide/forms/form-validation)
  6284. *
  6285. * @usageNotes
  6286. *
  6287. * ### Adding a max validator
  6288. *
  6289. * The following example shows how to add a max validator to an input attached to an
  6290. * ngModel binding.
  6291. *
  6292. * ```html
  6293. * <input type="number" ngModel max="4">
  6294. * ```
  6295. *
  6296. * @ngModule ReactiveFormsModule
  6297. * @ngModule FormsModule
  6298. * @publicApi
  6299. */
  6300. class MaxValidator extends AbstractValidatorDirective {
  6301. /**
  6302. * @description
  6303. * Tracks changes to the max bound to this directive.
  6304. */
  6305. max;
  6306. /** @internal */
  6307. inputName = 'max';
  6308. /** @internal */
  6309. normalizeInput = (input) => toFloat(input);
  6310. /** @internal */
  6311. createValidator = (max) => maxValidator(max);
  6312. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: MaxValidator, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  6313. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: MaxValidator, isStandalone: false, selector: "input[type=number][max][formControlName],input[type=number][max][formControl],input[type=number][max][ngModel]", inputs: { max: "max" }, host: { properties: { "attr.max": "_enabled ? max : null" } }, providers: [MAX_VALIDATOR], usesInheritance: true, ngImport: i0 });
  6314. }
  6315. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: MaxValidator, decorators: [{
  6316. type: Directive,
  6317. args: [{
  6318. selector: 'input[type=number][max][formControlName],input[type=number][max][formControl],input[type=number][max][ngModel]',
  6319. providers: [MAX_VALIDATOR],
  6320. host: { '[attr.max]': '_enabled ? max : null' },
  6321. standalone: false,
  6322. }]
  6323. }], propDecorators: { max: [{
  6324. type: Input
  6325. }] } });
  6326. /**
  6327. * @description
  6328. * Provider which adds `MinValidator` to the `NG_VALIDATORS` multi-provider list.
  6329. */
  6330. const MIN_VALIDATOR = {
  6331. provide: NG_VALIDATORS,
  6332. useExisting: forwardRef(() => MinValidator),
  6333. multi: true,
  6334. };
  6335. /**
  6336. * A directive which installs the {@link MinValidator} for any `formControlName`,
  6337. * `formControl`, or control with `ngModel` that also has a `min` attribute.
  6338. *
  6339. * @see [Form Validation](guide/forms/form-validation)
  6340. *
  6341. * @usageNotes
  6342. *
  6343. * ### Adding a min validator
  6344. *
  6345. * The following example shows how to add a min validator to an input attached to an
  6346. * ngModel binding.
  6347. *
  6348. * ```html
  6349. * <input type="number" ngModel min="4">
  6350. * ```
  6351. *
  6352. * @ngModule ReactiveFormsModule
  6353. * @ngModule FormsModule
  6354. * @publicApi
  6355. */
  6356. class MinValidator extends AbstractValidatorDirective {
  6357. /**
  6358. * @description
  6359. * Tracks changes to the min bound to this directive.
  6360. */
  6361. min;
  6362. /** @internal */
  6363. inputName = 'min';
  6364. /** @internal */
  6365. normalizeInput = (input) => toFloat(input);
  6366. /** @internal */
  6367. createValidator = (min) => minValidator(min);
  6368. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: MinValidator, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  6369. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: MinValidator, isStandalone: false, selector: "input[type=number][min][formControlName],input[type=number][min][formControl],input[type=number][min][ngModel]", inputs: { min: "min" }, host: { properties: { "attr.min": "_enabled ? min : null" } }, providers: [MIN_VALIDATOR], usesInheritance: true, ngImport: i0 });
  6370. }
  6371. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: MinValidator, decorators: [{
  6372. type: Directive,
  6373. args: [{
  6374. selector: 'input[type=number][min][formControlName],input[type=number][min][formControl],input[type=number][min][ngModel]',
  6375. providers: [MIN_VALIDATOR],
  6376. host: { '[attr.min]': '_enabled ? min : null' },
  6377. standalone: false,
  6378. }]
  6379. }], propDecorators: { min: [{
  6380. type: Input
  6381. }] } });
  6382. /**
  6383. * @description
  6384. * Provider which adds `RequiredValidator` to the `NG_VALIDATORS` multi-provider list.
  6385. */
  6386. const REQUIRED_VALIDATOR = {
  6387. provide: NG_VALIDATORS,
  6388. useExisting: forwardRef(() => RequiredValidator),
  6389. multi: true,
  6390. };
  6391. /**
  6392. * @description
  6393. * Provider which adds `CheckboxRequiredValidator` to the `NG_VALIDATORS` multi-provider list.
  6394. */
  6395. const CHECKBOX_REQUIRED_VALIDATOR = {
  6396. provide: NG_VALIDATORS,
  6397. useExisting: forwardRef(() => CheckboxRequiredValidator),
  6398. multi: true,
  6399. };
  6400. /**
  6401. * @description
  6402. * A directive that adds the `required` validator to any controls marked with the
  6403. * `required` attribute. The directive is provided with the `NG_VALIDATORS` multi-provider list.
  6404. *
  6405. * @see [Form Validation](guide/forms/form-validation)
  6406. *
  6407. * @usageNotes
  6408. *
  6409. * ### Adding a required validator using template-driven forms
  6410. *
  6411. * ```html
  6412. * <input name="fullName" ngModel required>
  6413. * ```
  6414. *
  6415. * @ngModule FormsModule
  6416. * @ngModule ReactiveFormsModule
  6417. * @publicApi
  6418. */
  6419. class RequiredValidator extends AbstractValidatorDirective {
  6420. /**
  6421. * @description
  6422. * Tracks changes to the required attribute bound to this directive.
  6423. */
  6424. required;
  6425. /** @internal */
  6426. inputName = 'required';
  6427. /** @internal */
  6428. normalizeInput = booleanAttribute;
  6429. /** @internal */
  6430. createValidator = (input) => requiredValidator;
  6431. /** @docs-private */
  6432. enabled(input) {
  6433. return input;
  6434. }
  6435. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: RequiredValidator, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  6436. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: RequiredValidator, isStandalone: false, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: { required: "required" }, host: { properties: { "attr.required": "_enabled ? \"\" : null" } }, providers: [REQUIRED_VALIDATOR], usesInheritance: true, ngImport: i0 });
  6437. }
  6438. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: RequiredValidator, decorators: [{
  6439. type: Directive,
  6440. args: [{
  6441. selector: ':not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]',
  6442. providers: [REQUIRED_VALIDATOR],
  6443. host: { '[attr.required]': '_enabled ? "" : null' },
  6444. standalone: false,
  6445. }]
  6446. }], propDecorators: { required: [{
  6447. type: Input
  6448. }] } });
  6449. /**
  6450. * A Directive that adds the `required` validator to checkbox controls marked with the
  6451. * `required` attribute. The directive is provided with the `NG_VALIDATORS` multi-provider list.
  6452. *
  6453. * @see [Form Validation](guide/forms/form-validation)
  6454. *
  6455. * @usageNotes
  6456. *
  6457. * ### Adding a required checkbox validator using template-driven forms
  6458. *
  6459. * The following example shows how to add a checkbox required validator to an input attached to an
  6460. * ngModel binding.
  6461. *
  6462. * ```html
  6463. * <input type="checkbox" name="active" ngModel required>
  6464. * ```
  6465. *
  6466. * @publicApi
  6467. * @ngModule FormsModule
  6468. * @ngModule ReactiveFormsModule
  6469. */
  6470. class CheckboxRequiredValidator extends RequiredValidator {
  6471. /** @internal */
  6472. createValidator = (input) => requiredTrueValidator;
  6473. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CheckboxRequiredValidator, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  6474. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: CheckboxRequiredValidator, isStandalone: false, selector: "input[type=checkbox][required][formControlName],input[type=checkbox][required][formControl],input[type=checkbox][required][ngModel]", host: { properties: { "attr.required": "_enabled ? \"\" : null" } }, providers: [CHECKBOX_REQUIRED_VALIDATOR], usesInheritance: true, ngImport: i0 });
  6475. }
  6476. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: CheckboxRequiredValidator, decorators: [{
  6477. type: Directive,
  6478. args: [{
  6479. selector: 'input[type=checkbox][required][formControlName],input[type=checkbox][required][formControl],input[type=checkbox][required][ngModel]',
  6480. providers: [CHECKBOX_REQUIRED_VALIDATOR],
  6481. host: { '[attr.required]': '_enabled ? "" : null' },
  6482. standalone: false,
  6483. }]
  6484. }] });
  6485. /**
  6486. * @description
  6487. * Provider which adds `EmailValidator` to the `NG_VALIDATORS` multi-provider list.
  6488. */
  6489. const EMAIL_VALIDATOR = {
  6490. provide: NG_VALIDATORS,
  6491. useExisting: forwardRef(() => EmailValidator),
  6492. multi: true,
  6493. };
  6494. /**
  6495. * A directive that adds the `email` validator to controls marked with the
  6496. * `email` attribute. The directive is provided with the `NG_VALIDATORS` multi-provider list.
  6497. *
  6498. * The email validation is based on the WHATWG HTML specification with some enhancements to
  6499. * incorporate more RFC rules. More information can be found on the [Validators.email
  6500. * page](api/forms/Validators#email).
  6501. *
  6502. * @see [Form Validation](guide/forms/form-validation)
  6503. *
  6504. * @usageNotes
  6505. *
  6506. * ### Adding an email validator
  6507. *
  6508. * The following example shows how to add an email validator to an input attached to an ngModel
  6509. * binding.
  6510. *
  6511. * ```html
  6512. * <input type="email" name="email" ngModel email>
  6513. * <input type="email" name="email" ngModel email="true">
  6514. * <input type="email" name="email" ngModel [email]="true">
  6515. * ```
  6516. *
  6517. * @publicApi
  6518. * @ngModule FormsModule
  6519. * @ngModule ReactiveFormsModule
  6520. */
  6521. class EmailValidator extends AbstractValidatorDirective {
  6522. /**
  6523. * @description
  6524. * Tracks changes to the email attribute bound to this directive.
  6525. */
  6526. email;
  6527. /** @internal */
  6528. inputName = 'email';
  6529. /** @internal */
  6530. normalizeInput = booleanAttribute;
  6531. /** @internal */
  6532. createValidator = (input) => emailValidator;
  6533. /** @docs-private */
  6534. enabled(input) {
  6535. return input;
  6536. }
  6537. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: EmailValidator, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  6538. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: EmailValidator, isStandalone: false, selector: "[email][formControlName],[email][formControl],[email][ngModel]", inputs: { email: "email" }, providers: [EMAIL_VALIDATOR], usesInheritance: true, ngImport: i0 });
  6539. }
  6540. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: EmailValidator, decorators: [{
  6541. type: Directive,
  6542. args: [{
  6543. selector: '[email][formControlName],[email][formControl],[email][ngModel]',
  6544. providers: [EMAIL_VALIDATOR],
  6545. standalone: false,
  6546. }]
  6547. }], propDecorators: { email: [{
  6548. type: Input
  6549. }] } });
  6550. /**
  6551. * @description
  6552. * Provider which adds `MinLengthValidator` to the `NG_VALIDATORS` multi-provider list.
  6553. */
  6554. const MIN_LENGTH_VALIDATOR = {
  6555. provide: NG_VALIDATORS,
  6556. useExisting: forwardRef(() => MinLengthValidator),
  6557. multi: true,
  6558. };
  6559. /**
  6560. * A directive that adds minimum length validation to controls marked with the
  6561. * `minlength` attribute. The directive is provided with the `NG_VALIDATORS` multi-provider list.
  6562. *
  6563. * @see [Form Validation](guide/forms/form-validation)
  6564. *
  6565. * @usageNotes
  6566. *
  6567. * ### Adding a minimum length validator
  6568. *
  6569. * The following example shows how to add a minimum length validator to an input attached to an
  6570. * ngModel binding.
  6571. *
  6572. * ```html
  6573. * <input name="firstName" ngModel minlength="4">
  6574. * ```
  6575. *
  6576. * @ngModule ReactiveFormsModule
  6577. * @ngModule FormsModule
  6578. * @publicApi
  6579. */
  6580. class MinLengthValidator extends AbstractValidatorDirective {
  6581. /**
  6582. * @description
  6583. * Tracks changes to the minimum length bound to this directive.
  6584. */
  6585. minlength;
  6586. /** @internal */
  6587. inputName = 'minlength';
  6588. /** @internal */
  6589. normalizeInput = (input) => toInteger(input);
  6590. /** @internal */
  6591. createValidator = (minlength) => minLengthValidator(minlength);
  6592. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: MinLengthValidator, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  6593. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: MinLengthValidator, isStandalone: false, selector: "[minlength][formControlName],[minlength][formControl],[minlength][ngModel]", inputs: { minlength: "minlength" }, host: { properties: { "attr.minlength": "_enabled ? minlength : null" } }, providers: [MIN_LENGTH_VALIDATOR], usesInheritance: true, ngImport: i0 });
  6594. }
  6595. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: MinLengthValidator, decorators: [{
  6596. type: Directive,
  6597. args: [{
  6598. selector: '[minlength][formControlName],[minlength][formControl],[minlength][ngModel]',
  6599. providers: [MIN_LENGTH_VALIDATOR],
  6600. host: { '[attr.minlength]': '_enabled ? minlength : null' },
  6601. standalone: false,
  6602. }]
  6603. }], propDecorators: { minlength: [{
  6604. type: Input
  6605. }] } });
  6606. /**
  6607. * @description
  6608. * Provider which adds `MaxLengthValidator` to the `NG_VALIDATORS` multi-provider list.
  6609. */
  6610. const MAX_LENGTH_VALIDATOR = {
  6611. provide: NG_VALIDATORS,
  6612. useExisting: forwardRef(() => MaxLengthValidator),
  6613. multi: true,
  6614. };
  6615. /**
  6616. * A directive that adds maximum length validation to controls marked with the
  6617. * `maxlength` attribute. The directive is provided with the `NG_VALIDATORS` multi-provider list.
  6618. *
  6619. * @see [Form Validation](guide/forms/form-validation)
  6620. *
  6621. * @usageNotes
  6622. *
  6623. * ### Adding a maximum length validator
  6624. *
  6625. * The following example shows how to add a maximum length validator to an input attached to an
  6626. * ngModel binding.
  6627. *
  6628. * ```html
  6629. * <input name="firstName" ngModel maxlength="25">
  6630. * ```
  6631. *
  6632. * @ngModule ReactiveFormsModule
  6633. * @ngModule FormsModule
  6634. * @publicApi
  6635. */
  6636. class MaxLengthValidator extends AbstractValidatorDirective {
  6637. /**
  6638. * @description
  6639. * Tracks changes to the maximum length bound to this directive.
  6640. */
  6641. maxlength;
  6642. /** @internal */
  6643. inputName = 'maxlength';
  6644. /** @internal */
  6645. normalizeInput = (input) => toInteger(input);
  6646. /** @internal */
  6647. createValidator = (maxlength) => maxLengthValidator(maxlength);
  6648. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: MaxLengthValidator, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  6649. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: MaxLengthValidator, isStandalone: false, selector: "[maxlength][formControlName],[maxlength][formControl],[maxlength][ngModel]", inputs: { maxlength: "maxlength" }, host: { properties: { "attr.maxlength": "_enabled ? maxlength : null" } }, providers: [MAX_LENGTH_VALIDATOR], usesInheritance: true, ngImport: i0 });
  6650. }
  6651. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: MaxLengthValidator, decorators: [{
  6652. type: Directive,
  6653. args: [{
  6654. selector: '[maxlength][formControlName],[maxlength][formControl],[maxlength][ngModel]',
  6655. providers: [MAX_LENGTH_VALIDATOR],
  6656. host: { '[attr.maxlength]': '_enabled ? maxlength : null' },
  6657. standalone: false,
  6658. }]
  6659. }], propDecorators: { maxlength: [{
  6660. type: Input
  6661. }] } });
  6662. /**
  6663. * @description
  6664. * Provider which adds `PatternValidator` to the `NG_VALIDATORS` multi-provider list.
  6665. */
  6666. const PATTERN_VALIDATOR = {
  6667. provide: NG_VALIDATORS,
  6668. useExisting: forwardRef(() => PatternValidator),
  6669. multi: true,
  6670. };
  6671. /**
  6672. * @description
  6673. * A directive that adds regex pattern validation to controls marked with the
  6674. * `pattern` attribute. The regex must match the entire control value.
  6675. * The directive is provided with the `NG_VALIDATORS` multi-provider list.
  6676. *
  6677. * @see [Form Validation](guide/forms/form-validation)
  6678. *
  6679. * @usageNotes
  6680. *
  6681. * ### Adding a pattern validator
  6682. *
  6683. * The following example shows how to add a pattern validator to an input attached to an
  6684. * ngModel binding.
  6685. *
  6686. * ```html
  6687. * <input name="firstName" ngModel pattern="[a-zA-Z ]*">
  6688. * ```
  6689. *
  6690. * @ngModule ReactiveFormsModule
  6691. * @ngModule FormsModule
  6692. * @publicApi
  6693. */
  6694. class PatternValidator extends AbstractValidatorDirective {
  6695. /**
  6696. * @description
  6697. * Tracks changes to the pattern bound to this directive.
  6698. */
  6699. pattern; // This input is always defined, since the name matches selector.
  6700. /** @internal */
  6701. inputName = 'pattern';
  6702. /** @internal */
  6703. normalizeInput = (input) => input;
  6704. /** @internal */
  6705. createValidator = (input) => patternValidator(input);
  6706. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PatternValidator, deps: null, target: i0.ɵɵFactoryTarget.Directive });
  6707. static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.2.13", type: PatternValidator, isStandalone: false, selector: "[pattern][formControlName],[pattern][formControl],[pattern][ngModel]", inputs: { pattern: "pattern" }, host: { properties: { "attr.pattern": "_enabled ? pattern : null" } }, providers: [PATTERN_VALIDATOR], usesInheritance: true, ngImport: i0 });
  6708. }
  6709. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PatternValidator, decorators: [{
  6710. type: Directive,
  6711. args: [{
  6712. selector: '[pattern][formControlName],[pattern][formControl],[pattern][ngModel]',
  6713. providers: [PATTERN_VALIDATOR],
  6714. host: { '[attr.pattern]': '_enabled ? pattern : null' },
  6715. standalone: false,
  6716. }]
  6717. }], propDecorators: { pattern: [{
  6718. type: Input
  6719. }] } });
  6720. const SHARED_FORM_DIRECTIVES = [
  6721. ɵNgNoValidate,
  6722. NgSelectOption,
  6723. ɵNgSelectMultipleOption,
  6724. DefaultValueAccessor,
  6725. NumberValueAccessor,
  6726. RangeValueAccessor,
  6727. CheckboxControlValueAccessor,
  6728. SelectControlValueAccessor,
  6729. SelectMultipleControlValueAccessor,
  6730. RadioControlValueAccessor,
  6731. NgControlStatus,
  6732. NgControlStatusGroup,
  6733. RequiredValidator,
  6734. MinLengthValidator,
  6735. MaxLengthValidator,
  6736. PatternValidator,
  6737. CheckboxRequiredValidator,
  6738. EmailValidator,
  6739. MinValidator,
  6740. MaxValidator,
  6741. ];
  6742. const TEMPLATE_DRIVEN_DIRECTIVES = [NgModel, NgModelGroup, NgForm];
  6743. const REACTIVE_DRIVEN_DIRECTIVES = [
  6744. FormControlDirective,
  6745. FormGroupDirective,
  6746. FormControlName,
  6747. FormGroupName,
  6748. FormArrayName,
  6749. ];
  6750. /**
  6751. * Internal module used for sharing directives between FormsModule and ReactiveFormsModule
  6752. */
  6753. class ɵInternalFormsSharedModule {
  6754. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: ɵInternalFormsSharedModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  6755. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.13", ngImport: i0, type: ɵInternalFormsSharedModule, declarations: [ɵNgNoValidate,
  6756. NgSelectOption,
  6757. ɵNgSelectMultipleOption,
  6758. DefaultValueAccessor,
  6759. NumberValueAccessor,
  6760. RangeValueAccessor,
  6761. CheckboxControlValueAccessor,
  6762. SelectControlValueAccessor,
  6763. SelectMultipleControlValueAccessor,
  6764. RadioControlValueAccessor,
  6765. NgControlStatus,
  6766. NgControlStatusGroup,
  6767. RequiredValidator,
  6768. MinLengthValidator,
  6769. MaxLengthValidator,
  6770. PatternValidator,
  6771. CheckboxRequiredValidator,
  6772. EmailValidator,
  6773. MinValidator,
  6774. MaxValidator], exports: [ɵNgNoValidate,
  6775. NgSelectOption,
  6776. ɵNgSelectMultipleOption,
  6777. DefaultValueAccessor,
  6778. NumberValueAccessor,
  6779. RangeValueAccessor,
  6780. CheckboxControlValueAccessor,
  6781. SelectControlValueAccessor,
  6782. SelectMultipleControlValueAccessor,
  6783. RadioControlValueAccessor,
  6784. NgControlStatus,
  6785. NgControlStatusGroup,
  6786. RequiredValidator,
  6787. MinLengthValidator,
  6788. MaxLengthValidator,
  6789. PatternValidator,
  6790. CheckboxRequiredValidator,
  6791. EmailValidator,
  6792. MinValidator,
  6793. MaxValidator] });
  6794. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: ɵInternalFormsSharedModule });
  6795. }
  6796. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: ɵInternalFormsSharedModule, decorators: [{
  6797. type: NgModule,
  6798. args: [{
  6799. declarations: SHARED_FORM_DIRECTIVES,
  6800. exports: SHARED_FORM_DIRECTIVES,
  6801. }]
  6802. }] });
  6803. /**
  6804. * Tracks the value and validity state of an array of `FormControl`,
  6805. * `FormGroup` or `FormArray` instances.
  6806. *
  6807. * A `FormArray` aggregates the values of each child `FormControl` into an array.
  6808. * It calculates its status by reducing the status values of its children. For example, if one of
  6809. * the controls in a `FormArray` is invalid, the entire array becomes invalid.
  6810. *
  6811. * `FormArray` accepts one generic argument, which is the type of the controls inside.
  6812. * If you need a heterogenous array, use {@link UntypedFormArray}.
  6813. *
  6814. * `FormArray` is one of the four fundamental building blocks used to define forms in Angular,
  6815. * along with `FormControl`, `FormGroup`, and `FormRecord`.
  6816. *
  6817. * @usageNotes
  6818. *
  6819. * ### Create an array of form controls
  6820. *
  6821. * ```ts
  6822. * const arr = new FormArray([
  6823. * new FormControl('Nancy', Validators.minLength(2)),
  6824. * new FormControl('Drew'),
  6825. * ]);
  6826. *
  6827. * console.log(arr.value); // ['Nancy', 'Drew']
  6828. * console.log(arr.status); // 'VALID'
  6829. * ```
  6830. *
  6831. * ### Create a form array with array-level validators
  6832. *
  6833. * You include array-level validators and async validators. These come in handy
  6834. * when you want to perform validation that considers the value of more than one child
  6835. * control.
  6836. *
  6837. * The two types of validators are passed in separately as the second and third arg
  6838. * respectively, or together as part of an options object.
  6839. *
  6840. * ```ts
  6841. * const arr = new FormArray([
  6842. * new FormControl('Nancy'),
  6843. * new FormControl('Drew')
  6844. * ], {validators: myValidator, asyncValidators: myAsyncValidator});
  6845. * ```
  6846. *
  6847. * ### Set the updateOn property for all controls in a form array
  6848. *
  6849. * The options object is used to set a default value for each child
  6850. * control's `updateOn` property. If you set `updateOn` to `'blur'` at the
  6851. * array level, all child controls default to 'blur', unless the child
  6852. * has explicitly specified a different `updateOn` value.
  6853. *
  6854. * ```ts
  6855. * const arr = new FormArray([
  6856. * new FormControl()
  6857. * ], {updateOn: 'blur'});
  6858. * ```
  6859. *
  6860. * ### Adding or removing controls from a form array
  6861. *
  6862. * To change the controls in the array, use the `push`, `insert`, `removeAt` or `clear` methods
  6863. * in `FormArray` itself. These methods ensure the controls are properly tracked in the
  6864. * form's hierarchy. Do not modify the array of `AbstractControl`s used to instantiate
  6865. * the `FormArray` directly, as that result in strange and unexpected behavior such
  6866. * as broken change detection.
  6867. *
  6868. * @publicApi
  6869. */
  6870. class FormArray extends AbstractControl {
  6871. /**
  6872. * Creates a new `FormArray` instance.
  6873. *
  6874. * @param controls An array of child controls. Each child control is given an index
  6875. * where it is registered.
  6876. *
  6877. * @param validatorOrOpts A synchronous validator function, or an array of
  6878. * such functions, or an `AbstractControlOptions` object that contains validation functions
  6879. * and a validation trigger.
  6880. *
  6881. * @param asyncValidator A single async validator or array of async validator functions
  6882. *
  6883. */
  6884. constructor(controls, validatorOrOpts, asyncValidator) {
  6885. super(pickValidators(validatorOrOpts), pickAsyncValidators(asyncValidator, validatorOrOpts));
  6886. this.controls = controls;
  6887. this._initObservables();
  6888. this._setUpdateStrategy(validatorOrOpts);
  6889. this._setUpControls();
  6890. this.updateValueAndValidity({
  6891. onlySelf: true,
  6892. // If `asyncValidator` is present, it will trigger control status change from `PENDING` to
  6893. // `VALID` or `INVALID`.
  6894. // The status should be broadcasted via the `statusChanges` observable, so we set `emitEvent`
  6895. // to `true` to allow that during the control creation process.
  6896. emitEvent: !!this.asyncValidator,
  6897. });
  6898. }
  6899. controls;
  6900. /**
  6901. * Get the `AbstractControl` at the given `index` in the array.
  6902. *
  6903. * @param index Index in the array to retrieve the control. If `index` is negative, it will wrap
  6904. * around from the back, and if index is greatly negative (less than `-length`), the result is
  6905. * undefined. This behavior is the same as `Array.at(index)`.
  6906. */
  6907. at(index) {
  6908. return this.controls[this._adjustIndex(index)];
  6909. }
  6910. /**
  6911. * Insert a new `AbstractControl` at the end of the array.
  6912. *
  6913. * @param control Form control to be inserted
  6914. * @param options Specifies whether this FormArray instance should emit events after a new
  6915. * control is added.
  6916. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  6917. * `valueChanges` observables emit events with the latest status and value when the control is
  6918. * inserted. When false, no events are emitted.
  6919. */
  6920. push(control, options = {}) {
  6921. this.controls.push(control);
  6922. this._registerControl(control);
  6923. this.updateValueAndValidity({ emitEvent: options.emitEvent });
  6924. this._onCollectionChange();
  6925. }
  6926. /**
  6927. * Insert a new `AbstractControl` at the given `index` in the array.
  6928. *
  6929. * @param index Index in the array to insert the control. If `index` is negative, wraps around
  6930. * from the back. If `index` is greatly negative (less than `-length`), prepends to the array.
  6931. * This behavior is the same as `Array.splice(index, 0, control)`.
  6932. * @param control Form control to be inserted
  6933. * @param options Specifies whether this FormArray instance should emit events after a new
  6934. * control is inserted.
  6935. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  6936. * `valueChanges` observables emit events with the latest status and value when the control is
  6937. * inserted. When false, no events are emitted.
  6938. */
  6939. insert(index, control, options = {}) {
  6940. this.controls.splice(index, 0, control);
  6941. this._registerControl(control);
  6942. this.updateValueAndValidity({ emitEvent: options.emitEvent });
  6943. }
  6944. /**
  6945. * Remove the control at the given `index` in the array.
  6946. *
  6947. * @param index Index in the array to remove the control. If `index` is negative, wraps around
  6948. * from the back. If `index` is greatly negative (less than `-length`), removes the first
  6949. * element. This behavior is the same as `Array.splice(index, 1)`.
  6950. * @param options Specifies whether this FormArray instance should emit events after a
  6951. * control is removed.
  6952. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  6953. * `valueChanges` observables emit events with the latest status and value when the control is
  6954. * removed. When false, no events are emitted.
  6955. */
  6956. removeAt(index, options = {}) {
  6957. // Adjust the index, then clamp it at no less than 0 to prevent undesired underflows.
  6958. let adjustedIndex = this._adjustIndex(index);
  6959. if (adjustedIndex < 0)
  6960. adjustedIndex = 0;
  6961. if (this.controls[adjustedIndex])
  6962. this.controls[adjustedIndex]._registerOnCollectionChange(() => { });
  6963. this.controls.splice(adjustedIndex, 1);
  6964. this.updateValueAndValidity({ emitEvent: options.emitEvent });
  6965. }
  6966. /**
  6967. * Replace an existing control.
  6968. *
  6969. * @param index Index in the array to replace the control. If `index` is negative, wraps around
  6970. * from the back. If `index` is greatly negative (less than `-length`), replaces the first
  6971. * element. This behavior is the same as `Array.splice(index, 1, control)`.
  6972. * @param control The `AbstractControl` control to replace the existing control
  6973. * @param options Specifies whether this FormArray instance should emit events after an
  6974. * existing control is replaced with a new one.
  6975. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  6976. * `valueChanges` observables emit events with the latest status and value when the control is
  6977. * replaced with a new one. When false, no events are emitted.
  6978. */
  6979. setControl(index, control, options = {}) {
  6980. // Adjust the index, then clamp it at no less than 0 to prevent undesired underflows.
  6981. let adjustedIndex = this._adjustIndex(index);
  6982. if (adjustedIndex < 0)
  6983. adjustedIndex = 0;
  6984. if (this.controls[adjustedIndex])
  6985. this.controls[adjustedIndex]._registerOnCollectionChange(() => { });
  6986. this.controls.splice(adjustedIndex, 1);
  6987. if (control) {
  6988. this.controls.splice(adjustedIndex, 0, control);
  6989. this._registerControl(control);
  6990. }
  6991. this.updateValueAndValidity({ emitEvent: options.emitEvent });
  6992. this._onCollectionChange();
  6993. }
  6994. /**
  6995. * Length of the control array.
  6996. */
  6997. get length() {
  6998. return this.controls.length;
  6999. }
  7000. /**
  7001. * Sets the value of the `FormArray`. It accepts an array that matches
  7002. * the structure of the control.
  7003. *
  7004. * This method performs strict checks, and throws an error if you try
  7005. * to set the value of a control that doesn't exist or if you exclude the
  7006. * value of a control.
  7007. *
  7008. * @usageNotes
  7009. * ### Set the values for the controls in the form array
  7010. *
  7011. * ```ts
  7012. * const arr = new FormArray([
  7013. * new FormControl(),
  7014. * new FormControl()
  7015. * ]);
  7016. * console.log(arr.value); // [null, null]
  7017. *
  7018. * arr.setValue(['Nancy', 'Drew']);
  7019. * console.log(arr.value); // ['Nancy', 'Drew']
  7020. * ```
  7021. *
  7022. * @param value Array of values for the controls
  7023. * @param options Configure options that determine how the control propagates changes and
  7024. * emits events after the value changes
  7025. *
  7026. * * `onlySelf`: When true, each change only affects this control, and not its parent. Default
  7027. * is false.
  7028. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  7029. * `valueChanges`
  7030. * observables emit events with the latest status and value when the control value is updated.
  7031. * When false, no events are emitted.
  7032. * The configuration options are passed to the {@link AbstractControl#updateValueAndValidity
  7033. * updateValueAndValidity} method.
  7034. */
  7035. setValue(value, options = {}) {
  7036. assertAllValuesPresent(this, false, value);
  7037. value.forEach((newValue, index) => {
  7038. assertControlPresent(this, false, index);
  7039. this.at(index).setValue(newValue, { onlySelf: true, emitEvent: options.emitEvent });
  7040. });
  7041. this.updateValueAndValidity(options);
  7042. }
  7043. /**
  7044. * Patches the value of the `FormArray`. It accepts an array that matches the
  7045. * structure of the control, and does its best to match the values to the correct
  7046. * controls in the group.
  7047. *
  7048. * It accepts both super-sets and sub-sets of the array without throwing an error.
  7049. *
  7050. * @usageNotes
  7051. * ### Patch the values for controls in a form array
  7052. *
  7053. * ```ts
  7054. * const arr = new FormArray([
  7055. * new FormControl(),
  7056. * new FormControl()
  7057. * ]);
  7058. * console.log(arr.value); // [null, null]
  7059. *
  7060. * arr.patchValue(['Nancy']);
  7061. * console.log(arr.value); // ['Nancy', null]
  7062. * ```
  7063. *
  7064. * @param value Array of latest values for the controls
  7065. * @param options Configure options that determine how the control propagates changes and
  7066. * emits events after the value changes
  7067. *
  7068. * * `onlySelf`: When true, each change only affects this control, and not its parent. Default
  7069. * is false.
  7070. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  7071. * `valueChanges` observables emit events with the latest status and value when the control
  7072. * value is updated. When false, no events are emitted. The configuration options are passed to
  7073. * the {@link AbstractControl#updateValueAndValidity updateValueAndValidity} method.
  7074. */
  7075. patchValue(value, options = {}) {
  7076. // Even though the `value` argument type doesn't allow `null` and `undefined` values, the
  7077. // `patchValue` can be called recursively and inner data structures might have these values,
  7078. // so we just ignore such cases when a field containing FormArray instance receives `null` or
  7079. // `undefined` as a value.
  7080. if (value == null /* both `null` and `undefined` */)
  7081. return;
  7082. value.forEach((newValue, index) => {
  7083. if (this.at(index)) {
  7084. this.at(index).patchValue(newValue, { onlySelf: true, emitEvent: options.emitEvent });
  7085. }
  7086. });
  7087. this.updateValueAndValidity(options);
  7088. }
  7089. /**
  7090. * Resets the `FormArray` and all descendants are marked `pristine` and `untouched`, and the
  7091. * value of all descendants to null or null maps.
  7092. *
  7093. * You reset to a specific form state by passing in an array of states
  7094. * that matches the structure of the control. The state is a standalone value
  7095. * or a form state object with both a value and a disabled status.
  7096. *
  7097. * @usageNotes
  7098. * ### Reset the values in a form array
  7099. *
  7100. * ```ts
  7101. * const arr = new FormArray([
  7102. * new FormControl(),
  7103. * new FormControl()
  7104. * ]);
  7105. * arr.reset(['name', 'last name']);
  7106. *
  7107. * console.log(arr.value); // ['name', 'last name']
  7108. * ```
  7109. *
  7110. * ### Reset the values in a form array and the disabled status for the first control
  7111. *
  7112. * ```ts
  7113. * arr.reset([
  7114. * {value: 'name', disabled: true},
  7115. * 'last'
  7116. * ]);
  7117. *
  7118. * console.log(arr.value); // ['last']
  7119. * console.log(arr.at(0).status); // 'DISABLED'
  7120. * ```
  7121. *
  7122. * @param value Array of values for the controls
  7123. * @param options Configure options that determine how the control propagates changes and
  7124. * emits events after the value changes
  7125. *
  7126. * * `onlySelf`: When true, each change only affects this control, and not its parent. Default
  7127. * is false.
  7128. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  7129. * `valueChanges`
  7130. * observables emit events with the latest status and value when the control is reset.
  7131. * When false, no events are emitted.
  7132. * The configuration options are passed to the {@link AbstractControl#updateValueAndValidity
  7133. * updateValueAndValidity} method.
  7134. */
  7135. reset(value = [], options = {}) {
  7136. this._forEachChild((control, index) => {
  7137. control.reset(value[index], { onlySelf: true, emitEvent: options.emitEvent });
  7138. });
  7139. this._updatePristine(options, this);
  7140. this._updateTouched(options, this);
  7141. this.updateValueAndValidity(options);
  7142. }
  7143. /**
  7144. * The aggregate value of the array, including any disabled controls.
  7145. *
  7146. * Reports all values regardless of disabled status.
  7147. */
  7148. getRawValue() {
  7149. return this.controls.map((control) => control.getRawValue());
  7150. }
  7151. /**
  7152. * Remove all controls in the `FormArray`.
  7153. *
  7154. * @param options Specifies whether this FormArray instance should emit events after all
  7155. * controls are removed.
  7156. * * `emitEvent`: When true or not supplied (the default), both the `statusChanges` and
  7157. * `valueChanges` observables emit events with the latest status and value when all controls
  7158. * in this FormArray instance are removed. When false, no events are emitted.
  7159. *
  7160. * @usageNotes
  7161. * ### Remove all elements from a FormArray
  7162. *
  7163. * ```ts
  7164. * const arr = new FormArray([
  7165. * new FormControl(),
  7166. * new FormControl()
  7167. * ]);
  7168. * console.log(arr.length); // 2
  7169. *
  7170. * arr.clear();
  7171. * console.log(arr.length); // 0
  7172. * ```
  7173. *
  7174. * It's a simpler and more efficient alternative to removing all elements one by one:
  7175. *
  7176. * ```ts
  7177. * const arr = new FormArray([
  7178. * new FormControl(),
  7179. * new FormControl()
  7180. * ]);
  7181. *
  7182. * while (arr.length) {
  7183. * arr.removeAt(0);
  7184. * }
  7185. * ```
  7186. */
  7187. clear(options = {}) {
  7188. if (this.controls.length < 1)
  7189. return;
  7190. this._forEachChild((control) => control._registerOnCollectionChange(() => { }));
  7191. this.controls.splice(0);
  7192. this.updateValueAndValidity({ emitEvent: options.emitEvent });
  7193. }
  7194. /**
  7195. * Adjusts a negative index by summing it with the length of the array. For very negative
  7196. * indices, the result may remain negative.
  7197. * @internal
  7198. */
  7199. _adjustIndex(index) {
  7200. return index < 0 ? index + this.length : index;
  7201. }
  7202. /** @internal */
  7203. _syncPendingControls() {
  7204. let subtreeUpdated = this.controls.reduce((updated, child) => {
  7205. return child._syncPendingControls() ? true : updated;
  7206. }, false);
  7207. if (subtreeUpdated)
  7208. this.updateValueAndValidity({ onlySelf: true });
  7209. return subtreeUpdated;
  7210. }
  7211. /** @internal */
  7212. _forEachChild(cb) {
  7213. this.controls.forEach((control, index) => {
  7214. cb(control, index);
  7215. });
  7216. }
  7217. /** @internal */
  7218. _updateValue() {
  7219. this.value = this.controls
  7220. .filter((control) => control.enabled || this.disabled)
  7221. .map((control) => control.value);
  7222. }
  7223. /** @internal */
  7224. _anyControls(condition) {
  7225. return this.controls.some((control) => control.enabled && condition(control));
  7226. }
  7227. /** @internal */
  7228. _setUpControls() {
  7229. this._forEachChild((control) => this._registerControl(control));
  7230. }
  7231. /** @internal */
  7232. _allControlsDisabled() {
  7233. for (const control of this.controls) {
  7234. if (control.enabled)
  7235. return false;
  7236. }
  7237. return this.controls.length > 0 || this.disabled;
  7238. }
  7239. _registerControl(control) {
  7240. control.setParent(this);
  7241. control._registerOnCollectionChange(this._onCollectionChange);
  7242. }
  7243. /** @internal */
  7244. _find(name) {
  7245. return this.at(name) ?? null;
  7246. }
  7247. }
  7248. const UntypedFormArray = FormArray;
  7249. /**
  7250. * @description
  7251. * Asserts that the given control is an instance of `FormArray`
  7252. *
  7253. * @publicApi
  7254. */
  7255. const isFormArray = (control) => control instanceof FormArray;
  7256. function isAbstractControlOptions(options) {
  7257. return (!!options &&
  7258. (options.asyncValidators !== undefined ||
  7259. options.validators !== undefined ||
  7260. options.updateOn !== undefined));
  7261. }
  7262. /**
  7263. * @description
  7264. * Creates an `AbstractControl` from a user-specified configuration.
  7265. *
  7266. * The `FormBuilder` provides syntactic sugar that shortens creating instances of a
  7267. * `FormControl`, `FormGroup`, or `FormArray`. It reduces the amount of boilerplate needed to
  7268. * build complex forms.
  7269. *
  7270. * @see [Reactive Forms Guide](guide/forms/reactive-forms)
  7271. *
  7272. * @publicApi
  7273. */
  7274. class FormBuilder {
  7275. useNonNullable = false;
  7276. /**
  7277. * @description
  7278. * Returns a FormBuilder in which automatically constructed `FormControl` elements
  7279. * have `{nonNullable: true}` and are non-nullable.
  7280. *
  7281. * **Constructing non-nullable controls**
  7282. *
  7283. * When constructing a control, it will be non-nullable, and will reset to its initial value.
  7284. *
  7285. * ```ts
  7286. * let nnfb = new FormBuilder().nonNullable;
  7287. * let name = nnfb.control('Alex'); // FormControl<string>
  7288. * name.reset();
  7289. * console.log(name); // 'Alex'
  7290. * ```
  7291. *
  7292. * **Constructing non-nullable groups or arrays**
  7293. *
  7294. * When constructing a group or array, all automatically created inner controls will be
  7295. * non-nullable, and will reset to their initial values.
  7296. *
  7297. * ```ts
  7298. * let nnfb = new FormBuilder().nonNullable;
  7299. * let name = nnfb.group({who: 'Alex'}); // FormGroup<{who: FormControl<string>}>
  7300. * name.reset();
  7301. * console.log(name); // {who: 'Alex'}
  7302. * ```
  7303. * **Constructing *nullable* fields on groups or arrays**
  7304. *
  7305. * It is still possible to have a nullable field. In particular, any `FormControl` which is
  7306. * *already* constructed will not be altered. For example:
  7307. *
  7308. * ```ts
  7309. * let nnfb = new FormBuilder().nonNullable;
  7310. * // FormGroup<{who: FormControl<string|null>}>
  7311. * let name = nnfb.group({who: new FormControl('Alex')});
  7312. * name.reset(); console.log(name); // {who: null}
  7313. * ```
  7314. *
  7315. * Because the inner control is constructed explicitly by the caller, the builder has
  7316. * no control over how it is created, and cannot exclude the `null`.
  7317. */
  7318. get nonNullable() {
  7319. const nnfb = new FormBuilder();
  7320. nnfb.useNonNullable = true;
  7321. return nnfb;
  7322. }
  7323. group(controls, options = null) {
  7324. const reducedControls = this._reduceControls(controls);
  7325. let newOptions = {};
  7326. if (isAbstractControlOptions(options)) {
  7327. // `options` are `AbstractControlOptions`
  7328. newOptions = options;
  7329. }
  7330. else if (options !== null) {
  7331. // `options` are legacy form group options
  7332. newOptions.validators = options.validator;
  7333. newOptions.asyncValidators = options.asyncValidator;
  7334. }
  7335. return new FormGroup(reducedControls, newOptions);
  7336. }
  7337. /**
  7338. * @description
  7339. * Constructs a new `FormRecord` instance. Accepts a single generic argument, which is an object
  7340. * containing all the keys and corresponding inner control types.
  7341. *
  7342. * @param controls A collection of child controls. The key for each child is the name
  7343. * under which it is registered.
  7344. *
  7345. * @param options Configuration options object for the `FormRecord`. The object should have the
  7346. * `AbstractControlOptions` type and might contain the following fields:
  7347. * * `validators`: A synchronous validator function, or an array of validator functions.
  7348. * * `asyncValidators`: A single async validator or array of async validator functions.
  7349. * * `updateOn`: The event upon which the control should be updated (options: 'change' | 'blur'
  7350. * | submit').
  7351. */
  7352. record(controls, options = null) {
  7353. const reducedControls = this._reduceControls(controls);
  7354. // Cast to `any` because the inferred types are not as specific as Element.
  7355. return new FormRecord(reducedControls, options);
  7356. }
  7357. /**
  7358. * @description
  7359. * Constructs a new `FormControl` with the given state, validators and options. Sets
  7360. * `{nonNullable: true}` in the options to get a non-nullable control. Otherwise, the
  7361. * control will be nullable. Accepts a single generic argument, which is the type of the
  7362. * control's value.
  7363. *
  7364. * @param formState Initializes the control with an initial state value, or
  7365. * with an object that contains both a value and a disabled status.
  7366. *
  7367. * @param validatorOrOpts A synchronous validator function, or an array of
  7368. * such functions, or a `FormControlOptions` object that contains
  7369. * validation functions and a validation trigger.
  7370. *
  7371. * @param asyncValidator A single async validator or array of async validator
  7372. * functions.
  7373. *
  7374. * @usageNotes
  7375. *
  7376. * ### Initialize a control as disabled
  7377. *
  7378. * The following example returns a control with an initial value in a disabled state.
  7379. *
  7380. * {@example forms/ts/formBuilder/form_builder_example.ts region='disabled-control'}
  7381. */
  7382. control(formState, validatorOrOpts, asyncValidator) {
  7383. let newOptions = {};
  7384. if (!this.useNonNullable) {
  7385. return new FormControl(formState, validatorOrOpts, asyncValidator);
  7386. }
  7387. if (isAbstractControlOptions(validatorOrOpts)) {
  7388. // If the second argument is options, then they are copied.
  7389. newOptions = validatorOrOpts;
  7390. }
  7391. else {
  7392. // If the other arguments are validators, they are copied into an options object.
  7393. newOptions.validators = validatorOrOpts;
  7394. newOptions.asyncValidators = asyncValidator;
  7395. }
  7396. return new FormControl(formState, { ...newOptions, nonNullable: true });
  7397. }
  7398. /**
  7399. * Constructs a new `FormArray` from the given array of configurations,
  7400. * validators and options. Accepts a single generic argument, which is the type of each control
  7401. * inside the array.
  7402. *
  7403. * @param controls An array of child controls or control configs. Each child control is given an
  7404. * index when it is registered.
  7405. *
  7406. * @param validatorOrOpts A synchronous validator function, or an array of such functions, or an
  7407. * `AbstractControlOptions` object that contains
  7408. * validation functions and a validation trigger.
  7409. *
  7410. * @param asyncValidator A single async validator or array of async validator functions.
  7411. */
  7412. array(controls, validatorOrOpts, asyncValidator) {
  7413. const createdControls = controls.map((c) => this._createControl(c));
  7414. // Cast to `any` because the inferred types are not as specific as Element.
  7415. return new FormArray(createdControls, validatorOrOpts, asyncValidator);
  7416. }
  7417. /** @internal */
  7418. _reduceControls(controls) {
  7419. const createdControls = {};
  7420. Object.keys(controls).forEach((controlName) => {
  7421. createdControls[controlName] = this._createControl(controls[controlName]);
  7422. });
  7423. return createdControls;
  7424. }
  7425. /** @internal */
  7426. _createControl(controls) {
  7427. if (controls instanceof FormControl) {
  7428. return controls;
  7429. }
  7430. else if (controls instanceof AbstractControl) {
  7431. // A control; just return it
  7432. return controls;
  7433. }
  7434. else if (Array.isArray(controls)) {
  7435. // ControlConfig Tuple
  7436. const value = controls[0];
  7437. const validator = controls.length > 1 ? controls[1] : null;
  7438. const asyncValidator = controls.length > 2 ? controls[2] : null;
  7439. return this.control(value, validator, asyncValidator);
  7440. }
  7441. else {
  7442. // T or FormControlState<T>
  7443. return this.control(controls);
  7444. }
  7445. }
  7446. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormBuilder, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  7447. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormBuilder, providedIn: 'root' });
  7448. }
  7449. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormBuilder, decorators: [{
  7450. type: Injectable,
  7451. args: [{ providedIn: 'root' }]
  7452. }] });
  7453. /**
  7454. * @description
  7455. * `NonNullableFormBuilder` is similar to {@link FormBuilder}, but automatically constructed
  7456. * {@link FormControl} elements have `{nonNullable: true}` and are non-nullable.
  7457. *
  7458. * @publicApi
  7459. */
  7460. class NonNullableFormBuilder {
  7461. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NonNullableFormBuilder, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
  7462. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NonNullableFormBuilder, providedIn: 'root', useFactory: () => inject(FormBuilder).nonNullable });
  7463. }
  7464. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: NonNullableFormBuilder, decorators: [{
  7465. type: Injectable,
  7466. args: [{
  7467. providedIn: 'root',
  7468. useFactory: () => inject(FormBuilder).nonNullable,
  7469. }]
  7470. }] });
  7471. /**
  7472. * UntypedFormBuilder is the same as `FormBuilder`, but it provides untyped controls.
  7473. */
  7474. class UntypedFormBuilder extends FormBuilder {
  7475. group(controlsConfig, options = null) {
  7476. return super.group(controlsConfig, options);
  7477. }
  7478. /**
  7479. * Like `FormBuilder#control`, except the resulting control is untyped.
  7480. */
  7481. control(formState, validatorOrOpts, asyncValidator) {
  7482. return super.control(formState, validatorOrOpts, asyncValidator);
  7483. }
  7484. /**
  7485. * Like `FormBuilder#array`, except the resulting array is untyped.
  7486. */
  7487. array(controlsConfig, validatorOrOpts, asyncValidator) {
  7488. return super.array(controlsConfig, validatorOrOpts, asyncValidator);
  7489. }
  7490. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: UntypedFormBuilder, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
  7491. static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: UntypedFormBuilder, providedIn: 'root' });
  7492. }
  7493. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: UntypedFormBuilder, decorators: [{
  7494. type: Injectable,
  7495. args: [{ providedIn: 'root' }]
  7496. }] });
  7497. /**
  7498. * @module
  7499. * @description
  7500. * Entry point for all public APIs of the forms package.
  7501. */
  7502. /**
  7503. * @publicApi
  7504. */
  7505. const VERSION = new Version('19.2.13');
  7506. /**
  7507. * Exports the required providers and directives for template-driven forms,
  7508. * making them available for import by NgModules that import this module.
  7509. *
  7510. * @see [Forms Overview](guide/forms)
  7511. * @see [Template-driven Forms Guide](guide/forms)
  7512. *
  7513. * @publicApi
  7514. */
  7515. class FormsModule {
  7516. /**
  7517. * @description
  7518. * Provides options for configuring the forms module.
  7519. *
  7520. * @param opts An object of configuration options
  7521. * * `callSetDisabledState` Configures whether to `always` call `setDisabledState`, which is more
  7522. * correct, or to only call it `whenDisabled`, which is the legacy behavior.
  7523. */
  7524. static withConfig(opts) {
  7525. return {
  7526. ngModule: FormsModule,
  7527. providers: [
  7528. {
  7529. provide: CALL_SET_DISABLED_STATE,
  7530. useValue: opts.callSetDisabledState ?? setDisabledStateDefault,
  7531. },
  7532. ],
  7533. };
  7534. }
  7535. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  7536. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.13", ngImport: i0, type: FormsModule, declarations: [NgModel, NgModelGroup, NgForm], exports: [ɵInternalFormsSharedModule, NgModel, NgModelGroup, NgForm] });
  7537. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormsModule, imports: [ɵInternalFormsSharedModule] });
  7538. }
  7539. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: FormsModule, decorators: [{
  7540. type: NgModule,
  7541. args: [{
  7542. declarations: TEMPLATE_DRIVEN_DIRECTIVES,
  7543. exports: [ɵInternalFormsSharedModule, TEMPLATE_DRIVEN_DIRECTIVES],
  7544. }]
  7545. }] });
  7546. /**
  7547. * Exports the required infrastructure and directives for reactive forms,
  7548. * making them available for import by NgModules that import this module.
  7549. *
  7550. * @see [Forms Overview](guide/forms)
  7551. * @see [Reactive Forms Guide](guide/forms/reactive-forms)
  7552. *
  7553. * @publicApi
  7554. */
  7555. class ReactiveFormsModule {
  7556. /**
  7557. * @description
  7558. * Provides options for configuring the reactive forms module.
  7559. *
  7560. * @param opts An object of configuration options
  7561. * * `warnOnNgModelWithFormControl` Configures when to emit a warning when an `ngModel`
  7562. * binding is used with reactive form directives.
  7563. * * `callSetDisabledState` Configures whether to `always` call `setDisabledState`, which is more
  7564. * correct, or to only call it `whenDisabled`, which is the legacy behavior.
  7565. */
  7566. static withConfig(opts) {
  7567. return {
  7568. ngModule: ReactiveFormsModule,
  7569. providers: [
  7570. {
  7571. provide: NG_MODEL_WITH_FORM_CONTROL_WARNING,
  7572. useValue: opts.warnOnNgModelWithFormControl ?? 'always',
  7573. },
  7574. {
  7575. provide: CALL_SET_DISABLED_STATE,
  7576. useValue: opts.callSetDisabledState ?? setDisabledStateDefault,
  7577. },
  7578. ],
  7579. };
  7580. }
  7581. static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: ReactiveFormsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
  7582. static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.13", ngImport: i0, type: ReactiveFormsModule, declarations: [FormControlDirective, FormGroupDirective, FormControlName, FormGroupName, FormArrayName], exports: [ɵInternalFormsSharedModule, FormControlDirective, FormGroupDirective, FormControlName, FormGroupName, FormArrayName] });
  7583. static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: ReactiveFormsModule, imports: [ɵInternalFormsSharedModule] });
  7584. }
  7585. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: ReactiveFormsModule, decorators: [{
  7586. type: NgModule,
  7587. args: [{
  7588. declarations: [REACTIVE_DRIVEN_DIRECTIVES],
  7589. exports: [ɵInternalFormsSharedModule, REACTIVE_DRIVEN_DIRECTIVES],
  7590. }]
  7591. }] });
  7592. export { AbstractControl, AbstractControlDirective, AbstractFormGroupDirective, COMPOSITION_BUFFER_MODE, CheckboxControlValueAccessor, CheckboxRequiredValidator, ControlContainer, ControlEvent, DefaultValueAccessor, EmailValidator, FormArray, FormArrayName, FormBuilder, FormControl, FormControlDirective, FormControlName, FormGroup, FormGroupDirective, FormGroupName, FormRecord, FormResetEvent, FormSubmittedEvent, FormsModule, MaxLengthValidator, MaxValidator, MinLengthValidator, MinValidator, NG_ASYNC_VALIDATORS, NG_VALIDATORS, NG_VALUE_ACCESSOR, NgControl, NgControlStatus, NgControlStatusGroup, NgForm, NgModel, NgModelGroup, NgSelectOption, NonNullableFormBuilder, NumberValueAccessor, PatternValidator, PristineChangeEvent, RadioControlValueAccessor, RangeValueAccessor, ReactiveFormsModule, RequiredValidator, SelectControlValueAccessor, SelectMultipleControlValueAccessor, StatusChangeEvent, TouchedChangeEvent, UntypedFormArray, UntypedFormBuilder, UntypedFormControl, UntypedFormGroup, VERSION, Validators, ValueChangeEvent, isFormArray, isFormControl, isFormGroup, isFormRecord, ɵInternalFormsSharedModule, ɵNgNoValidate, ɵNgSelectMultipleOption };
  7593. //# sourceMappingURL=forms.mjs.map