Angular forms work beautifully with standard HTML. input, textarea, and select all slot into Reactive Forms without extra effort. The framework understands their events, their values, and their states.
But modern applications rarely get by on standard elements alone. You might need a star rating widget, a composite date selector, or a custom color picker. Drop one of these into a form group, and Angular treats it as dead HTML. patchValue does nothing. Validators ignore it. The form has no idea when a user interacts with the control, and form.disable() leaves the custom widget fully interactive.
This is the problem ControlValueAccessor exists to solve.
What ControlValueAccessor Actually Does
ControlValueAccessor is the contract that turns a custom component into a first-class form citizen. It acts as a translator between the Angular Forms API and your own UI. Once you implement it correctly, your component becomes indistinguishable from a native input from the form’s point of view. It can receive values, emit changes, report touches, and respect disabled states just like a built-in element.
The interface requires four specific methods. Each one handles a distinct direction of communication.
writeValue: Form to Component
writeValue(obj) is the inbound lane. Whenever the form model updates and needs to push a new value into your UI, Angular calls this method. If you invoke patchValue({ rating: 4 }) on a form group, that value of 4 arrives inside your component through writeValue. If you reset the form, writeValue receives the new initial value or null. Your job inside this method is to take that incoming data and map it onto your component’s internal state. If you are building a color picker, writeValue receives a hex string like #ff4400, and you must update your view to show that color as selected.
There is a practical wrinkle here. Angular can call writeValue before your view is fully initialized, especially inside dynamically rendered components, dialogs, or tabbed interfaces. If your component tries to touch the DOM or child components too early, you can hit runtime errors. A solid pattern is to store the value in a local property and apply it after the view initializes, or to guard against undefined child references. Never assume writeValue only fires when your template is stable.
registerOnChange: Component to Form
registerOnChange(fn) sets up the outbound lane. Angular hands you a callback function, and you must keep a reference to it. Every time the user changes the value inside your component, you call that function with the new value. In a star rating component, when the user clicks the third star, you invoke the stored callback with 3. That call flows back into the FormControl, updates the model, triggers any valueChanges subscriptions, and re-runs validators.
Skipping this step is the most common way to silently break a form. The widget might look alive. The user sees stars light up, colors shift, or dates populate. But the form model never updates. Validators continue to evaluate stale data. Submit handlers send old values. The component appears to work, yet the form is effectively blind. If your custom control accepts user input but the surrounding form never notices, this is almost always the culprit.
registerOnTouched: Reporting Interaction
Forms do not just track values. They track whether a user has interacted with a field. Angular uses the touched state to decide when it is appropriate to show validation errors. A required text input should not flash red the instant the page loads. It should wait until the user tabs away or clicks elsewhere.
Native inputs handle this automatically through blur events. Custom components do not. You must use registerOnTouched(fn) to report these interactions yourself. Angular gives you another callback; you call it when you decide the user has meaningfully engaged with the control.
The exact timing depends on your component. For a text-like custom input, you might call it on blur. For a star rating, the first click is probably the right moment. For a color picker that opens a popover, you might wait until the palette closes. The key is consistency. If you never call the touched callback, Angular keeps marking the control as pristine. Validation errors stay hidden even after the user has clearly finished editing. That leads to confusion and poor user experience.
setDisabledState: ફોર્મ કમાન્ડ્સનું પાલન કરવું
ડાયનેમિક ફોર્મ્સ બિઝનેસ લોજિકના આધારે સતત ફીલ્ડ્સને ઇનેબલ (enable) અને ડિસેબલ (disable) કરતા રહે છે. જ્યારે તમે FormControl પર .disable() કોલ કરો છો, ત્યારે Angular ને તમારા કસ્ટમ કમ્પોનન્ટ દ્વારા પ્રતિસાદની જરૂર હોય છે. setDisabledState(isDisabled) એક બુલિયન (boolean) મેળવે છે. જ્યારે તે true હોય, ત્યારે તમારે તમારા UI ને લોક કરી દેવું જોઈએ.
આનો અર્થ માત્ર ક્લિક્સને અવગણવા કરતાં ઘણો વધારે છે. તમારે આંતરિક બટનોને ડિસેબલ કરવા જોઈએ, ફોકસ કરી શકાય તેવી સ્થિતિઓ (focusable states) દૂર કરવી જોઈએ, અને ઓપેસિટી ઘટાડવી અથવા pointer-events: none જેવા વિઝ્યુઅલ ટ્રીટમેન્ટ્સ લાગુ કરવા જોઈએ. જો તમે આ મેથડને અવગણશો, તો તમારો કમ્પોનન્ટ સંપૂર્ણ રીતે ઇન્ટરેક્ટિવ રહેશે જ્યારે ફોર્મ મોડલ એવો આગ્રહ રાખશે કે તે ડિસેબલ છે. આનાથી શોધવા મુશ્કેલ એવા બગ્સ (bugs) પેદા થાય છે. વપરાશકર્તાઓ એવા મૂલ્યોમાં ફેરફાર કરી શકે છે જેને ફોર્મ દ્વારા નકારવામાં આવ્યા હોવા જોઈએ. સેવ બટનો અમાન્ય સ્થિતિઓના આધારે ઇનેબલ થઈ શકે છે. ફોર્મ ગ્રુપ અને UI એકબીજાથી અલગ પડી જાય છે.
એક સારી રીતે બનાવેલ કસ્ટમ કંટ્રોલ setDisabledState ને એક પ્રાથમિક જરૂરિયાત તરીકે જુએ છે, માત્ર વિચાર્યા પછી ઉમેરાયેલી વસ્તુ તરીકે નહીં.
ભૂલો જે તમારો ડિબગિંગ સમય બગાડશે
આ ઇન્ટરફેસથી નવા ડેવલપર્સ વારંવાર કેટલીક ભૂલો કરે છે.
change callback ને કોલ કરવાનું ભૂલી જવું. તમારો કમ્પોનન્ટ તેની આંતરિક સ્થિતિ અપડેટ કરે છે, પરંતુ ફોર્મને તેની જાણ થતી નથી. વેલિડેટર્સ (Validators) અટકી જાય છે, અને પેરેન્ટ ફોર્મ્સ જૂનો (stale) ડેટા સબમિટ કરે છે. જ્યારે વપરાશકર્તા નવું મૂલ્ય સબમિટ કરે ત્યારે હંમેશા તે સ્ટોર કરેલ onChange ફંક્શનને ફાયર કરો.
touched callback ને સ્કીપ કરવું. તેના વગર, Angular કંટ્રોલને ક્યારેય 'touched' તરીકે માર્ક કરતું નથી. touched અથવા dirty સ્ટેટ્સ સાથે જોડાયેલા એરર મેસેજ દેખાતા નથી. વપરાશકર્તાઓ એવા ફોર્મ સામે જોતા રહે છે જે સાચું લાગે છે પરંતુ સબમિટ થતું નથી, અને શું ખોટું છે તેનો કોઈ દેખીતો સંકેત મળતો નથી.
disabled સ્ટેટની અવગણના કરવી. વિઝ્યુઅલી ઇનેબલ દેખાતું કંટ્રોલ જેને ફોર્મ ડિસેબલ માને છે, તે વિશ્વાસની મર્યાદા તોડે છે. વપરાશકર્તા ટાઈપ કરવાનું અથવા ક્લિક કરવાનું ચાલુ રાખી શકે છે, પરંતુ મોડલ તેમને અવગણે છે. અથવા તેનાથી પણ ખરાબ, મોડલ સિંક સાયકલ દરમિયાન તેમના ઇનપુટને અચાનક ઓવરરાઈટ કરી શકે છે.
NG_VALUE_ACCESSOR પ્રોવાઈડરને છોડી દેવું. આ એક છૂપો ખતરો છે. જો તમે ચાર મેથડ્સ અમલમાં મૂકો છો પરંતુ તમારા કમ્પોનન્ટના providers એરેમાં NG_VALUE_ACCESSOR ઉમેરવાનું ભૂલી જાઓ છો, તો Angular તમારા કમ્પોનન્ટને વેલ્યુ એક્સેસર તરીકે ક્યારેય રજિસ્ટર કરતું નથી. કોડ કમ્પાઈલ થાય છે. વ્યુ રેન્ડર થાય છે. પરંતુ કંઈ જ બાઈન્ડ થતું નથી. કોઈ એરર મેસેજ મળતો નથી, ફક્ત એક કમ્પોનન્ટ જે આખેઆખા ફોર્મની બહાર તરતો રહે છે. હંમેશા તેને ડેકોરેટર મેટાડેટામાં સામેલ કરો.
Signals, Validators, અને આધુનિક Angular
ControlValueAccessor એ કોઈ જૂની (legacy) API સપાટી નથી. તે આધુનિક Angular ડેવલપમેન્ટમાં સારી રીતે ફિટ થાય છે. તમે આંતરિક સ્ટેટને Signals, સાદા પ્રોપર્ટીઝ અથવા RxJS subjects દ્વારા મેનેજ કરો, ચાર મેથડ્સ ફોર્મ્સ મોડ્યુલ સાથે તમારો પબ્લિક કોન્ટ્રાક્ટ બની રહે છે. તમે writeValue માં મૂલ્યોનો ઉપયોગ કરો છો, તમારા Signals અથવા સ્ટેટમાં ફેરફાર કરો છો, અને Angular દ્વારા પૂરા પાડવામાં આવતા કોલબેક્સ દ્વારા તેને એમિટ (emit) કરો છો.
સ્ટાન્ડર્ડ વેલિડેટર્સ કોઈપણ ફેરફાર વગર કામ કરે છે. Validators.required, Validators.min, Validators.pattern, અને કસ્ટમ ક્રોસ-ફિલ્ડ વેલિડેટર્સ તમારા CVA-બેક્ડ કમ્પોનન્ટનું મૂલ્યાંકન બરાબર નેટિવ ઇનપુટની જેમ જ કરે છે. ફોર્મ કંટ્રોલ એક મૂલ્ય અને એક સ્ટેટ જુએ છે. તેને તેનાથી કોઈ ફરક પડતો નથી કે તે મૂલ્ય ટેક્સ્ટ બોક્સમાંથી આવ્યું છે કે હેન્ડ-ક્રાફ્ટેડ મંથ-પિકર (month-picker) માંથી.
આ પોર્ટેબિલિટીને કારણે જ ડિઝાઇન સિસ્ટમ્સ અને શેર કરેલી UI લાઇબ્રેરીઓ માટે CVA મહત્વનું છે. એક ટીમ મજબૂત ફોન-નંબર ઇનપુટ અથવા ફાઇલ અપલોડ વિજેટ બનાવે છે. તેઓ એકવાર ઇન્ટરફેસ અમલમાં મૂકે છે. સંસ્થાની અન્ય દરેક ટીમ તેને કોઈપણ વધારાના વાયરિંગ વગર તેમના Reactive Forms માં વાપરી શકે છે. કમ્પોનન્ટ અનુમાનિત રીતે વર્તે છે, સમાન રીતે વેલિડેટ થાય છે, અને દરેક ફીચર મોડ્યુલમાં સુસંગત રીતે ડિસેબલ થાય છે.
મુખ્ય સારાંશ
ControlValueAccessor એ માત્ર ઇન્ટરવ્યુ પ્રશ્નો માટે યાદ રાખવા માટેનું બીજું ઇન્ટરફેસ નથી. તે એવો સેતુ (bridge) છે જે તમારા કસ્ટમ કમ્પોનન્ટ્સને નેટિવ HTML એલિમેન્ટ્સ જેટલા જ સમાન તરીકે Angular ના ફોર્મ ઇકોસિસ્ટમમાં ભાગ લેવા દે છે. તેમાં માસ્ટરી મેળવવાનો અર્થ છે તમારા વિજેટ અને ફોર્મ વચ્ચેની સંપૂર્ણ વાતચીત સમજવી: મૂલ્યો મેળવવા, ફેરફારો રિપોર્ટ કરવા, ટચ (touches) ની જાણ કરવી અને ડિસેબલ સ્ટેટ્સનું પાલન કરવું. આ ચાર બાબતો બરાબર સમજી લો, અને તમે જટિલ, પુનઃઉપયોગી (reusable) ફોર્મ કંટ્રોલ્સ બનાવી શકશો જે તેનો ઉપયોગ કરનારા ડેવલપર્સને અદ્રશ્ય લાગે. તે એક પ્રોફેશનલ Angular કમ્પોનન્ટની ઓળખ છે.
