Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // виджет
- class CActiveFormWrapper extends CWidget {
- public $defaultFieldClass = 'Text';
- public $fields = null;
- public $form = null;
- public $formOptions = array();
- public $htmlOptions = array('class'=>'form');
- public $model = null;
- public function run() {
- $this->begin();
- $this->fields();
- $this->end():
- }
- public function begin() {
- echo CHtml::openTag('div', $this->htmlOptions);
- $this->form = $this->beginWidget('CActiveForm', $this->formOptions);
- }
- public function end() {
- $this->endWidget();
- echo CHtml::closeTag('div');
- }
- public function fields() {
- // если поля не указаны явно, будем показывать все подряд поля модели
- if (is_null($this->fields)) {
- $this->fields = array_keys($this->model->getAttributes());
- }
- // циклично выводим поля
- foreach ($this->fields as $k=>$v) {
- $options = (is_array($v))
- ? $v
- : array('attribute'=>$v);
- $this->field($options);
- }
- }
- public function field($options) {
- $class = (!empty($options['class']))
- ? $options['class']
- : $this->defaultFieldClass;
- $class = ucfirst($class).'Field';
- $field = new $class($this);
- foreach ($options as $k=>$v) {
- $field->$k => $v;
- }
- $field->init();
- $field->run();
- }
- }
- // абстрактное поле
- // в интерфейсе IField такие методы: init(), run(), element()
- abstract class Field extends CComponent implements IField {
- public $attribute = '';
- public $class = '';
- public $htmlOptions = array();
- public $rowHtmlOptions = array('class'=>'row');
- public $owner = null;
- public function __construct($owner) {
- $this->owner = $owner;
- }
- public function init() {
- }
- public function run() {
- $this->begin();
- $this->label();
- $this->element();
- $this->error();
- $this->end();
- }
- public function begin() {
- echo CHtml::openTag('div', $this->rowHtmlOptions);
- }
- public function end() {
- echo CHtml::closeTag('div');
- }
- public function label() {
- echo $this->owner->form->labelEx($this->model, $this->attribute);
- }
- public function error() {
- echo $this->owner->form->error($this->model, $this->attribute);
- }
- public function getModel() {
- return $this->owner->model;
- }
- }
- // текстовое, например, поле
- class TextField extends Field {
- public function element() {
- echo $this->owner->form->textField($this->model, $this->attribute,
- $this->htmlOptions);
- }
- }
- // textarea, допустим
- class TextAreaField extends Field {
- public function element() {
- echo $this->owner->form->textArea($this->model, $this->attribute,
- $this->htmlOptions);
- }
- }
- // Ну и примеры вызова всей этой хуйни из вьюшки:
- $model = new Post(); // id, title, content, ctime, utime
- // Тупо модель, без параметров - выведутся все поля, включая ID, ctime, utime, нехорошо
- $this->widget('ext.CActiveFormWrapper', array(
- 'model'=>$model,
- ));
- // То же самое, но с AJAX валидацией
- $this->widget('ext.CActiveFormWrapper', array(
- 'model'=>$model,
- 'formOptions'=>array(
- 'enableAjaxValidation'=>true,
- ),
- ));
- // Показать только title и content, причем content - textarea/
- $this->widget('ext.CActiveFormWrapper', array(
- 'model'=>$model,
- 'fields'=>array(
- 'title',
- array(
- 'attribute'=>'content',
- 'class'=>'TextArea',
- ),
- ),
- ));
Add Comment
Please, Sign In to add comment