validates_numericality_of¶
The validates_numericality_of validation in ActiveRecord allows you to validate whether an attribute's value is a valid numeric value. It ensures that the attribute contains a number and optionally checks for numerical constraints such as minimum and maximum values. To use validates_numericality_of, you need to specify the following options:
-
Attribute Name: This option specifies the name of the attribute you want to validate for numericality. It is typically set as a symbol or string.
static $validates_numericality_of = [ 'age' => [] ]; -
Message (Optional): To customize the error message displayed when the validation fails, you can use the
messageoption to provide a custom error message.static $validates_numericality_of = [ 'price' => [ 'message' => 'Price must be a valid number.', ], ]; -
Only Integer (Optional): The
only_integeroption allows you to specify whether the attribute's value must be an integer. By default,only_integeris set tofalse, meaning that any numeric value is accepted. You can set it totrueto enforce integer values.static $validates_numericality_of = [ 'quantity' => [ 'only_integer' => true ] ]; -
Greater Than (Optional): The
greater_thanoption allows you to specify a minimum value that the attribute's value must be greater than. It ensures that the numeric value is greater than the specified threshold.static $validates_numericality_of = [ 'score' => [ 'greater_than' => 0 ] ]; -
Greater Than Or Equal To (Optional): The
greater_than_or_equal_tooption allows you to specify a minimum value that the attribute's value must be greater than or equal to. It ensures that the numeric value is greater than or equal to the specified threshold.static $validates_numericality_of = [ 'temperature' => [ 'greater_than_or_equal_to' => -20 ] ]; -
Less Than (Optional): The
less_thanoption allows you to specify a maximum value that the attribute's value must be less than. It ensures that the numeric value is less than the specified threshold.static $validates_numericality_of = [ 'discount' => [ 'less_than' => 100 ] ]; -
Less Than Or Equal To (Optional): The
less_than_or_equal_tooption allows you to specify a maximum value that the attribute's value must be less than or equal to. It ensures that the numeric value is less than or equal to the specified threshold.static $validates_numericality_of = [ 'percentage' => [ 'less_than_or_equal_to' => 100 ] ]; -
Equal To (Optional): The
equal_tooption allows you to specify a value that the attribute's value must be equal to. It ensures that the numeric value matches the specified value.static $validates_numericality_of = [ 'quantity' => [ 'equal_to' => 10 ] ]; -
Allow Blank (Optional): The
allow_blankoption allows you to specify whether the attribute is allowed to be empty (e.g., an empty string). By default,allow_blankis set tofalse, meaning that the attribute must not be blank. You can set it totrueto allow blank values.static $validates_numericality_of = [ 'price' => [ 'allow_blank' => true ] ]; -
Allow Null (Optional): The
allow_nulloption allows you to specify whether the attribute is allowed to be null. By default,allow_nilis set tofalse, meaning that the attribute must not be null. You can set it totrueto allow null values.static $validates_numericality_of = [ 'height' => [ 'allow_null' => true ] ]; -
On (Optional): The
onoption allows you to specify when the validation should occur. By default, it is set to'save', which means the validation is performed when the record is saved. You can set it to'create'to validate only during the creation of a new record or'update'to validate only during updates.static $validates_numericality_of = [ 'quantity' => [ 'on' => 'create' ] ];
Example of Usage:
class Product extends ActiveRecord\Model {
static $validates_numericality_of = [
'quantity' => [
'only_integer' => true,
'greater_than' => 0
]
];
}