[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 = {
|
||||
loading: false,
|
||||
error: null,
|
||||
addForm: {},
|
||||
addForm: {
|
||||
name: "",
|
||||
description: ""
|
||||
},
|
||||
addFormOptions: [],
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
use Respect\Validation\Validator as DataValidator;
|
||||
DataValidator::with('CustomValidations', true);
|
||||
|
||||
/**
|
||||
* @api {post} /system/add-custom-field Add a custom field
|
||||
|
@ -41,7 +42,7 @@ class AddCustomFieldController extends Controller {
|
|||
'error' => ERRORS::INVALID_NAME
|
||||
],
|
||||
'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
|
||||
],
|
||||
'type' => [
|
||||
|
@ -53,7 +54,7 @@ class AddCustomFieldController extends Controller {
|
|||
],
|
||||
'options' => [
|
||||
'validation' => DataValidator::oneOf(
|
||||
DataValidator::json(),
|
||||
DataValidator::ValidOptions(),
|
||||
DataValidator::nullType()
|
||||
),
|
||||
'error' => ERRORS::INVALID_CUSTOM_FIELD_OPTIONS
|
||||
|
@ -68,8 +69,9 @@ class AddCustomFieldController extends Controller {
|
|||
$description = Controller::request('description', true);
|
||||
$options = Controller::request('options');
|
||||
|
||||
if(!Customfield::getDataStore($name, 'name')->isNull())
|
||||
if(!Customfield::getDataStore($name, 'name')->isNull()) {
|
||||
throw new Exception(ERRORS::CUSTOM_FIELD_ALREADY_EXISTS);
|
||||
}
|
||||
|
||||
$customField = new Customfield();
|
||||
$customField->setProperties([
|
||||
|
|
|
@ -11,7 +11,7 @@ class LengthConfig {
|
|||
const MIN_LENGTH_PASSWORD = 6;
|
||||
const MAX_LENGTH_PASSWORD = 200;
|
||||
|
||||
const MIN_LENGTH_DESCRIPTION = 2;
|
||||
const MIN_LENGTH_DESCRIPTION = 0;
|
||||
const MAX_LENGTH_DESCRIPTION = 100;
|
||||
|
||||
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