Giter Site home page Giter Site logo

Comments (8)

liamdebeasi avatar liamdebeasi commented on April 28, 2024

Thanks for the report. How were you doing things with the deprecated helper/error slots? From the example you provided it looks like you would still need to conditionally render text based on the validation state. For the helprText property, you could extract this logic into a function so you don't need to do nested ternaries.

from ionic-framework.

Marius-Romanus avatar Marius-Romanus commented on April 28, 2024

Hello, I have never really used the helper, I have put it because they go together in the documentation (although I suppose it could also be the case that you want different help depending on an if). The one I use is the error, in version 6 it had something similar to this:

<ion-item>
  <ion-label position="floating">Email</ion-label>
  <ion-input formControlName="email" type="email" clearOnEdit="false"></ion-input>
  <ion-icon
    *ngIf="getForm['email'].invalid && (getForm['email'].dirty || getForm['email'].touched) && (getForm['email'].errors?.['required'] || getForm['email'].errors?.['pattern'])"
    class="center-icon-item" slot="end" name="alert-circle" color="danger"></ion-icon>
  <ion-note
    *ngIf="getForm['email'].invalid && (getForm['email'].dirty || getForm['email'].touched) && getForm['email'].errors?.['required']"
    slot="error">email error 1</ion-note>
  <ion-note
    *ngIf="getForm['email'].invalid && (getForm['email'].dirty || getForm['email'].touched) && getForm['email'].errors?.['pattern']"
    slot="error">email error 2</ion-note>
  ...
</ion-item>

Or to make it a little less repetitive:

<ion-item>
  <ion-label position="floating">Email</ion-label>
  <ion-input formControlName="email" type="email" clearOnEdit="false"></ion-input>
  <ng-container *ngIf="getForm['email'].invalid && (getForm['email'].dirty || getForm['email'].touched)">
    <ion-icon *ngIf="getForm['email'].errors?.['required'] || getForm['email'].errors?.['pattern']"
      class="center-icon-item" slot="end" name="alert-circle" color="danger"></ion-icon>
    <ion-note *ngIf="getForm['email'].errors?.['required']" slot="error">email error 1</ion-note>
    <ion-note *ngIf="getForm['email'].errors?.['pattern']" slot="error">email error 2</ion-note>
    ...
  </ng-container>
</ion-item>

Greetings

from ionic-framework.

Marius-Romanus avatar Marius-Romanus commented on April 28, 2024

I add a little context, it would be very similar to the Angular documentation with error handling:

https://angular.io/guide/form-validation#validating-input-in-template-driven-forms

from ionic-framework.

liamdebeasi avatar liamdebeasi commented on April 28, 2024

Does your application architecture support the ability to extract the logic to a helper function? The benefit here is it would let you achieve the desired result and also have a template that is a bit easier to parse. Some rough pseudo code:

<ion-input [errorText]="getErrorText()"></ion-input>
getErrorText() {
  if (email.invalid && (email.dirty || email.touched) && email.errors?.required) {
    return 'email error 1'
  }
  
  ...
}

from ionic-framework.

Marius-Romanus avatar Marius-Romanus commented on April 28, 2024

Hello, yes, that logic can be implemented, but does it seem cleaner to us in the html instead of in ts?
At least to me personally it seemed cleaner to have the error messages in the html.

It is also assumed that with zone.js it is better not to have a function in a template that affects performance, but well that would be minimal and I understand that the get form also does the same in terms of performance.
But in cases where the if comes from a variable and not a function, it would improve performance.

from ionic-framework.

liamdebeasi avatar liamdebeasi commented on April 28, 2024

Have you tried listening for status changes on the form control? In this callback you can check the validity state. From there, you can assign an error message to a variable. This variable you can then render in the template. That should avoid any performance issues with rendering a function in a template.

from ionic-framework.

Marius-Romanus avatar Marius-Romanus commented on April 28, 2024

Hello, I'm sorry I couldn't answer sooner. Yes, it would be another valid option. Personally, I find it more readable and concise as it was done in the past, in HTML. Please consider that option, if you do not feel free to close the issue. :)

from ionic-framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.