2017 © Pedro Peláez
 

yii2-extension yii2-knockout

ActiveForm replacement. Automatically generates knockoutjs viewmodels from yii ActiveRecords. Allows computed fields and nested forms.

image

e-frank/yii2-knockout

ActiveForm replacement. Automatically generates knockoutjs viewmodels from yii ActiveRecords. Allows computed fields and nested forms.

  • Monday, June 25, 2018
  • by eFrank
  • Repository
  • 3 Watchers
  • 5 Stars
  • 235 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 2 Open issues
  • 4 Versions
  • 7 % Grown

The README.md

Knockout Viewmodel Generator for Yii2

This extension provides a replacement for ActiveForm. A KnockoutJS viewmodel is automatically generated (using knockout mapping lib), which serves a starting point for your customization, e.g. computed observables. Moreover you get some more input types, like "decimal", "percent", "date" and "datetime", which display the raw data in the user's locale format and store the data in ISO-format., (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist e-frank/yii2-knockout "*"

or add, (*4)

"e-frank/yii2-knockout": "*"

to the require section of your composer.json file., (*5)

Usage

example code for a view. note that you would place model load and validation code in your controller/action., (*6)

title  = 'Programmatic Viewmodel Generation';
    
// init data from default or post
$post = Yii::$app->request->post();
$data = empty($post) ? [
    'title' => '', 
    'value' => 1234.56, 
    'date'  => sprintf('%s%d:00', date('Y-m-d H:'), date('i') - (date('i') % 15))
    ] : $post['DynamicModel'] ;

// create a dynamic model, for demonstration purpose
$model = new yii\base\DynamicModel($data);
$model->addRule(['title'], 'string', ['max' => 128]);
$model->addRule(['title'], 'required');
$model->addRule(['value'], 'number', ['min' => 0]);

// validate, usually done in controller
if (!empty($post)) {
    $model->validate();
}

// show what we have got
var_dump([
    'attributes' => $model->attributes,
    'errors'     => $model->getErrors(),
    'post'       => $post,
    ]);

?>

 $model->getErrors(), 'defaults' => ['decimals' => 3]]); ?>


= $this->title ?>
= $form->field($model, 'title') ?> field($model, 'value')->decimal(['decimal' => ['thousandsSeparator' => ' ']]) ?> = $form->field($model, 'value')->decimal() ?> computed raw data: computed user's display format:
= $form->field($model, 'date')->datetime() ?>

custom viewModel enhancements, located at the end of your view, after all other scripts are loaded:, (*7)



the demo page should look like this:, (*8)

Screen1, (*9)

The Versions

25/06 2018

dev-master

9999999-dev

ActiveForm replacement. Automatically generates knockoutjs viewmodels from yii ActiveRecords. Allows computed fields and nested forms.

  Sources   Download

BSD-3-Clause

The Requires

 

yii knockout

24/06 2018

v1.1.2

1.1.2.0

ActiveForm replacement. Automatically generates knockoutjs viewmodels from yii ActiveRecords. Allows computed fields and nested forms.

  Sources   Download

BSD-3-Clause

The Requires

 

yii knockout

12/06 2018

v1.1.1

1.1.1.0

ActiveForm replacement. Automatically generates knockoutjs viewmodels from yii ActiveRecords. Allows computed fields and nested forms.

  Sources   Download

BSD-3-Clause

The Requires

 

yii knockout

11/06 2018

1.1.0

1.1.0.0

ActiveForm replacement. Automatically generates knockoutjs viewmodels from yii ActiveRecords. Allows computed fields and nested forms.

  Sources   Download

BSD-3-Clause

The Requires

 

yii knockout