Skip to content

Commit 762d057

Browse files
committed
feat: add example 2 component with form validation and styling
1 parent b23b162 commit 762d057

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
```html
2+
<form>
3+
<div>
4+
<input type="text" placeholder="Enter name" [control]="userForm.name" class="w-full p-2 border border-gray-300 rounded-md text-base mb-4" />
5+
@if(userForm.name().touched() || userForm.name().dirty()){
6+
@for (item of userForm.name().errors(); track item) {
7+
<p class="text-red-500 p-0">{{ item.message }}</p>
8+
}
9+
}
10+
</div>
11+
<div>
12+
<input type="email" placeholder="Enter email" [control]="userForm.email" class="w-full p-2 border border-gray-300 rounded-md text-base mb-4" />
13+
@if(userForm.email().touched() || userForm.email().dirty()){
14+
@for (item of userForm.email().errors(); track $index) {
15+
<p class="text-red-500 p-0">{{ item.message }}</p>
16+
}
17+
}
18+
</div>
19+
</form>
20+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
```scss
2+
:host {
3+
display: contents;
4+
.demo-section {
5+
margin-top: 2rem;
6+
display: flex;
7+
flex-direction: column;
8+
gap: 2rem;
9+
align-items: center;
10+
width: 100%;
11+
margin-left: auto;
12+
margin-right: auto;
13+
padding-bottom: 2rem;
14+
}
15+
.demo-section mat-card {
16+
width: 100%;
17+
max-width: 500px;
18+
box-shadow: 0 2px 8px rgba(0,0,0,0.07);
19+
border-radius: 8px;
20+
}
21+
}
22+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
```ts
2+
import { Component, effect, signal } from '@angular/core';
3+
import { form, Control, required, pattern, email, schema } from '@angular/forms/signals';
4+
5+
const formSchema = schema<{name: string, email: string}>((form) => {
6+
required(form.name, { message: 'please enter your name' });
7+
pattern(form.name, /^[a-z ]+$/i, { message: 'Enter valid name' });
8+
required(form.email, { message: 'please enter your email' });
9+
email(form.email, { message: 'Enter valid email' });
10+
});
11+
12+
@Component({
13+
selector: 'app-form-example2',
14+
templateUrl: './example2.component.html',
15+
styleUrl: './example2.component.scss'
16+
})
17+
export class FormExample2Component {
18+
public user = signal({ name: '', email: '' });
19+
public errors = effect(() => {
20+
console.log('errors - ', this.userForm().errors())
21+
return this.userForm().errors();
22+
});
23+
public userForm = form(this.user, formSchema);
24+
}

0 commit comments

Comments
 (0)