Nvd\Crud\Db::getValidationRule PHP Method

getValidationRule() public static method

public static getValidationRule ( $field )
    public static function getValidationRule($field)
    {
        // skip certain fields
        if (in_array($field->name, static::skippedFields())) {
            return "";
        }
        $rules = [];
        // required fields
        if ($field->required) {
            $rules[] = "required";
        }
        // strings
        if (in_array($field->type, ['varchar', 'text'])) {
            $rules[] = "string";
            if ($field->maxLength) {
                $rules[] = "max:" . $field->maxLength;
            }
        }
        // dates
        if (in_array($field->type, ['date', 'datetime'])) {
            $rules[] = "date";
        }
        // numbers
        if (in_array($field->type, ['int', 'unsigned_int'])) {
            $rules[] = "integer";
        }
        // emails
        if (preg_match("/email/", $field->name)) {
            $rules[] = "email";
        }
        // enums
        if ($field->type == 'enum') {
            $rules[] = "in:" . join(",", $field->enumValues);
        }
        return "'" . $field->name . "' => '" . join("|", $rules) . "',";
    }