init
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"github.com/goravel/framework/contracts/validation"
|
||||
"github.com/goravel/framework/facades"
|
||||
)
|
||||
|
||||
/**
|
||||
* exists 验证一个值在某个表中的字段中存在,相较于Laravel,支持同时判断多个字段
|
||||
* exists verify a value exists in a table field, compared to Laravel, support judging multiple fields at the same time
|
||||
* 用法:exists:表名称,字段名称,字段名称,字段名称
|
||||
* Usage: exists:table_name,field_name,field_name,field_name
|
||||
* 例子:exists:users,phone,email
|
||||
* Example: exists:users,phone,email
|
||||
*/
|
||||
|
||||
type Exists struct {
|
||||
}
|
||||
|
||||
// Signature The name of the rule.
|
||||
func (receiver *Exists) Signature() string {
|
||||
return "exists"
|
||||
}
|
||||
|
||||
// Passes Determine if the validation rule passes.
|
||||
func (receiver *Exists) Passes(_ validation.Data, val any, options ...any) bool {
|
||||
|
||||
tableName := options[0].(string)
|
||||
fieldName := options[1].(string)
|
||||
requestValue := val.(string)
|
||||
|
||||
if len(requestValue) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
var count int64
|
||||
query := facades.Orm().Query().Table(tableName).Where(fieldName, requestValue)
|
||||
if len(options) > 2 {
|
||||
for i := 2; i < len(options); i++ {
|
||||
query = query.OrWhere(options[i].(string), requestValue)
|
||||
}
|
||||
}
|
||||
count, err := query.Count()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return count != 0
|
||||
}
|
||||
|
||||
// Message Get the validation error message.
|
||||
func (receiver *Exists) Message() string {
|
||||
return "record does not exist"
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"github.com/goravel/framework/contracts/validation"
|
||||
"github.com/goravel/framework/facades"
|
||||
)
|
||||
|
||||
/**
|
||||
* not_exists 验证一个值在某个表中的字段中不存在,相较于Laravel,支持同时判断多个字段
|
||||
* not_exists verify a value does not exist in a table field, compared to Laravel, support judging multiple fields at the same time
|
||||
* 用法:not_exists:表名称,字段名称,字段名称,字段名称
|
||||
* Usage: not_exists:table_name,field_name,field_name,field_name
|
||||
* 例子:not_exists:users,phone,email
|
||||
* Example: not_exists:users,phone,email
|
||||
*/
|
||||
|
||||
type NotExists struct {
|
||||
}
|
||||
|
||||
// Signature The name of the rule.
|
||||
func (receiver *NotExists) Signature() string {
|
||||
return "not_exists"
|
||||
}
|
||||
|
||||
// Passes Determine if the validation rule passes.
|
||||
func (receiver *NotExists) Passes(_ validation.Data, val any, options ...any) bool {
|
||||
|
||||
tableName := options[0].(string)
|
||||
fieldName := options[1].(string)
|
||||
requestValue := val.(string)
|
||||
|
||||
if len(requestValue) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
var count int64
|
||||
query := facades.Orm().Query().Table(tableName).Where(fieldName, requestValue)
|
||||
if len(options) > 2 {
|
||||
for i := 2; i < len(options); i++ {
|
||||
query = query.OrWhere(options[i].(string), requestValue)
|
||||
}
|
||||
}
|
||||
count, err := query.Count()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return count == 0
|
||||
}
|
||||
|
||||
// Message Get the validation error message.
|
||||
func (receiver *NotExists) Message() string {
|
||||
return "record already exists"
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"github.com/goravel/framework/contracts/validation"
|
||||
)
|
||||
|
||||
/**
|
||||
* same 验证一个字段的值必须与另一个字段的值相同
|
||||
* same verify a field value must be the same as another field value
|
||||
* 用法:same:字段名称
|
||||
* Usage: same:field_name
|
||||
* 例子:same:new_password
|
||||
* Example: same:new_password
|
||||
*/
|
||||
|
||||
type Same struct {
|
||||
}
|
||||
|
||||
// Signature The name of the rule.
|
||||
func (receiver *Same) Signature() string {
|
||||
return "same"
|
||||
}
|
||||
|
||||
// Passes Determine if the validation rule passes.
|
||||
func (receiver *Same) Passes(data validation.Data, val any, options ...any) bool {
|
||||
if len(options) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
fieldName := options[0].(string)
|
||||
compareValue, exist := data.Get(fieldName)
|
||||
if !exist {
|
||||
return false
|
||||
}
|
||||
|
||||
// 转换为字符串进行比较
|
||||
valStr := ""
|
||||
compareStr := ""
|
||||
|
||||
if strVal, ok := val.(string); ok {
|
||||
valStr = strVal
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
if strCompareVal, ok := compareValue.(string); ok {
|
||||
compareStr = strCompareVal
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
return valStr == compareStr
|
||||
}
|
||||
|
||||
// Message Get the validation error message.
|
||||
func (receiver *Same) Message() string {
|
||||
return "The :attribute must match :other."
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user