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

Please provide option to validate form elements like <ion-input>, <ion-select>, <ion-textarea> #6040

Closed
TheMohanraj opened this issue Apr 5, 2016 · 19 comments
Assignees

Comments

@TheMohanraj
Copy link

ionic v2

@Ionitron Ionitron added the v2 label Apr 5, 2016
@jgw96
Copy link
Contributor

jgw96 commented Apr 5, 2016

Hello, thanks for opening an issue with us! You can already do form validation in Ionic 2 using the angular 2 forms/formbuilder module. Thats the great thing about Ionic 2, it includes all the awesomeness from Angular 2! Here is a link to the Forms docs on the Angular 2 site: https://angular.io/docs/ts/latest/guide/forms.html Thanks again!

@jgw96 jgw96 closed this as completed Apr 5, 2016
@TheMohanraj
Copy link
Author

TheMohanraj commented Apr 6, 2016

I actually talking about <ion-input>, <ion-select>, <ion-textarea>

@TheMohanraj
Copy link
Author

TheMohanraj commented Apr 18, 2016

@adamdbradley @brandyscarney Is there a way to validate <ion-input>, <ion-select>, <ion-textarea>

@TheMohanraj TheMohanraj changed the title Please provide option to validate form elements like input, select, textarea Please provide option to validate form elements like <ion-input>, <ion-select>, <ion-textarea> Apr 18, 2016
@brandyscarney brandyscarney reopened this Apr 18, 2016
@tlaverdure
Copy link
Contributor

Wanted to point out an issue I've come across using the angular2 formbuilder with an ion-input using the required attribute. If you focus in and then focus out I would expect the input to have the ng-invalid class added, but it doesn't.

To summarize:

  • The ion-item class wrapping the ion-input has a the class ng-valid
  • The ion-input has the class ng-invalid
  • The actual input field has the class ng-valid

See also:
https://forum.ionicframework.com/t/ion-input-and-ion-item-validation-classes-dont-match/48895

@xr0master
Copy link
Contributor

xr0master commented May 11, 2016

@tlaverdure +1
@brandyscarney Validation works but it has some issues )

First:
.item-inner has padding-right 8px, but ion-input::after hasn't
image
btw why do you use position: static instead of position: relative?
something like;

.item-input ion-input, .item-input ion-textarea {
    position: relative;
}
ion-input::after {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    border-bottom-width: 2px;
    border-bottom-style: solid;
    border-bottom-color: transparent;
    content: "";
}

Second:
ion-select doesn't show an any notification. And it is always valid for NgForm. So I can submit invalid form. I think, it happens, because ngControl can't understand the not input element...

Update:
About second. I was confuse. The ngControl directive works without problem, and if I use [disabled]="!myForm.form.valid" on the submit button, it works like need.

@brandyscarney brandyscarney added this to the 2.0.0-beta.8 milestone May 11, 2016
@brandyscarney brandyscarney self-assigned this May 11, 2016
@WHarris22
Copy link

What I have noticed is that <ion-item> gets updated with other classes such as ng-dirty and ng-touched but it always stays as ng-valid even when <ion-input> has ng-invalid assigned. Here is an image to show you what I mean...

ionic2-form-validation

For me it would be useful if <ion-item> got updated with ng-invalid because I want to be able to change the text color of <ion-label> accordingly. At the minute I don't see a way of targeting it with CSS selectors.

brandyscarney added a commit that referenced this issue May 21, 2016
this is to make the native input, ion-input and item all have the same
classes for the control

references #6040
@brandyscarney
Copy link
Member

So there is a lot going on on this issue. I'm going to try to address everything but let me know if there is something I miss.

@Dheran: I'm not sure what you are looking for in terms of validation. Could you please explain what you mean? The form validation Angular 2 offers should be what you need but if you could explain more of what you want, I can help more. 😄

@tlaverdure: Thanks for bringing this to our attention. I have fixed this in the repo, and published a nightly version of the framework where it can be tested. Could you please take a look and let me know if you find any issues? To install the nightly version:

npm install --save [email protected]

If you haven't updated to beta.7 yet please follow the guide in the changelog to update: https://github.com/driftyco/ionic/blob/2.0/CHANGELOG.md#angular-update-to-200-rc1

@xr0master: Thanks for letting me know about the UI issues. I've updated the UI to extend the length of the item and added Sass variables for turning off success/error highlighting:

$text-input-md-show-success-highlight:      true !default;
$text-input-md-show-error-highlight:        true !default;

