This class provides a stub validator for fields that should contain a unique value along an application (typically logins or ids).
Here an example that uses a Repository
if an email is unique:
class UniqueEmailField
super UniqueField
var users: UsersRepository
redef fun check_unicity(v, field, val) do
var user = users.find_by_email(val)
if user != null then
v.validation.add_error(field, "Email `{val}` already used")
return false
end
return true
end
end
popcorn :: UniqueField :: check_unicity
Check ifval
is already used somewhere
popcorn :: UniqueField :: defaultinit
popcorn $ UniqueField :: SELF
Type of this instance, automatically specialized in every classpopcorn $ UniqueField :: validate_field
Validatefield
in obj
popcorn :: UniqueField :: check_unicity
Check ifval
is already used somewhere
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
popcorn :: StringField :: defaultinit
core :: Object :: defaultinit
popcorn :: RequiredField :: defaultinit
popcorn :: FieldValidator :: defaultinit
popcorn :: UniqueField :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
popcorn :: StringField :: max_size
String max size (default: not checked)popcorn :: StringField :: max_size=
String max size (default: not checked)popcorn :: StringField :: min_size
String min size (default: not checked)popcorn :: StringField :: min_size=
String min size (default: not checked)core :: Object :: output_class_name
Display class name on stdout (debug only).popcorn :: RequiredField :: required=
Is this field required?popcorn :: FieldValidator :: validate_field
Validatefield
in obj
# Check if a field value is already used
#
# This class provides a stub validator for fields that should contain a unique value along an
# application (typically logins or ids).
#
# Here an example that uses a `Repository` if an email is unique:
# ~~~nitish
# class UniqueEmailField
# super UniqueField
#
# var users: UsersRepository
#
# redef fun check_unicity(v, field, val) do
# var user = users.find_by_email(val)
# if user != null then
# v.validation.add_error(field, "Email `{val}` already used")
# return false
# end
# return true
# end
# end
# ~~~
class UniqueField
super StringField
# Check if `val` is already used somewhere
#
# You must redefine this method to handle your own validation.
fun check_unicity(v: ObjectValidator, field, val: String): Bool is abstract
redef fun validate_field(v, obj) do
if not super then return false
var val = obj.get_or_null(field)
if not val isa String then return false
if not check_unicity(v, field, val) then return false
return true
end
end
lib/popcorn/pop_validation.nit:773,1--810,3