2

I have popup component in which I want to insert form validation on input element for name (required field and minlength 3 characters). Is is possible to do it without setting up complete code in form tag?

<div class="scenario-div">
<label style="font-size: 1.4rem; font-weight: 400; margin-bottom: 10px;">{{context.header}}</label>
<div class="" style="width: 90%; margin-bottom: 10px; align-self: center;">
    <div style="display: flex; flex-direction: column; margin-bottom: 10px;">
        <label style="font-size: 1.2rem; font-weight: 500;">Name</label>
        <div style="display:flex; flex-direction:row;">
            <input type="text" [(ngModel)]="context.name" placeholder="enter Name" style="color: #569bd2; margin: 0px; width: 50%" minlength="3" required/>

            <select *ngIf="context.showOptionDropdown" class="custom-select-project" style="width:50%" #optionSelect (change)="context.option.value">
                <option disabled>Select type</option>
                <option value="option1">option1</option>
                <option value="option2">option2</option>
                <option value="option3">option3</option>
                <option value="option4">option4</option>
            </select>
        </div>
    </div>

    <div style="display: flex; flex-direction: column; margin-bottom: 10px;" *ngIf="!context.hideDescription">
        <label style="font-size: 1.2rem; font-weight: 400;">Description</label>
        <textarea rows="4" [(ngModel)]="context.description" cols="50" maxlength="250" placeholder="Enter text here" style="color: #569bd2; resize: none; font-weight: 400; font-size: 1.2rem;"></textarea>
    </div>
</div>
<div>   
    <button class="btn-main" (click)="confirm(context)" style="width: 15%;">Confirm</button>
</div>

0

2 Answers 2

8

'Validation' based on ngModel

    <input type="text" required name="title" [(ngModel)]="titleModel">
    <span style="color:red" *ngIf="titleModel?.length > 10">
          10 max
    </span>
    <span style="color:red" *ngIf="!titleModel">
          required
    </span>
    <button [disabled]="titleModel?.length > 10 || !titleModel">OK</button>

DEMO


'Validation' based on local template variable:

<input type="text" (keyup)='0' #title>
<span style="color:red" *ngIf="title.value.length > 10">
      10 max
</span>
<span style="color:red" *ngIf="!title.value">
      required
</span>

<button [disabled]="title.value.length > 10 || !title.value">OK</button>

DEMO

Sign up to request clarification or add additional context in comments.

6 Comments

Ok, I got it and creating spans for value.length works fine, but I still am able to do submitt ... I need to lock submit if my length is less then 3 characters.
I don't see any form tag in your snippet
But there is none ...That is the question ...How can I do form validation, but without form tag. However, thaks to you, i can manage to hide submit button until user enters more than 3 characters :)
If there is no form how do you have submit? You mean an action on a plain button? Then, you hide the button based on the condition or check in the class for the model/value length with if-else
Sorry ...my bad ...had some editing on question...will fix it now
|
4

You need to use FormControl from the reactive forms API, which can be used with a standalone <input> that's not wrapped into a <form> tag.

Component({
   ...
   template: `<input type="text" [formControl]="name">` 
})
class MyComponent {
  name = new FormControl('',
           [Validators.required, Validators.minLength(3)]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.