There is another issue for this (#5052), could you please put any feedback you have for improving success/error highlighting on that issue?

@WHarris22 I believe your issue is the same as @tlaverdure mentioned. Please read my above response to him.

Here's a gif of my most recent changes:

Source code for this is here:
https://github.com/driftyco/ionic/tree/7c0fe93061fc17d1e023698983b7b58d9b82f0a9/src/components/input/test/form-inputs

Thanks everyone! 😄

@xr0master
Copy link
Contributor

@brandyscarney Very cool! Can you implement it for ion-select too, please? And can you append ability to change the line color? It'll be amazing.

BTW iOS hasn't this validation syntax (bottom line); It will be fixed? Or iOS hasn't validation style?

About the another issue (#5052). I think no special about it, any container with ngSwitch directive. Something like a ngMessages directive of md, IMO. You need be carefully with a long text and an animation (changing the error message to another). It'll be cool, if we could disable the animation and change the error text color too.

PS Thanks you for your amazing work

@brandyscarney
Copy link
Member

Yes, I'll be working on adding it to the select next. 😄

I'm not sure about adding validation style to iOS, it's something that needs to be discussed.

I agree with the ngMessages directive. There has actually been an issue created with that in mind here: #6785

@TheMohanraj
Copy link
Author

TheMohanraj commented Jun 13, 2016

@brandyscarney after updating to beta 8, ion-textarea and ion-input adding border bottom automatically. Is there a way to prevent this?

@brandyscarney
Copy link
Member

@Dheran

The highlighting can be turned off via the following Sass variables:

$text-input-md-show-success-highlight:      false;
$text-input-md-show-error-highlight:        false;

and to change the colors:

// Input highlight - normal
$text-input-md-highlight-color:             color($colors-md, primary);

// Input highlight - valid
$text-input-md-hightlight-color-valid:      color($colors-md, secondary);

// Input highlight - invalid
$text-input-md-hightlight-color-invalid:    color($colors-md, danger);

The issue is a WIP so please add any feedback on #5052. Thanks!

@WHarris22
Copy link

As I discussed in this closed issue I think it would be useful to be able to validate inputs inside the alert prompt component.

Not sure how you could go about this, but one idea is to have more options under the input option to provide validators and error messages?

e.g.

inputs: [
      {
        name: 'username',
        placeholder: 'Username'
      },
      {
        name: 'password',
        placeholder: 'Password',
        type: 'password',
        validation: [ { validator: 'type of validation', message: 'error message' } ]
      }
    ]

@AntoineGermanique
Copy link

AntoineGermanique commented Oct 13, 2016

Hi,
Is required supposed to work on ion-select now ? For me it doesn't .
Thanks for your help!

@BradyNadeau
Copy link

BradyNadeau commented Mar 23, 2017

Is there any update on making the classes on ion-item for ion-select, etc?
I can do a pull request, but i'm guessing that there needs to be a base class for these controls...
most likely the base class for ion-input: IonicFormInput

And will need to implement some common functionality among all of them... Like the setControlCss function

function setControlCss(element: any, control: NgControl) 
{
  element.setElementClass('ng-untouched', control.untouched);
  element.setElementClass('ng-touched', control.touched);
  element.setElementClass('ng-pristine', control.pristine);
  element.setElementClass('ng-dirty', control.dirty);
  element.setElementClass('ng-valid', control.valid);
  element.setElementClass('ng-invalid', !control.valid);
}

As well as injecting and implementing NgControl into every input control component.

If there is a set way Ionic wants to implement it, I don't want to waste my time doing something that doesn't follow current architecture. So if there is.. any guidance would definitely be appreciated! (:

@abner
Copy link

abner commented Mar 24, 2017

@BradyLiles Have you already started to work on this issue? I want to join forces with you.

@tommck
Copy link

tommck commented Apr 27, 2017

ion-select still doesn't work with required attribute

@brianlittmann
Copy link

A bit of a hack, but a quick workaround for me for anything other than ion-input was to conditionally add classes based on the FormGroup control's valid state:

<ion-item [class.ng-invalid]="!profileForm.controls.state.valid">
    <ion-label stacked>State</ion-label>
    <ion-select formControlName="state">
        ...
    </ion-select>
</ion-item>

@cyptus
Copy link

cyptus commented Jul 13, 2017

ionic is automatically validating the input fields, but it misses to update the parent ion-item if a control is marked as touched (markAsTouched() Method on control) from code. I've a next button which should mark all untouched and invalid controls as invalid (red border). i have to update the parent ion-item style manually like this:

html:

<form [formGroup]="personForm">
  <ion-list>
    <ion-item>
      <ion-label color="primary">Name</ion-label>
      <ion-input type="text" placeholder="Name" [(ngModel)]="username" formControlName="username"></ion-input>
    </ion-item>
  </ion-list>
</form>

code:

  next()
  {
    for (var controlName in this.personForm.controls) {
      let ele = document.querySelector('[formControlName="' + controlName + '"]');
      this.personForm.controls[controlName].markAsTouched();

      if(ele) {
        this.setControlCss(ele.parentElement.parentElement.parentElement, this.personForm.controls[controlName]);
      }
    }
  }

  setControlCss(element: any, control: AbstractControl) 
  {
    this.renderer.setElementClass(element, 'ng-untouched', control.untouched);
    this.renderer.setElementClass(element, 'ng-touched', control.touched);
    this.renderer.setElementClass(element, 'ng-pristine', control.pristine);
    this.renderer.setElementClass(element, 'ng-dirty', control.dirty);
    this.renderer.setElementClass(element, 'ng-valid', control.valid);
    this.renderer.setElementClass(element, 'ng-invalid', !control.valid);
  }

see also: https://forum.ionicframework.com/t/ionic-form-validate-from-code-remove-ng-untouched/98010/2?u=cyptus

EDIT:
i've put this into a own feature request (validation border on ion-select) and an bug report (no border on update on ion-item parent):
#12344
#12345

@ionitron-bot
Copy link

ionitron-bot bot commented Jul 13, 2018

Thanks for the issue! This issue is being closed due to inactivity. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Thank you for using Ionic!

@ionitron-bot ionitron-bot bot closed this as completed Jul 13, 2018
@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Jul 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests