Is it possible to turn validation into form error in Angular?

Asked

Viewed 28 times

0

It’s kind of hard to explain. I have a form that checks a field if it is greater than zero. If so, I show a message to the user. I want to turn this into a form error, because I can only release the save button if the form is error free. I wonder if it’s possible to do that. Follow my source (I could not format in a way that can look cute here):

<form #lancamentoForm="ngForm">
<div class="ui-g-12 ui-md-3 ui-fluid">
            <label>Valor</label>
            <input pInputText type="text" placeholder="0,00" currencyMask name="valor" #valor="ngModel"
                   [options]="{ prefix: '', thousands: '.', decimal: ',', allowNegative: false }" ngModel>
            <div *ngIf="valor.value <= 0 && valor.dirty" class="ui-message ui-message-error">
              Valor deve ser maior que 0
            </div>
          </div>
<button type="submit" pButton label="Salvar" [disabled]="lancamentoForm.invalid"></button>
</form> 

I want to make the message 'Value greater than 0' lancamentForm is invalid for button handling. Is it possible? Thank you!

1 answer

0

Face for this you can create a validation in css class that disables the button if the value is less than or equal to 0.

And for that just use the ng-disabled on the button and would be next to it:

<button ng-model="button" ng-disabled="valor.value <= 0 && valor.dirty">Button</button>

References

  • I liked the idea Lucas, but then I would have to do a validation exclusive for this check, right? There are already several other fields that I already do a treatment (for example, by required or minlength) where the form is already valid or invalid. I would like to take advantage of this 'generic' check and add my need for this value field where I check if it is greater than 0.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.