yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

add property yii\widgets\ActiveField::renderContent = null #7877

Closed lynicidn closed 8 months ago

lynicidn commented 9 years ago

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 generate label, hint and input of template parts

'fieldConfig' => [
    'renderContent' => function($field) {
        if (!isset($field->parts['{input}'])) {
            $field->parts['{input}'] = myLogicForGenerateInputAsDefault($field->model, $field->attribute, $field->inputOptions);
        }
        return strtr($field->template, $field->parts);
    }
]

now it need do for every attribute:

$form->field($model, 'attr')->inputText()->render(function($field){/*...*/});
//or
$form->field($model, 'attr')->render(function($field){/*...*/});
cebe commented 9 years 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.

lynicidn commented 9 years ago

@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

lynicidn commented 9 years ago

@cebe also with it i can set function only in one place in code

lynicidn commented 9 years ago

problem in bootstrap extension - i can't write extensions for form or write for concrete yii\bootstrap\ActiveField or yii\widgets\ActiveField

lynicidn commented 9 years ago

reopen it please and mark as under discussion

lynicidn commented 9 years ago

ok. why we always use textInput, why not textarea as default ?

lynicidn commented 9 years ago

i think user should set default input type for ActiveField

lynicidn commented 9 years ago

or do it as here - https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseHtml.php#L1186 if string <= 32 - textInput, other - textarea

lynicidn commented 9 years ago

Вообщем решать вам, но имхо подход на корню неверный, почему разрабочик не может повлиять на формирования филда по умолчанию. И раз уж на то пошло, то объект филд должен иметь свойство тип и методы аля textInput должны просто устанавливать этот тип, по умолчанию он text, но никак не генерировать налету html код - это не ооп подход, т.к. повлиять уже на сгенерированный код нельзя. Второй проблемой является то что написав свой филд вы должны выбрать расширяться от бутстрап или от дефолтного - следовательно забываем про расширения для форм, т.к. охватить оба варианта не получится.

cebe commented 9 years ago

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.

lynicidn commented 9 years ago

@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.

lynicidn commented 9 years ago

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);
    }
]
cebe commented 9 years ago

can you give a link to the symfony documentation?

lynicidn commented 9 years ago

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

lynicidn commented 9 years ago

http://symfony.com/doc/current/reference/forms/types.html

lynicidn commented 9 years ago

also am find cookbook http://symfony.com/doc/current/cookbook/form/index.html

Insolita commented 9 years ago

I think it will be good feature for autobuilding forms, and add more flexibility ...