Nebular Auth Angular Validation

蹲街弑〆低调 提交于 2020-06-16 17:56:47

问题


I use Nebular to create login form. But the form of Nebular using email validation. I want to disable email validation because my website using username.

enter image description here


回答1:


I don't believe there is an easy configuration option to change the user properties of the login form. If you look at the source of the login form you can see the user is just an object of type any. This mean you can extend the NbLoginComponent and change the [(ngModel)] binding for the email field to username. (see below for a working stackblitz).

You'll have to create your own login component extending NbLoginComponent. For the custom login component you don't need anything in your ts file, you are only providing a new template and inheriting all the functionality of NbLoginComponent. In the template you can change the model binding for the Email input and remove the email validation (pattern=".+@.+\..+") from the input.

username-login.component.ts

import { Component } from '@angular/core';
import { NbLoginComponent } from '@nebular/auth';

@Component({
  selector: 'username-login',
  templateUrl: './login.component.html',
})
export class UsernameLoginComponent extends NbLoginComponent {
}

username-login.component.html

... omitted
<form (ngSubmit)="login()" #form="ngForm" aria-labelledby="title">

  <div class="form-control-group">
    <label class="label" for="input-email">Email address:</label>
    <input nbInput
           fullWidth
           [(ngModel)]="user.username"
           #username="ngModel"
           name="username"
           id="input-username"
           placeholder="Username"
           fieldSize="large"
           autofocus
           [status]="username.dirty ? (username.invalid  ? 'danger' : 'success') : 'basic'"
           [required]="getConfigValue('forms.validation.username.required')"
           [attr.aria-invalid]="username.invalid && username.touched ? true : null">
    <ng-container *ngIf="username.invalid && username.touched">
      <p class="caption status-danger" *ngIf="username.errors?.required">
        Username is required!
      </p>
    </ng-container>
  </div>
  ...
</form>

Then in your routes, just route to your custom login component

app-routing.module.ts

const routes: Routes = [
  {
    path: '',
    redirectTo: 'auth',
    pathMatch: 'full',
  },
  {
    path: 'auth',
    component: NbAuthComponent,
    children: [
      {
        path: '',
        component: UsernameLoginComponent,
      }
    ],
  },
];

In the stackblitz below I have a working example. After Log In button is pressed, I have an alert showing the object that is sent in the post request. You can also check the request in the Network tab of the dev tools to see the request body.

STACKBLITZ: https://stackblitz.com/edit/nebular-dynamic-auth-api-laoksx

https://github.com/akveo/nebular/tree/master/src/framework/auth/components

https://akveo.github.io/nebular/docs/auth/custom-auth-components#create-auth-module



来源:https://stackoverflow.com/questions/59834217/nebular-auth-angular-validation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!