Validators
Overview #
Validators are group of widgets used to validate inputs widget in Dashboard.
Example:
use \koolreport\dashboard\Dashboard;
use \koolreport\dashboard\inputs\TextBox;
use \koolreport\dashboard\validators\RequiredFieldValidator;
class MyDashboard extends Dashboard
{
protected function content()
{
return [
//Input to be validated
TextBox::create("name"),
//Validator
RequiredFieldValidator::create("nameRequired")
->inputToValidate("name")
->errorMessage("Name field is required"),
// Hit button, the name will be checked,
// If correct, do something
// If wrong the error message will automaticlly show.
Button::create("submitName")
->action("submit",function($resquest, $response){
if($this->sibling("name")->validate()->isValid()) {
// Do something when form is valid
}
$this->sibling("nameRequired")->update();//Update the status of validator
})
];
}
}
You can see from above example, we have a RequiredFieldValidator
added and point to input "name"
as the input to validate.
Common properties #
Below are common property of a filter
Name | type | default | description |
---|---|---|---|
inputToValidate | string/object | The name or object of the input to validate | |
errorMessage | string | * | Error message to be shown when validation fails |
validationGroup | string/object | The name of object of ValidationGroup | |
cssStyle | string | Customize your css style | |
cssClass | string | Customize the css class |
List of validators #
RequiredFieldValidator #
RequireFieldValidator validate if input is empty. If the input is empty then it will show error message.
CompareValidator #
CompareValidator let us compare an input's value with a predefined value or compare 2 input's values with each others.
Name | type | default | description |
---|---|---|---|
inputToCompare | string/object | The input or its name that needs to compare to | |
valueToCompare | mixed | Get/set the value that validator will compare to the input's value | |
operator | string | = | The operator accepts "=", "!=", ">", ">=", "<", "<=" |
errorMessage | Get/set error message |
Example:
protected function content()
{
return [
TextBox::create("aBox"),
TextBox::create("bBox"),
CompareValidator::create("compareAandB")
->inputToValidate("aBox")
->inputToCompare("bBox")
->operator(">"),
Button::create("submitName")
->action("submit",function($resquest, $response){
if($this->sibling("name")->validate()->isValid()) {
// Do something when form is valid
}
})
];
}
NumericValidator #
The NumericValidator check if an input's value is numeric.
RegularExpressionValidator #
RegularExpressionValidator is used to validate an input's value by an regular expression.
Name | type | default | description |
---|---|---|---|
expression | string | Enter the regular expression string |
RangeValidator #
RangeValidator validate whether the input's value is within a range
Name | type | default | description |
---|---|---|---|
min | number | Set the min value | |
max | number | Set the max value |
EmailValidator #
EmailValidator validate whether input's value is a correct email.
CustomValidator #
CustomValidator is an special validator in which you can set your own validation function to check value:
Name | type | default | description |
---|---|---|---|
validationFunction | function | Custom function which receive value as parameter and return whether value is valid |
Example:
CustomValidator::create("myOwnValidator")
->validationFunction(function($value){
//Only accept even value
return $value % 2 === 0;
});
ValidationGroup #
ValidationGroup can group the error message from all validators to show in one place.
Name | type | default | description |
---|---|---|---|
gatheringErrors | bool | false | Set whether the error is gathered to show with validation group |
display | mixed | "ul" | Accept value "ul" , "il" or any anonymous function to display errors |
Example:
protected function content()
{
return [
TextBox::create("age"),
RequiredFieldValidator::create("ageRequired")
->inputToValidate("age")
->errorMessage("Age field is required")
->validationGroup("vGroup"), // Add string validator into validation group
NumericValidator::create("ageNumeric")
->inputToValidate("age")
->errorMessage("Age field is not a number")
->validationGroup("vGroup"), // Add numeric validator into validation group
ValidationGroup::create("vGroup")
->gatheringErrors(true)
->display(function($errors){
return implode("<br/>",$errors);
});
Button::create("submitName")
->action("submit",function($resquest, $response){
if($this->sibling("vGroup")->validate()->isValid()) {
// Do something all is valid
}
$this->sibling("vGroup")->update();
})
];
}
Get started with KoolReport
KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.