Skip to content

Commit ac936cb

Browse files
committed
form render
1 parent 3178238 commit ac936cb

File tree

4 files changed

+59
-320
lines changed

4 files changed

+59
-320
lines changed

Form.php

+7-267
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
/**
1515
* FormRender: Render form
16-
* Two method render form : php or js (beta)
16+
* Two method form render: php or js (beta)
1717
*
1818
* @author Rafal Marguzewicz <[email protected]>
19-
* @version 1.4
19+
* @version 2.0
2020
* @license MIT
2121
*
2222
* https://github.com/pceuropa/yii2-forum
@@ -48,7 +48,6 @@ class Form extends Widget {
4848
*/
4949
public $formId = null;
5050

51-
5251
/**
5352
* @var array|string JSON Object representing the form body
5453
*/
@@ -62,7 +61,6 @@ class Form extends Widget {
6261

6362
/**
6463
* Initializes the object.
65-
*
6664
* @return void
6765
* @see Widget
6866
*/
@@ -72,7 +70,6 @@ public function init() {
7270
$form = FormModel::FindOne($this->formId);
7371
$this->body = $form->body;
7472
}
75-
7673
$this->body = Json::decode($this->body);
7774
}
7875

@@ -90,8 +87,7 @@ public function run() {
9087

9188

9289
/**
93-
* Create form
94-
* Render form by PHP language.
90+
* Render form by PHP and add rules
9591
* @param array $form
9692
* @return View Form
9793
*/
@@ -113,274 +109,18 @@ public function phpRender($form) {
113109
}
114110

115111
return $this->render('form_php', [
116-
'form_body' => $form,
117-
'model' => $DynamicModel
118-
]);
112+
'form_body' => $form,
113+
'model' => $DynamicModel
114+
]);
119115
}
120116

