[DEV-163] Fix custom fields issue (#1079)
* Fix custom fields issue * Add custom fields options validation * Add not blank option validation * Fix description length
This commit is contained in:
parent
eaeaaa647e
commit
edddc8d2c5
|
@ -21,7 +21,10 @@ class AdminPanelCustomFieldForm extends React.Component {
|
||||||
state = {
|
state = {
|
||||||
loading: false,
|
loading: false,
|
||||||
error: null,
|
error: null,
|
||||||
addForm: {},
|
addForm: {
|
||||||
|
name: "",
|
||||||
|
description: ""
|
||||||
|
},
|
||||||
addFormOptions: [],
|
addFormOptions: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
use Respect\Validation\Validator as DataValidator;
|
use Respect\Validation\Validator as DataValidator;
|
||||||
|
DataValidator::with('CustomValidations', true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @api {post} /system/add-custom-field Add a custom field
|
* @api {post} /system/add-custom-field Add a custom field
|
||||||
|
@ -41,7 +42,7 @@ class AddCustomFieldController extends Controller {
|
||||||
'error' => ERRORS::INVALID_NAME
|
'error' => ERRORS::INVALID_NAME
|
||||||
],
|
],
|
||||||
'description' => [
|
'description' => [
|
||||||
'validation' => DataValidator::notBlank()->length(LengthConfig::MIN_LENGTH_DESCRIPTION, LengthConfig::MAX_LENGTH_DESCRIPTION),
|
'validation' => DataValidator::length(LengthConfig::MIN_LENGTH_DESCRIPTION, LengthConfig::MAX_LENGTH_DESCRIPTION),
|
||||||
'error' => ERRORS::INVALID_DESCRIPTION
|
'error' => ERRORS::INVALID_DESCRIPTION
|
||||||
],
|
],
|
||||||
'type' => [
|
'type' => [
|
||||||
|
@ -53,7 +54,7 @@ class AddCustomFieldController extends Controller {
|
||||||
],
|
],
|
||||||
'options' => [
|
'options' => [
|
||||||
'validation' => DataValidator::oneOf(
|
'validation' => DataValidator::oneOf(
|
||||||
DataValidator::json(),
|
DataValidator::ValidOptions(),
|
||||||
DataValidator::nullType()
|
DataValidator::nullType()
|
||||||
),
|
),
|
||||||
'error' => ERRORS::INVALID_CUSTOM_FIELD_OPTIONS
|
'error' => ERRORS::INVALID_CUSTOM_FIELD_OPTIONS
|
||||||
|
@ -68,8 +69,9 @@ class AddCustomFieldController extends Controller {
|
||||||
$description = Controller::request('description', true);
|
$description = Controller::request('description', true);
|
||||||
$options = Controller::request('options');
|
$options = Controller::request('options');
|
||||||
|
|
||||||
if(!Customfield::getDataStore($name, 'name')->isNull())
|
if(!Customfield::getDataStore($name, 'name')->isNull()) {
|
||||||
throw new Exception(ERRORS::CUSTOM_FIELD_ALREADY_EXISTS);
|
throw new Exception(ERRORS::CUSTOM_FIELD_ALREADY_EXISTS);
|
||||||
|
}
|
||||||
|
|
||||||
$customField = new Customfield();
|
$customField = new Customfield();
|
||||||
$customField->setProperties([
|
$customField->setProperties([
|
||||||
|
|
|
@ -11,7 +11,7 @@ class LengthConfig {
|
||||||
const MIN_LENGTH_PASSWORD = 6;
|
const MIN_LENGTH_PASSWORD = 6;
|
||||||
const MAX_LENGTH_PASSWORD = 200;
|
const MAX_LENGTH_PASSWORD = 200;
|
||||||
|
|
||||||
const MIN_LENGTH_DESCRIPTION = 2;
|
const MIN_LENGTH_DESCRIPTION = 0;
|
||||||
const MAX_LENGTH_DESCRIPTION = 100;
|
const MAX_LENGTH_DESCRIPTION = 100;
|
||||||
|
|
||||||
const MIN_LENGTH_TOKEN = 1;
|
const MIN_LENGTH_TOKEN = 1;
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace CustomValidations;
|
||||||
|
|
||||||
|
use Respect\Validation\Rules\AbstractRule;
|
||||||
|
|
||||||
|
class ValidOptions extends AbstractRule {
|
||||||
|
|
||||||
|
public function validate($options) {
|
||||||
|
if(is_array(json_decode($options))) {
|
||||||
|
$arrayOptions = json_decode($options);
|
||||||
|
|
||||||
|
if(2 <= (sizeof($arrayOptions))) {
|
||||||
|
foreach($arrayOptions as $option) {
|
||||||
|
if(!$option && $option != '0' || empty(trim($option))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue