Skip to content

Commit 74daf35

Browse files
author
Kirill Serebrennikov
committed
add validate
1 parent af5bb65 commit 74daf35

File tree

8 files changed

+106
-14
lines changed

8 files changed

+106
-14
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"register-service-worker": "^1.7.1",
1414
"vue": "^2.6.11",
1515
"vue-router": "^3.2.0",
16+
"vuelidate": "^0.7.5",
1617
"vuex": "^3.4.0"
1718
},
1819
"devDependencies": {

src/App.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<template>
22
<div id="app">
33
<component :is="layout">
4-
<router-view />
54
</component>
65

76
</div>

src/components/app/Navbar.vue

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
<a href="#" @click.prevent="$emit('click')">
66
<i class="material-icons black-text">dehaze</i>
77
</a>
8-
<span class="black-text">12.12.12</span>
8+
<span class="black-text">{{date | date('datetime')}}</span>
99
</div>
1010

1111
<ul class="right hide-on-small-and-down">
1212
<li>
13-
<a class="dropdown-trigger black-text" href="#" data-target="dropdown">
13+
<a class="dropdown-trigger black-text" href="#" data-target="dropdown" ref="dropdown">
1414
USER NAME
1515
<i class="material-icons right">arrow_drop_down</i>
1616
</a>
1717

1818
<ul id="dropdown" class="dropdown-content">
1919
<li>
20-
<a href="#" class="black-text">
20+
<router-link to="/profile" class="black-text">
2121
<i class="material-icons">account_circle</i>Профиль
22-
</a>
22+
</router-link>
2323
</li>
2424
<li class="divider" tabindex="-1"></li>
2525
<li>
26-
<a href="#" class="black-text">
26+
<a href="#" class="black-text" @click.prevent="logout">
2727
<i class="material-icons">assignment_return</i>Выйти
2828
</a>
2929
</li>
@@ -33,3 +33,34 @@
3333
</div>
3434
</nav>
3535
</template>
36+
37+
<script>
38+
import M from 'materialize-css'
39+
export default {
40+
data: () => ({
41+
date: new Date(),
42+
interval: null,
43+
dropdown: null
44+
}),
45+
methods: {
46+
logout() {
47+
console.log('logout')
48+
this.$router.push('/login?message=logout')
49+
}
50+
},
51+
mounted() {
52+
this.interval = setInterval(() => {
53+
this.date = new Date()
54+
}, 1000)
55+
this.dropdown = M.Dropdown.init(this.$refs.dropdown, {
56+
constrainWidth: false
57+
})
58+
},
59+
beforeDestroy() {
60+
clearInterval(this.interval)
61+
if(this.dropdown && this.dropdown.destroy) {
62+
this.dropdown.destroy()
63+
}
64+
}
65+
}
66+
</script>

src/filters/date.filter.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default function dateFilter(value, format = 'date') {
2+
const options = {}
3+
4+
if(format.includes('date')) {
5+
options.day = '2-digit'
6+
options.month = 'long'
7+
options.year = 'numeric'
8+
}
9+
10+
if(format.includes('time')) {
11+
options.hour = '2-digit'
12+
options.minute = '2-digit'
13+
options.second = '2-digit'
14+
}
15+
16+
return new Intl.DateTimeFormat('ru-RU', options).format(new Date(value))
17+
}

src/layout/MainLayout.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
</main>
1111

1212
<div class="fixed-action-btn">
13-
<a class="btn-floating btn-large blue" href="#">
13+
<router-link class="btn-floating btn-large blue" to="/record">
1414
<i class="large material-icons">add</i>
15-
</a>
15+
</router-link>
1616
</div>
1717
</div>
1818
</template>

src/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import Vue from "vue";
2+
import Vuelidate from 'vuelidate'
23
import App from "./App.vue";
34
import "./registerServiceWorker";
45
import router from "./router";
56
import store from "./store";
7+
import dateFilter from '@/filters/date.filter'
68
import 'materialize-css/dist/js/materialize.min'
79

810
Vue.config.productionTip = false;
911

12+
Vue.use(Vuelidate)
13+
Vue.filter('date', dateFilter)
14+
1015
new Vue({
1116
router,
1217
store,

src/views/Login.vue

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<template>
2-
<form class="card auth-card">
2+
<form class="card auth-card" @submit.prevent="submitHandler">
33
<div class="card-content">
44
<span class="card-title">Домашняя бухгалтерия</span>
55
<div class="input-field">
6-
<input id="email" type="text" class="validate" />
6+
<input id="email" type="text" v-model.trim="email" :class="{invalid: ($v.email.$dirty && !$v.email.required) || ($v.email.$dirty && !$v.email.email)}"/>
77
<label for="email">Email</label>
8-
<small class="helper-text invalid">Email</small>
8+
<small class="helper-text invalid" v-if="$v.email.$dirty && !$v.email.required">Поле Email не должно быть пустым</small>
9+
<small class="helper-text invalid" v-else-if="$v.email.$dirty && !$v.email.email">Введите корректный Email</small>
910
</div>
1011
<div class="input-field">
11-
<input id="password" type="password" class="validate" />
12+
<input id="password" type="password" v-model.trim="password" :class="{invalid: ($v.password.$dirty && !$v.password.required) || ($v.password.$dirty && !$v.password.minLength)}" />
1213
<label for="password">Пароль</label>
13-
<small class="helper-text invalid">Password</small>
14+
<small class="helper-text invalid" v-if="$v.password.$dirty && !$v.password.required">Введите пароль</small>
15+
<small class="helper-text invalid" v-if="$v.password.$dirty && !$v.password.minLength">Пароль должен быть {{$v.password.$params.minLength.min}} символов. Сейчас он {{password.length}}</small>
1416
</div>
1517
</div>
1618
<div class="card-action">
@@ -23,8 +25,40 @@
2325

2426
<p class="center">
2527
Нет аккаунта?
26-
<a href="/">Зарегистрироваться</a>
28+
<router-link to="/register">Зарегистрироваться</router-link>
2729
</p>
2830
</div>
2931
</form>
3032
</template>
33+
34+
<script>
35+
import {email, required, minLength} from 'vuelidate/lib/validators'
36+
37+
export default {
38+
name: 'login',
39+
data: () => ({
40+
email: '',
41+
password: ''
42+
}),
43+
validations: {
44+
email: {email, required},
45+
password: {required, minLength: minLength(8)}
46+
},
47+
methods: {
48+
submitHandler() {
49+
if(this.$v.$invalid) {
50+
this.$v.$touch()
51+
return
52+
}
53+
const formData = {
54+
email: this.email,
55+
password: this.password
56+
}
57+
58+
console.log(formData)
59+
60+
this.$router.push('/')
61+
}
62+
}
63+
}
64+
</script>

0 commit comments

Comments
 (0)