measuring-unit-select Form input widget to select measuring units and convert to base unit at server-side

Measuring Unit Select

  1. Information
  2. Description
  3. Requirements
  4. Installation
  5. Usage

Information

Description

Simple extension for Yii framework, providing server-side conversion of specified form fields values, given in different measuring units. Widget looks like text field followed by dropdown menu with list of measuring units. Conversion itself will be done using CModel behavior after submitiing form.

Requirements

  • PHP 5.4.x+
  • Yii framework 1.1.x (Tested under 1.1.13)

Installation

Put files into your directory under application extensions directory (For example into protected/extension/MeasuringUnitSelect folder)

Usage

Add behavior configuration to you model behaviors method:

class MyRecord extends \CActiveRecord {
			...
			public function behaviors() {
				return [
					...
					'WalletUnits' => [
						'class' => '\\MeasuringUnitSelect\\MeasuringUnitSelectActiveRecordBehavior',
						'attributes' => ['price'],
						'baseUnit' => 'rub',
						'unitFactors' => ['rub' => 1, 'usd' => function($unit, $attribute) {return 31.5;}],
						'unitLabels' => [
							'rub' => \Yii::app()->locale->getCurrencySymbol('RUB'),
							'usd' => \Yii::app()->locale->getCurrencySymbol('USD'),
						],
					],
					...
				];
			}
			...
		}

If you using CForm, add following configuration to your form config:

return [
			...
			'price' => [
				'type' => '\\MeasuringUnitSelect\\MeasuringUnitSelectInputWidget',
				'unitLabels' => [
					'rub' => \Yii::app()->locale->getCurrencySymbol('RUB'),
					'usd' => \Yii::app()->locale->getCurrencySymbol('USD'),
				],
				'baseUnit' => 'rub',
			],
			...
		];

If you're don't using form builder, add to your view file code that draws input widget, it may be something like that (I dont't tested that code at all):

$this->beginForm();
		...
		echo $this->widget(
			'\\MeasuringUnitSelect\\MeasuringUnitSelectInputWidget',
			[
				'model' => $model,
				'attribute' => 'price',
				'unitLabels' => [
		            'rub' => \Yii::app()->locale->getCurrencySymbol('RUB'),
		            'usd' => \Yii::app()->locale->getCurrencySymbol('USD'),
		        ],
		        'baseUnit' => 'rub',
			]
		);
		...
		$this->endForm();
1 0
1 follower
275 downloads
Yii Version: Unknown
License: MIT
Category: Others
Developed by: migel
Created on: Jun 25, 2013
Last updated: 11 years ago

Downloads

show all

Related Extensions