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

Nametypedefaultdescription
inputToValidatestring/objectThe name or object of the input to validate
errorMessagestring*Error message to be shown when validation fails
validationGroupstring/objectThe name of object of ValidationGroup
cssStylestringCustomize your css style
cssClassstringCustomize 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.

Nametypedefaultdescription
inputToComparestring/objectThe input or its name that needs to compare to
valueToComparemixedGet/set the value that validator will compare to the input's value
operatorstring=The operator accepts "=", "!=", ">", ">=", "<", "<="
errorMessageGet/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.

Nametypedefaultdescription
expressionstringEnter the regular expression string

RangeValidator #

RangeValidator validate whether the input's value is within a range

Nametypedefaultdescription
minnumberSet the min value
maxnumberSet 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:

Nametypedefaultdescription
validationFunctionfunctionCustom 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.

Nametypedefaultdescription
gatheringErrorsboolfalseSet whether the error is gathered to show with validation group
displaymixed"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.