-
Notifications
You must be signed in to change notification settings - Fork 27
Validations
The following methods trigger validations, and will save the object to the database only if the object is valid:
create
save
validate
saveEither
The following methods skip validations, and will save the object to the database regardless of its validity. They should be used with caution.
save(validate = false)
After validations, any errors found can be accessed through the errors
instance method, which returns a collection of errors.
By definition, an object is valid if this collection is empty after running validations.
To verify whether or not a particular attribute of an object is valid, you can use errors#apply(attribute)
or errors#get(attribute)
.
case class User(@Required name: String) extends ActiveRecord
object User extends ActiveRecordCompanion[User]
val user = User("").create
println(user.isValid) // => false
println(user.hasErrors) // => true
println(user.errors.messges) // => Seq("Name is required")
println(user.hasError("name")) // => true
If you want to be the functional error handling, you can use saveEither
.
case class User(@Required name: String) extends ActiveRecord
object User extends ActiveRecordCompanion[User]
User("john").saveEither match {
case Right(user) => println(user.name)
case Left(errors) => println(errors.messages)
}
// => "john"
User("").saveEither match {
case Right(user) => println(user.name)
case Left(errors) => println(errors.messages)
}
// => "Name is required"
Annotation | Options | Validation |
---|---|---|
@Required | Check for null or empty or white space only string. | |
@Length | min = 0, max = Integer.MAX_NUM | Check for string length between min to max . |
@Range | min = Double.NEGATIVE_INFINITY, max = Double.POSITIVE_INFINITY | Check for numeric value between min to max . |
@Accepted | Check for value is true . |
|
Check for string is e-mail formatted. | ||
@Unique | Check for the unique field value in DB. | |
@NumberEnum | value | Check if a numeric array value contains field value. |
@StringEnum | value | Check if a string array value contains field value. |
@Format | value | Check if a regular expression value matches field value. |
@Confirmation | value = "" | Check if the same value is set in the field name of value , and with a _confirmation suffix field. |
The default error messages are defined by activerecord.properties
message
on
The message
option is a string type. This option lets you specify the message that will be added to the com.github.aselab.activerecord.Errors
collection when validation fails.
Note : Can also be set the i18n resource file. (e.g.:
src/main/resources/activerecord_en.properties
)
case class User(@Required(message="custom message") name: String) extends ActiveRecord
object User extends ActiveRecordCompanion[User]
val user = User("").create
println(user.errors.messges) // => Seq("custom message")
The on
option is a string type. This option lets you specify when the validation should happen.
-
save
: The model validation on save. (on both create and update. it's default value) -
create
: The model validation only when a record is created. -
update
: The model validation only when a record is updated.
// it will be possible to update name with a empty value.
case class User(@Required(on="create") name: String) extends ActiveRecord
// it will be possible to create name with a empty value.
case class Person(@Required(on="update") name: String) extends ActiveRecord
// the default (validates on both create and update)
case class Group(@Required(on="save") name: String) extends ActiveRecord
- Default locale is
Locale.getDefault
- The i18n file name is
activerecord_(locale).properties
in classpath.
e.g.) src/main/resources/activerecord_en.properties
- Error message consists of two parts:
ModelAttributeName
andValidationErrorMessage
.
The format is activerecord.models.[Model.class.getSimpleName].[AttributeName]
activerecord.models.User.name = User Name
activerecord.models.User.age = Age
activerecord.models.User.phoneNumber = Phone number
Note : Default translation file is activerecord.properties.
activerecord.errors.invalid = custom error message
activerecord.errors.accepted = custom error message
TODO