Closed lynicidn closed 8 months ago
why would you need this? We have template https://github.com/yiisoft/yii2/blob/master/framework/widgets/ActiveField.php#L55 and parts https://github.com/yiisoft/yii2/blob/master/framework/widgets/ActiveField.php#L144 which lets you control everything already.
@cebe i want change default behavior for generate input part of template in extension. Without it i should extend yii\bootstrap\ActiveField and yii\widgets\ActiveField with this changes i can write extension unknowing what user is use
@cebe also with it i can set function only in one place in code
problem in bootstrap extension - i can't write extensions for form or write for concrete yii\bootstrap\ActiveField or yii\widgets\ActiveField
reopen it please and mark as under discussion
ok. why we always use textInput
, why not textarea
as default ?
i think user should set default input type for ActiveField
or do it as here - https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseHtml.php#L1186 if string <= 32 - textInput, other - textarea
Вообщем решать вам, но имхо подход на корню неверный, почему разрабочик не может повлиять на формирования филда по умолчанию. И раз уж на то пошло, то объект филд должен иметь свойство тип
и методы аля textInput должны просто устанавливать этот тип
, по умолчанию он text, но никак не генерировать налету html код - это не ооп подход, т.к. повлиять уже на сгенерированный код нельзя. Второй проблемой является то что написав свой филд вы должны выбрать расширяться от бутстрап или от дефолтного - следовательно забываем про расширения для форм, т.к. охватить оба варианта не получится.
can you show a concrete use case? why can't you extend ActiveField class to adjust its behavior? Sry, my russian is not good enough to understand the last part.
@cebe i write extension for ActiveField - what is class i should extend - yii\bootstrap\ActiveField or yii\widgets\ActiveField ? I purpose case not break BC and it solve problem with override logic for generate field as default If now i should render every field as:
echo $form->field($model, 'name')->render($function);
echo $form->field($model, 'altname')->render($function);
vs
echo $form->field($model, 'name');
echo $form->field($model, 'altname');
i don't think that use anonymous function in view it good idea. I purpose move it in property and allow configure it via ActiveForm::fieldConfig. simple example case:
'fieldConfig' => [
'renderContent' => function($field) {
if (!isset($field->parts['{input}'])) {
$field->parts['{input}'] = myRenderLogic($field);
}
return strtr($field->template, $field->parts);
}
]
in myRenderLogic
i can generate field based on validators or other logic instead of use textInput for all. I want write universe extension for bootstrap and classic widgets. More example: i want add support hint
in Model via attributeHints()
- now i should create 2 classes - for bootstrap and for classic.
Also i don't think that generate html code on fly for input part of template is OOP style. I mean that textInput, textarea and other methods should set field type
property - as example fileInput should add multipart/form for form (DRY), but we generate html in init
.
More example: i want create field types instances as in symfony
'fieldConfig' => [
'renderContent' => function($field) {
if (!isset($field->parts['{input}'])) {
$instance = $field->model->getFieldTypeInstance($field->attribute);
$field->parts['{input}'] = $instance->render();
}
return strtr($field->template, $field->parts);
}
]
can you give a link to the symfony documentation?
hm, i don't read guid :) i read code types here https://github.com/symfony/symfony/tree/master/src/Symfony/Component/Form/Extension/Core/Type
also am find cookbook http://symfony.com/doc/current/cookbook/form/index.html
I think it will be good feature for autobuilding forms, and add more flexibility ...
here https://github.com/yiisoft/yii2/blob/master/framework/widgets/ActiveField.php#L156 we do render without args - i propose add new property for active field with
null
value as default. Bonus: we can configure default behavior for generatelabel
,hint
andinput
oftemplate
parts
now it need do for every attribute: