Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the formControl value with mask applied? #948

Closed
andrebrz opened this issue Nov 18, 2021 · 8 comments
Closed

How to get the formControl value with mask applied? #948

andrebrz opened this issue Nov 18, 2021 · 8 comments

Comments

@andrebrz
Copy link

The mask is working fine to me, the problem is when I'm gonna get a formControl that has it's value masked from component the value doesn't came with mask... How to solve this?

tnx

@leandroneves-net
Copy link
Contributor

I have the same problem.

When I use FormBuilder (1) or patchValue (2), the values are not masked.

(1)
this.f = this.formBuilder.group({
  'numero':                     [ 12.34 ]
});  }

(2) 
this.f.patchValue({'numero': 12.34})

Html Template

<form [formGroup]="f">
    <input type="text" formControlName="numero"  mask="separator.2" thousandSeparator="." nbInput status="primary">
</form>

@leandroneves-net
Copy link
Contributor

leandroneves-net commented Nov 27, 2021

I solve my problem.

First, Its needed to declare decimalMarker.

<form [formGroup]="f">
    <input type="text" formControlName="numero"  mask="separator.2" thousandSeparator="." decimalMarker="," nbInput status="primary">
</form>

Second, my server application send some numbers as strings. So I rewrote "mask.directive.ts" file and changed the line number 449 from if (typeof inputValue === 'number') { to if (typeof inputValue === 'number' || this._maskValue.startsWith('separator')) {

This changes solved the problem for me.

Hope this helps.

@rodineicirineu
Copy link

I solve my problem.

First, Its needed to declare decimalMarker.

<form [formGroup]="f">
    <input type="text" formControlName="numero"  mask="separator.2" thousandSeparator="." decimalMarker="," nbInput status="primary">
</form>

Second, my server application send some numbers as strings. So I rewrote "mask.directive.ts" file and changed the line number 449 from if (typeof inputValue === 'number') { to if (typeof inputValue === 'number' || this._maskValue.startsWith('separator')) {

This changes solved the problem for me.

Hope this helps.

@leandroneves-net
How did you get decimalMarker to work? I've tried it in several ways and it just doesn't work, what version of ngx-mask are you using??

@leandroneves-net
Copy link
Contributor

I tried with the version 12.0.0. I did not try with 13.0.0 yet.

For test propouses only, you can try #957 (applied on lnx-mask-test2 package for angular12.0.0). This is a test package and I will delete this soon.

Best regards,

Leandro

@rodineicirineu
Copy link

@leandroneves-net
How strange, I use version 12.0.0 and for me the decimal doesn't work at all.
I'll check this pr.
Example: "<input required prefix="R$" mask="separator.2" thousandSeparator="." decimalMarker="," [dropSpecialCharacters]="false"
type="string" formControlName="totalValue" class="form-control" placeholder="Digite o valor total">"

@andrebrz
Copy link
Author

andrebrz commented Dec 2, 2021

Hey guys, fortunately I solved this problem, maybe not with an elegant way but it's working pretty good in my project, take a look:

In HTML I added a variable to the input file, just like this:

<input #varIdentificacao type="text" class="form-control"  formControlName="identificacao" mask="{{tituloRepresentacao?.mask}}">

In the component I declared a global variable to access the input variable, like this:

//Global Variable
@ViewChild('varIdentificacao', { static: false }) varIdentificacao: ElementRef;

//Inside the form submit method
let identificacao = this.varIdentificacao.nativeElement.value;

Hope it helps!

@andrebrz andrebrz closed this as completed Feb 3, 2022
GlebChiz added a commit that referenced this issue Apr 18, 2022
NepipenkoIgor pushed a commit that referenced this issue Apr 18, 2022
* fix(mask.directive.ts): parse input as a number if  maskvalue starts
with 'separator'

My server sends numeric values as javascrpt strings. So if separator
mask is used the input string will be treated as a NumberValue.

* fix(mask.directive.ts): test files for parse input as a number if
maskvalue starts with 'separator'

* fix(#948): updated changelog, up version

Co-authored-by: leandro <[email protected]>
Co-authored-by: Leandro Neves <[email protected]>
@NepipenkoIgor
Copy link
Collaborator

Hi @andrebrz @leandroneves-net Thank you that using ngx-mask. Please try version >=13.1.10

@foerstj-dv
Copy link

This causes follow-up issue #1042

Could this maybe be reverted, and people be expected to parse their numbers themselves? Or fix their backends so it doesn't send numbers as strings? ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants