Skip to content

Commit 695c9a2

Browse files
committed
add DataFormHelper for Ubiquity
1 parent 9067369 commit 695c9a2

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

Ajax/common/Widget.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function __construct($identifier, $model, $modelInstance = NULL) {
7171
$this->_template = "%wrapContentBefore%%content%%wrapContentAfter%";
7272
$this->setModel($model);
7373
if (isset($modelInstance)) {
74+
$this->_model=get_class($modelInstance);
7475
$this->show($modelInstance);
7576
}
7677
$this->_generated = false;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
namespace Ajax\php\ubiquity\utils;
3+
4+
use Ajax\semantic\widgets\dataform\DataForm;
5+
use Ubiquity\contents\validation\ValidatorsManager;
6+
use Ubiquity\orm\OrmUtils;
7+
8+
class DataFormHelper {
9+
10+
/**
11+
* Adds the instance members as form fields.
12+
*
13+
* @param DataForm $form
14+
*/
15+
public static function defaultFields(DataForm $form): void {
16+
$form->setFields(OrmUtils::getFormAllFields($form->getModel()));
17+
}
18+
19+
/**
20+
* Adds the default ui constraints to the form.
21+
*
22+
* @param DataForm $form
23+
*/
24+
public static function defaultUIConstraints(DataForm $form): void {
25+
self::addDefaultUIConstraints($form, OrmUtils::getFieldTypes($form->getModel()), ValidatorsManager::getUIConstraints($form->getModelInstance()));
26+
}
27+
28+
/**
29+
*
30+
* @param DataForm $form
31+
* @param array $fieldTypes
32+
* @param array $uiConstraints
33+
*/
34+
public static function addDefaultUIConstraints(DataForm $form, array $fieldTypes, array $uiConstraints): void {
35+
foreach ($fieldTypes as $property => $type) {
36+
$rules = $uiConstraints[$property] ?? [];
37+
if ($hasRules = \count($rules) > 0) {
38+
$form->setValidationParams([
39+
"on" => "blur",
40+
"inline" => true
41+
]);
42+
}
43+
$noPName = false;
44+
$noPType = false;
45+
switch ($property) {
46+
case 'password':
47+
$form->fieldAsInput($property, [
48+
'inputType' => 'password'
49+
] + $rules);
50+
break;
51+
case 'email':
52+
case 'mail':
53+
$form->fieldAsInput($property, $rules);
54+
break;
55+
default:
56+
$noPName = true;
57+
}
58+
59+
switch ($type) {
60+
case 'tinyint(1)':
61+
case 'bool':
62+
case 'boolean':
63+
$form->fieldAsCheckbox($property, \array_diff($rules['rules'] ?? [], [
64+
'empty'
65+
]));
66+
break;
67+
case 'int':
68+
case 'integer':
69+
$form->fieldAsInput($property, [
70+
'inputType' => 'number'
71+
] + $rules);
72+
break;
73+
case 'date':
74+
$form->fieldAsInput($property, [
75+
'inputType' => 'date'
76+
] + $rules);
77+
break;
78+
case 'datetime':
79+
$form->fieldAsInput($property, [
80+
'inputType' => 'datetime-local'
81+
] + $rules);
82+
break;
83+
case 'text':
84+
$form->fieldAsTextarea($property, $rules);
85+
break;
86+
default:
87+
$noPType = true;
88+
}
89+
if ($hasRules && $noPName && $noPType) {
90+
$form->fieldAsInput($property, $rules);
91+
}
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)