完善表单验证

pull/21/merge
ouqiang 8 years ago
parent dfa96bc9f2
commit 29e4daaf93

@ -12,6 +12,7 @@ import (
"fmt"
"html/template"
"github.com/ouqiang/gocron/routers/base"
"github.com/go-macaron/binding"
)
func Index(ctx *macaron.Context) {
@ -96,7 +97,7 @@ func Ping(ctx *macaron.Context) string {
type HostForm struct {
Id int16
Name string `binding:"Required;MaxSize(100)"`
Name string `binding:"Required;MaxSize(64)"`
Alias string `binding:"Required;MaxSize(32)"`
Username string `binding:"Required;MaxSize(32)"`
Port int `binding:"Required;Range(1-65535)"`
@ -104,6 +105,16 @@ type HostForm struct {
Remark string
}
func (f HostForm) Error(ctx *macaron.Context, errs binding.Errors) {
if len(errs) == 0 {
return
}
json := utils.JsonResponse{}
content := json.CommonFailure("表单验证失败, 请检测输入")
ctx.Resp.Write([]byte(content))
}
func Store(ctx *macaron.Context, form HostForm) string {
json := utils.JsonResponse{}
hostModel := new(models.Host)

@ -9,6 +9,7 @@ import (
"strconv"
"fmt"
"github.com/ouqiang/gocron/service"
"github.com/go-macaron/binding"
)
// 系统安装
@ -27,6 +28,16 @@ type InstallForm struct {
AdminEmail string `binding:"Required;Email;MaxSize(50)"`
}
func (f InstallForm) Error(ctx *macaron.Context, errs binding.Errors) {
if len(errs) == 0 {
return
}
json := utils.JsonResponse{}
content := json.CommonFailure("表单验证失败, 请检测输入")
ctx.Resp.Write([]byte(content))
}
func Create(ctx *macaron.Context) {
if app.Installed {
ctx.Redirect("/")

@ -12,14 +12,15 @@ import (
"fmt"
"html/template"
"github.com/ouqiang/gocron/routers/base"
"github.com/go-macaron/binding"
)
type TaskForm struct {
Id int
Name string `binding:"Required;"`
Name string `binding:"Required;MaxSize(32)"`
Spec string `binding:"Required;MaxSize(64)"`
Protocol models.TaskProtocol `binding:"In(1,2,3)"`
Command string `binding:"Required;MaxSize(512)"`
Command string `binding:"Required;MaxSize(256)"`
Timeout int `binding:"Range(-1,86400)"`
Multi int8 `binding:"In(1,2)"`
RetryTimes int8
@ -31,6 +32,17 @@ type TaskForm struct {
NotifyReceiverId string
}
func (f TaskForm) Error(ctx *macaron.Context, errs binding.Errors) {
if len(errs) == 0 {
return
}
json := utils.JsonResponse{}
content := json.CommonFailure("表单验证失败, 请检测输入")
ctx.Resp.Write([]byte(content))
}
// 首页
func Index(ctx *macaron.Context) {
taskModel := new(models.Task)
@ -141,6 +153,9 @@ func Store(ctx *macaron.Context, form TaskForm) string {
if taskModel.Protocol == models.TaskHTTP && taskModel.Timeout == -1 {
return json.CommonFailure("HTTP任务不支持后台运行", err)
}
if taskModel.RetryTimes > 10 || taskModel < 0 {
return json.CommonFailure("任务重试次数取值0-10")
}
if id == 0 {
id, err = taskModel.Create()
} else {

@ -91,6 +91,10 @@
{
type : 'empty',
prompt : ''
},
{
type : 'maxLength[64]',
prompt : '64'
}
]
},
@ -100,6 +104,10 @@
{
type : 'empty',
prompt : ''
},
{
type : 'maxLength[32]',
prompt : '32'
}
]
},
@ -109,6 +117,10 @@
{
type : 'empty',
prompt : 'SSH'
},
{
type : 'maxLength[32]',
prompt : '32'
}
]
},
@ -120,6 +132,15 @@
prompt : ''
}
]
},
remark: {
identifier : 'remark',
rules: [
{
type : 'maxLength[100]',
prompt : '100'
}
]
}
},
inline : true

@ -58,6 +58,7 @@
<h5 class="ui header">{{{.Task.Name}}} {{{if eq .Status 1}}}<i class="large checkmark blue icon"></i> {{{else}}} <i class="large red minus icon"></i> {{{end}}}
</h5>
<p>ID <span class="stress">{{{.Id}}}</span></p>
<p> <span class="stress">{{{if eq .Status 1}}}{{{else}}}{{{end}}}</span></p>
<p>cron {{{.Spec}}}</p>
<p>: {{{if eq .Protocol 1}}} HTTP {{{else if eq .Protocol 2}}} SSH {{{else if eq .Protocol 3}}}{{{end}}}</p>
<p class="sensorStatus">{{{.Command}}}</p>
@ -74,7 +75,7 @@
<div class="ui buttons operation">
<a class="ui purple button" href="/task/edit/{{{.Id}}}"></a>
{{{if eq .Status 1}}}
<button class="ui primary button" @click="changeStatus({{{.Id}}},{{{.Status}}})"></button>
<button class="ui primary button" @click="changeStatus({{{.Id}}},{{{.Status}}})"></button>
{{{else}}}
<button class="ui blue button" @click="changeStatus({{{.Id}}},{{{.Status}}})"> </button>
{{{end}}}

@ -29,7 +29,7 @@
</div>
<div class="field">
<select name="protocol" id="protocol">
<option value="0"></option>
<option value="0"></option>
<option value="3" {{{if eq .Params.Protocol 3}}}selected{{{end}}}></option>
<option value="2" {{{if eq .Params.Protocol 2}}}selected{{{end}}} data-match="host_id" data-validate-type="selectProtocol">SSH</option>
<option value="1" {{{if eq .Params.Protocol 1}}}selected{{{end}}}>HTTP</option>

@ -97,7 +97,7 @@
</div>
<select name="status">
<option value="2"{{{if .Task}}} {{{if eq .Task.Status 0}}}selected{{{end}}} {{{end}}}></option>
<option value="2"{{{if .Task}}} {{{if eq .Task.Status 0}}}selected{{{end}}} {{{end}}}></option>
<option value="1" {{{if .Task}}} {{{if eq .Task.Status 1}}}selected{{{end}}} {{{end}}}></option>
</select>
</div>
@ -295,6 +295,10 @@
{
type : 'empty',
prompt : ''
},
{
type : 'maxLength[32]',
prompt : '32'
}
]
},
@ -304,6 +308,10 @@
{
type : 'empty',
prompt : 'crontab'
},
{
type : 'maxLength[64]',
prompt : '64'
}
]
},
@ -313,6 +321,10 @@
{
type : 'empty',
prompt : ''
},
{
type : 'maxLength[256]',
prompt : '256'
}
]
},
@ -324,6 +336,33 @@
prompt : ''
}
]
},
timeout: {
identifier : 'timeout',
rules: [
{
type : 'integer[-1..86400]',
prompt : '-1 - 86400'
}
]
},
remark: {
identifier : 'remark',
rules: [
{
type : 'maxLength[100]',
prompt : '100'
}
]
},
retryTimes: {
identifier : 'retry_times',
rules: [
{
type : 'integer[0..10]',
prompt : ' 0 - 10'
}
]
}
},
inline : true

Loading…
Cancel
Save