Skip to content
Permalink
Browse files

Properties validation rules

  • Loading branch information...
sergejey committed Sep 30, 2019
1 parent 8cab832 commit 334c8459bc5e81e4b4c8d8f7c6e860204fc11f8c
@@ -1401,6 +1401,12 @@
'BLOCK_SENSOR' => 'Block sensor',
'UNBLOCK_SENSOR' => 'Un-block sensor',
'VALIDATION_TYPE' => 'Value validation',
'VALIDATION_TYPE_NUMBER' => 'Number (in range)',
'VALIDATION_TYPE_BINARY' => 'Binary value (1/0)',
'VALIDATION_TYPE_DICTIONARY' => 'Dictionary match',
'VALIDATION_TYPE_CUSTOM' => 'Custom (code)',
'TEST' => 'test'
@@ -1399,6 +1399,12 @@
'BLOCK_SENSOR' => 'Блокировать датчик',
'UNBLOCK_SENSOR' => 'Разблокировать датчик',
'VALIDATION_TYPE' => 'Проверка значений',
'VALIDATION_TYPE_NUMBER' => 'Число в диапазоне',
'VALIDATION_TYPE_BINARY' => 'Бинарное значение (1/0)',
'VALIDATION_TYPE_DICTIONARY' => 'По словарю',
'VALIDATION_TYPE_CUSTOM' => 'Расширенный (кодом)',
/* end module names */
@@ -813,6 +813,31 @@ function setProperty($property, $value, $no_linked = 0, $source = '')
startMeasure('setproperty_update');
if ($id) {
$prop = SQLSelectOne("SELECT * FROM properties WHERE ID='" . $id . "'");
if ($prop['VALIDATION_TYPE']==1) {
if (!is_numeric($value)) return false;
if ($prop['VALIDATION_NUM_MIN']!='' && (float)$value<(float)$prop['VALIDATION_NUM_MIN']) {
return false;
}
if ($prop['VALIDATION_NUM_MAX']!='' && (float)$value>(float)$prop['VALIDATION_NUM_MAX']) {
return false;
}
}
if ($prop['VALIDATION_TYPE']==2) {
if ($value!='1' && $value!='0') {
return false;
}
}
if ($prop['VALIDATION_TYPE']==3) {
$items=explode(',',$prop['VALIDATION_LIST']);
if (!in_array(mb_strtolower($value,'UTF-8'),$items)) return false;
}
if ($prop['VALIDATION_TYPE']==100) {
eval($prop['VALIDATION_CODE']);
if (is_null($value)) return false;
}
$property = $prop['TITLE'];
startMeasure('setproperty_update_getvalue');
$v = SQLSelectOne("SELECT * FROM pvalues WHERE PROPERTY_ID=" . (int)$id . " AND OBJECT_ID=" . (int)$this->id );
@@ -1117,6 +1142,11 @@ function dbInstall($data)
properties: DATA_KEY int(3) NOT NULL DEFAULT '0'
properties: DATA_TYPE int(3) NOT NULL DEFAULT '0'
properties: DESCRIPTION text
properties: VALIDATION_TYPE int(3) NOT NULL DEFAULT '0'
properties: VALIDATION_NUM_MIN varchar(20) NOT NULL DEFAULT ''
properties: VALIDATION_NUM_MAX varchar(20) NOT NULL DEFAULT ''
properties: VALIDATION_LIST varchar(255) NOT NULL DEFAULT ''
properties: VALIDATION_CODE text
properties: ONCHANGE varchar(255) NOT NULL DEFAULT ''
properties: INDEX (CLASS_ID)
properties: INDEX (OBJECT_ID)
@@ -57,6 +57,33 @@
global $data_type;
$rec['DATA_TYPE']=(int)$data_type;
$rec['VALIDATION_TYPE']=gr('validation_type','int');
$rec['VALIDATION_NUM_MIN']=gr('validation_num_min');
$rec['VALIDATION_NUM_MAX']=gr('validation_num_max');
if ($rec['VALIDATION_TYPE']==3) {
$rec['VALIDATION_LIST']=gr('validation_list');
if (!$rec['VALIDATION_LIST']) {
$out['ERR_VALIDATION_LIST']=1;
$ok=0;
} else {
$tmp=explode(',',$rec['VALIDATION_LIST']);
$total = count($tmp);
for($i=0;$i<$total;$i++) {
$tmp[$i]=trim($tmp[$i]);
$tmp[$i]=mb_strtolower($tmp[$i],'UTF-8');
}
$rec['VALIDATION_LIST']=implode(',',$tmp);
}
}
if ($rec['VALIDATION_TYPE']==100) {
$rec['VALIDATION_CODE']=gr('validation_code');
$errors=php_syntax_error($rec['VALIDATION_CODE']);
if ($errors) {
$out['ERR_VALIDATION_CODE']=$errors;
$ok=0;
}
}
//updating 'Description' (text)
global $description;
$rec['DESCRIPTION']=$description;
@@ -63,6 +63,69 @@
</div>
</div>

<script type="text/javascript">
function validationTypeUpdated() {
var vType=$('#validation_type').val();
$('#validation_min_max').hide();
$('#validation_dictionary').hide();
$('#validation_code').hide();
if (vType=='1') {
$('#validation_min_max').show();
}
if (vType=='3') {
$('#validation_dictionary').show();
}
if (vType=='100') {
$('#validation_code').show();
}
//validation_min_max
//validation_dictionary
//validation_code
}
</script>
<div class="form-group">
<label class="col-lg-4 control-label"><#LANG_VALIDATION_TYPE#>:<#LANG_HCB#>validationtype<#LANG_HCE#></label>
<div class="col-lg-4">
<select name="validation_type" id="validation_type" class="form-control" onchange="validationTypeUpdated();">
<option value="0"[#if VALIDATION_TYPE="0"#] selected[#endif#]><#LANG_NO#>
<option value="1"[#if VALIDATION_TYPE="1"#] selected[#endif#]><#LANG_VALIDATION_TYPE_NUMBER#></option>
<option value="2"[#if VALIDATION_TYPE="2"#] selected[#endif#]><#LANG_VALIDATION_TYPE_BINARY#></option>
<option value="3"[#if VALIDATION_TYPE="3"#] selected[#endif#]><#LANG_VALIDATION_TYPE_DICTIONARY#></option>
<option value="100"[#if VALIDATION_TYPE="100"#] selected[#endif#]><#LANG_VALIDATION_TYPE_CUSTOM#></option>
</select>
</div>
</div>

<div id="validation_min_max"[#if VALIDATION_TYPE!="1"#] style="display:none"[#endif#]>
<div class="form-group">
<label class="col-lg-4 control-label"><#LANG_MIN_VALUE#>:</label>
<div class="col-lg-4">
<input type="text" name="validation_num_min" value="[#VALIDATION_NUM_MIN#]" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label"><#LANG_MAX_VALUE#>:</label>
<div class="col-lg-4">
<input type="text" name="validation_num_max" value="[#VALIDATION_NUM_MAX#]" class="form-control">
</div>
</div>
</div>

<div class="form-group[#if ERR_VALIDATION_LIST="1"#] has-error[#endif#]" id="validation_dictionary"[#if VALIDATION_TYPE!="3"#] style="display:none"[#endif#]>
<label class="col-lg-4 control-label"><#LANG_VALIDATION_TYPE_DICTIONARY#>:</label>
<div class="col-lg-4">
<input type="text" name="validation_list" value="[#VALIDATION_LIST#]" class="form-control">
</div>
</div>

<div class="form-group[#if ERR_VALIDATION_CODE!=""#] has-error[#endif#]" id="validation_code"[#if VALIDATION_TYPE!="100"#] style="display:none"[#endif#]>
<label class="col-lg-4 control-label"><#LANG_CODE#>:</label>
<div class="col-lg-4">
[#if ERR_VALIDATION_CODE!=""#]<div>[#ERR_VALIDATION_CODE#]</div>[#endif#]
<textarea name="validation_code" rows="4" class="form-control" placeholder="$value">[#VALIDATION_CODE#]</textarea>
</div>
</div>

<div class="form-group ">
<label class="col-lg-4 control-label"><#LANG_DATA_KEY#>:<#LANG_HCB#>datakey<#LANG_HCE#></label>
<div class="col-lg-4">

0 comments on commit 334c845

Please sign in to comment.
You can’t perform that action at this time.