121117
/**
122-
* Create form
123-
* Render form by JavaScript language.
118+
* Render form by JavaScript
124119
* @param array $form
125120
* @return View
126121
*/
127122
public function jsRender($form) {
128123
return $this->render('form_js', ['form' => $form]);
129124
}
130-
131-
132-
/**
133-
* Select and return function render field
134-
* Render field in view
135-
* @param yii\bootstrap\ActiveForm $form
136-
* @param DynamicModel $model
137-
* @param array $field
138-
* @return string Return field in div HTML
139-
*/
140-
public static function field($form, $model, $field = null) {
141-
142-
$width = $field['width'];
143-
144-
switch ($field['field']) {
145-
case 'input':
146-
$field = self::input($form, $model, $field);
147-
break;
148-
case 'textarea':
149-
$field = self::textarea($form, $model, $field);
150-
break;
151-
case 'radio':
152-
$field = self::radio($form, $model, $field);
153-
break;
154-
case 'checkbox':
155-
$field = self::checkbox($form, $model, $field);
156-
break;
157-
case 'select':
158-
$field = self::select($form, $model, $field);
159-
break;
160-
case 'description':
161-
$field = self::description($field);
162-
break;
163-
case 'submit':
164-
$field = self::submit($field);
165-
break;
166-
default:
167-
$field = '';
168-
break;
169-
}
170-
171-
return self::div($width, $field);
172-
173-
}
174-
175-
/**
176-
* Return HTML div with field
177-
* @param string $width Class bootstrap
178-
* @param string $field
179-
* @return string
180-
*/
181-
public static function div($width, $field) {
182-
return '<div class="'.$width.'">'. $field .'</div>';
183-
}
184-
185-
/**
186-
* Title description
187-
* @param string $arg
188-
* @return void
189-
*/
190-
public function mergeOptions($options, $field) {
191-
192-
foreach (['placeholder', 'value', 'id', 'class'] as $key => $value) {
193-
if (isset($field[$value])) {
194-
$options[$value] = $field[$value];
195-
}
196-
}
197-
}
198-
199-
/**
200-
* Renders an input tag
201-
* @param yii\bootstrap\ActiveForm $form
202-
* @param DynamicModel $model
203-
* @param array $_data
204-
* @return this The field object itself.
205-
*/
206-
public function input($form, $model, $field) {
207-
208-
$options = [];
209-
if (!isset($field['name'])) return;
210-
211-
foreach (['placeholder', 'value', 'id', 'class'] as $key => $value) {
212-
if (isset($field[$value])) {
213-
$options[$value] = $field[$value];
214-
}
215-
}
216-
217-
$input = $form->field($model, $field['name'] )->input($field['type'], $options);
218-
$input->label($field['label'] ?? false);
219-
220-
return $input;
221-
}
222-
223-
/**
224-
* Renders a text area.
225-
* @param yii\bootstrap\ActiveForm $form
226-
* @param DynamicModel $model
227-
* @param array $field
228-
* @return $this The field object itself.
229-
*/
230-
public static function textarea($form, $model, $field) {
231-
$options = [];
232-
$template ="{label}\n{input}\n{hint}\n{error}";
233-
234-
if (!isset($field['name'])) return;
235-
236-
foreach (['placeholder', 'value', 'id', 'class'] as $key => $value) {
237-
if (isset($field[$value])) {
238-
$options[$value] = $field[$value];
239-
}
240-
}
241-
242-
$text_area = $form->field($model, $field['name'])->textArea($options);
243-
$text_area->label($field['label'] ?? false);
244-
245-
return $text_area;
246-
}
247-
248-
/**
249-
* Renders a list of radio buttons.
250-
* @param yii\bootstrap\ActiveForm $form
251-
* @param DynamicModel $model
252-
* @param array $field
253-
* @return $this The field object itself.
254-
*/
255-
public static function radio($form, $model, $field) {
256-
257-
$items = [];
258-
$checked = [];
259-
260-
foreach ($field['items'] as $key => $value) {
261-
$items[$value['value']] = $value['text'];
262-
if (isset( $value['checked'] )) {
263-
$checked[] = $key+1;
264-
}
265-
}
266-
267-
$model-> {$field['name']} = $checked;
268-
$radio_list = $form->field($model, $field['name'])->radioList($items);
269-
270-
$label = (isset($field['label'])) ? $field['label'] : '';
271-
272-
$radio_list->label($label, ['class' => 'bold']);
273-
274-
return $radio_list;
275-
}
276-
277-
/**
278-
* Renders a list of checkboxes.
279-
* @param yii\bootstrap\ActiveForm $form
280-
* @param DynamicModel $model
281-
* @param array $field
282-
* @return $this The field object itself.
283-
*/
284-
public static function checkbox($form, $model, $field) {
285-
$items = [];
286-
$checked = [];
287-
288-
foreach ($field['items'] as $key => $value) {
289-
290-
$items[$value['value']] = $value['text'];
291-
if (isset($value['checked'])) {
292-
$checked[] = $key+1;
293-
}
294-
295-
}
296-
$items = ArrayHelper::map($field['items'], 'value', 'text');
297-
$model-> {$field['name']} = $checked;
298-
$checkbox_list = $form->field($model, $field['name'])->checkboxList($items);
299-
300-
$label = (isset($field['label'])) ? $field['label'] : '';
301-
$checkbox_list->label($label);
302-
303-
return $checkbox_list;
304-
305-
306-
307-
}
308-
309-
/**
310-
* Renders a drop-down list.
311-
* @param yii\bootstrap\ActiveForm $form
312-
* @param DynamicModel $model
313-
* @param array $field
314-
* @return $this The field object itself.
315-
*/
316-
317-
public static function select($form, $model, $field) {
318-
if (ArrayHelper::keyExists('name', $field) ) {
319-
$items = [];
320-
$checked = [];
321-
322-
foreach ($field['items'] as $key => $value) {
323-
$items[$value['value']] = $value['text'];
324-
325-
if (isset($value['checked'])) {
326-
$checked[] = $key+1;
327-
}
328-
}
329-
330-
$model-> {$field['name']} = $checked;
331-
$select = $form->field($model, $field['name'])->dropDownList($items);
332-
$label = (isset($field['label'])) ? $field['label'] : '';
333-
$select->label($label);
334-
return $select;
335-
}
336-
}
337-
338-
/**
339-
* Renders a description html.
340-
* @param array $v
341-
* @return string
342-
*/
343-
public static function description($v) {
344-
return $v['textdescription'];
345-
}
346-
347-
/**
348-
* Renders a submit buton tag.
349-
* @param array $data
350-
* @return string The generated submit button tag
351-
*/
352-
public static function submit($data) {
353-
return Html::submitButton($data['label'], ['class' => 'btn '.$data['backgroundcolor'] ]);
354-
}
355-
356-
/**
357-
* Send email
358-
* @param string $emailSender
359-
* @param string $to
360-
* @param string $subject
361-
* @param string $response
362-
* @return void
363-
*/
364-
public function sendEmail($emailSender = null, $to = null, $subject = null, $response = null) {
365-
366-
if (!is_string($emailSender) && !is_string($to)) {
367-
return;
368-
}
369-
370-
SendEmail::widget([
371-
'from' => $emailSender,
372-
'to' => $to,
373-
'subject' => $subject,
374-
'textBody' => $response,
375-
]);
376-
}
377125
}
378-
379126
?>
380-
381-
382-
383-
384-
385-
386-

bootstrap/ActiveForm.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function dynamicField($model, $field) {
8484
$field = parent::field($model, $field['name'], $options)->checkboxList($items)->label($field['label'] ?? false);
8585
break;
8686
case 'select':
87-
$field = parent::field($model, $field['name'], $options)->dropDownList($items)->label(false);
87+
$field = parent::field($model, $field['name'], $options)->dropDownList($items)->label($field['label'] ?? false);
8888
break;
8989
case 'description':
9090
$field = self::description($field);

0 commit comments

Comments
 (0)