kartik-v / yii2-widget-activeform

Enhanced Yii2 active-form and active-field with full bootstrap styling support (sub repo split from yii2-widgets).
http://demos.krajee.com/widget-details/active-form
Other
64 stars 35 forks source link

Option "formConfig" can't be changed dynamically #76

Closed Enrica-r closed 8 years ago

Enrica-r commented 8 years ago

During init of activeForm 'formConfig' is read and CSS are calculated once. LabelSpan and Size is stored in private variables like _labelCss or _inputCss. ActiveField reads this values during layout built.

In my form I have smaller and larger fields and sometimes two or one in a single row. I looked for a possibility to override the labelSpan value. As I didn't find an option to change the labelSpan value by calling the field method. So I had the idea to change value in 'formConfig' like

$form->formConfig = array_merge($form->formConfig, ['labelSpan' => 7]);

then call the $form->field method and change 'formConfig' back. Unfortunately there is no change.

Methods like activeForm->getInputCss() should recreate the CSS part on the fly by using getFormLayoutStyle() instead of reading a private variable.

Or do you see another possibility? Thank you.

kartik-v commented 8 years ago

Form config is set at ActiveForm initialization in the init method.

In my opinion you must use FormGrid builder for such variation scenarios.

Enrica-r commented 8 years ago

I know about the init method. Form builder has other disadvantages. I can't build my own widgets and integrate it. I prefer to design my own views manually.

The grid system of bootstrap divide the size of a column if i use nested grids. see example: example screen

That's why 'yii/bootstrap/ActiveForm' resp. 'yii/bootstrap/ActiveField' builds the layout wrapper in the field class. Wrapping options can be defined on level form and level field. The field options have more priority. But I prefer your form classes because all other functionality is great.

Anyway I have to find a solution even if I have to override your classes.

kartik-v commented 8 years ago

Am open for an enhancement. You can submit your ideas or share a PR as well. Let know.

Enrica-r commented 8 years ago

Hi Kartik As you proposed I have enhanced the code. I will do the PR next week. see Screen example-screen-corrected

Short explanation: I moved the initialization of labelCss, inputCss and offsetCss (getFormLayoutStyle) from ActiveForm to ActiveField because CSS of a field is a property of a field and not a form. The form config can be a frame which inherits a standard setting.

Now the definition of 'labelSpan' and 'deviceSize' is settable in fieldConfig and 'formConfig'. Plus 'formConfig' can be changed between 'ActiveForm::begin' and 'ActiveForm::end. This gives the possibility to use the setting for a group of fields. For an individual field the call is:

<?= $form->field($model, 'attributeName', ['labelSpan' => 2, 'deviceSize' => 'sm'])

Further I have compared your widgets with 'yii/bootstrap/ActiveForm' resp. 'ActiveField'. Your widget has more functionalities and it's much better. The only advantage of them is the enhanced template with label- and input wrappers plus the option of 'horizontalCssClasses' which gives a complete control for individual grid classes. Example 'col-sm-3 hidden-xs'. see http://www.yiiframework.com/doc-2.0/yii-bootstrap-activeform.html and http://www.yiiframework.com/doc-2.0/yii-bootstrap-activefield.html My idea is: I enhance the template like as original Yii widget and build a 'Yii widget' like compatibility in your widget combined with your 'labelSpan' and 'deviceSize'. If 'horizontalCssClasses' is set then the CSS generated by 'labelSpan' / 'deviceSize' will be overwritten. Shall I try this?

kartik-v commented 8 years ago

Sure you can go ahead with the enh. I have some thoughts on where it may break a few functionality. Will update on how to overcome those.

kartik-v commented 8 years ago

Some feedback:

  1. The existing configuration as defined in formConfig (e.g. labelSpan, deviceSize) can continue to exist to avoid breaking BC.
  2. The fieldConfig property can override the above configuration.
  3. Need to consider fix for #75 as well...

I suppose you have mentioned on the same lines (but just wished to add few cents). Also when you have a code, I can update over that.