产品链接添加校验规则
Showing
1 changed file
with
46 additions
and
20 deletions
... | @@ -111,13 +111,17 @@ | ... | @@ -111,13 +111,17 @@ |
111 | >预览</el-button | 111 | >预览</el-button |
112 | > | 112 | > |
113 | </div> | 113 | </div> |
114 | <el-input | 114 | <el-form ref="linkForm" size="mini" :model="linkForm" :rules="linkRules"> |
115 | class="mb10" | 115 | <el-form-item prop="link"> |
116 | type="textarea" | 116 | <el-input |
117 | :rows="5" | 117 | class="mb10" |
118 | style="width:500px" | 118 | type="textarea" |
119 | v-model="productLink" | 119 | :rows="5" |
120 | ></el-input> | 120 | style="width:500px" |
121 | v-model="linkForm.link" | ||
122 | ></el-input> | ||
123 | </el-form-item> | ||
124 | </el-form> | ||
121 | <div class="bottom"> | 125 | <div class="bottom"> |
122 | <el-button type="primary" @click="submit">提交</el-button> | 126 | <el-button type="primary" @click="submit">提交</el-button> |
123 | </div> | 127 | </div> |
... | @@ -139,6 +143,8 @@ import { | ... | @@ -139,6 +143,8 @@ import { |
139 | productAdd | 143 | productAdd |
140 | } from '@/api/productInforManagement' | 144 | } from '@/api/productInforManagement' |
141 | 145 | ||
146 | const urlReg = /[a-zA-z]+:\/\/[^\s]*/ | ||
147 | |||
142 | export default { | 148 | export default { |
143 | props: ['idAndCode'], | 149 | props: ['idAndCode'], |
144 | data() { | 150 | data() { |
... | @@ -152,6 +158,16 @@ export default { | ... | @@ -152,6 +158,16 @@ export default { |
152 | } | 158 | } |
153 | callback() | 159 | callback() |
154 | } | 160 | } |
161 | const validateLink = (rule, value, callback) => { | ||
162 | if (!value) { | ||
163 | return callback() | ||
164 | } | ||
165 | if (urlReg.test(value)) { | ||
166 | callback() | ||
167 | } else { | ||
168 | callback(new Error('请输入正确格式的产品链接')) | ||
169 | } | ||
170 | } | ||
155 | return { | 171 | return { |
156 | inputMaxLength, | 172 | inputMaxLength, |
157 | fileList: [ | 173 | fileList: [ |
... | @@ -193,7 +209,12 @@ export default { | ... | @@ -193,7 +209,12 @@ export default { |
193 | ] | 209 | ] |
194 | }, | 210 | }, |
195 | currentFileIndex: 0, | 211 | currentFileIndex: 0, |
196 | productLink: '' | 212 | linkForm: { |
213 | link: '' | ||
214 | }, | ||
215 | linkRules: { | ||
216 | link: [{ validator: validateLink, trigger: 'blur' }] | ||
217 | } | ||
197 | } | 218 | } |
198 | }, | 219 | }, |
199 | computed: { | 220 | computed: { |
... | @@ -304,19 +325,23 @@ export default { | ... | @@ -304,19 +325,23 @@ export default { |
304 | }) | 325 | }) |
305 | }, | 326 | }, |
306 | previewLink() { | 327 | previewLink() { |
328 | if (!this.linkForm.link || !urlReg.test(this.linkForm.link)) { | ||
329 | return | ||
330 | } | ||
331 | this.$refs.linkForm.validateField('link') | ||
307 | const link = document.createElement('a') | 332 | const link = document.createElement('a') |
308 | link.href = this.productLink | 333 | link.href = this.linkForm.link |
309 | link.target = '_blank' | 334 | link.target = '_blank' |
310 | link.click() | 335 | link.click() |
311 | }, | 336 | }, |
312 | submit() { | 337 | async submit() { |
313 | this.$refs.form.validate(valid => { | 338 | try { |
314 | if (valid) { | 339 | await this.$refs.form.validate() |
315 | this.editOrAdd() | 340 | await this.$refs.linkForm.validate() |
316 | } else { | 341 | this.editOrAdd() |
317 | return false | 342 | } catch (error) { |
318 | } | 343 | console.log(error) |
319 | }) | 344 | } |
320 | }, | 345 | }, |
321 | async editOrAdd() { | 346 | async editOrAdd() { |
322 | const formData = this.constructorFormData() | 347 | const formData = this.constructorFormData() |
... | @@ -328,6 +353,7 @@ export default { | ... | @@ -328,6 +353,7 @@ export default { |
328 | message: '修改成功', | 353 | message: '修改成功', |
329 | type: 'success' | 354 | type: 'success' |
330 | }) | 355 | }) |
356 | this.$router.push({ path: 'product-infor-management' }) | ||
331 | } | 357 | } |
332 | } else { | 358 | } else { |
333 | const { code } = await productAdd(formData) | 359 | const { code } = await productAdd(formData) |
... | @@ -336,9 +362,9 @@ export default { | ... | @@ -336,9 +362,9 @@ export default { |
336 | message: '添加成功', | 362 | message: '添加成功', |
337 | type: 'success' | 363 | type: 'success' |
338 | }) | 364 | }) |
365 | this.$router.push({ path: 'product-infor-management' }) | ||
339 | } | 366 | } |
340 | } | 367 | } |
341 | this.$router.push({ path: 'product-infor-management' }) | ||
342 | }, | 368 | }, |
343 | constructorFormData() { | 369 | constructorFormData() { |
344 | const formData = new FormData() | 370 | const formData = new FormData() |
... | @@ -356,7 +382,7 @@ export default { | ... | @@ -356,7 +382,7 @@ export default { |
356 | } | 382 | } |
357 | }) | 383 | }) |
358 | 384 | ||
359 | formData.append('link', this.productLink) | 385 | formData.append('link', this.linkForm.link) |
360 | 386 | ||
361 | return formData | 387 | return formData |
362 | }, | 388 | }, |
... | @@ -381,7 +407,7 @@ export default { | ... | @@ -381,7 +407,7 @@ export default { |
381 | file.url = item.url | 407 | file.url = item.url |
382 | }) | 408 | }) |
383 | 409 | ||
384 | this.productLink = link | 410 | this.linkForm.link = link |
385 | } | 411 | } |
386 | } | 412 | } |
387 | } | 413 | } | ... | ... |
-
Please register or sign in to post a comment