/** * FormModel. * * @author Andrey Nilov <nilov@glavweb.ru> * @copyright Copyright (c) 2007-2012 Glavweb.Soft, Russia. (http://glavweb.ru) */ class FormModel extends CFormModel { /** * Config of the form * @var array */ protected $_formConfig = array(); /** * Config of the form by default * @var array */ private $_defaultFormConfig = array( 'method' => 'post' ); /** * Class name of form * @var string */ protected $_formClass = 'CForm'; /** * Object of Form * @var Form */ private $_form = null; /** * Constructor * * @param string $scenario Name of the scenario that this model is used in * @return void */ public function __construct($scenario = '') { parent::__construct($scenario); $this->_setFormConfig(); } /** * Sets config of the form * * @return void */ private function _setFormConfig() { $this->_formConfig = array_replace_recursive( $this->_defaultFormConfig, $this->_formConfig() ); } /** * Returns config of the form * * @return array */ protected function _formConfig() { return $this->_formConfig; } /** * Returns the attribute labels * * @return array Attribute labels (name=>label) */ public function attributeLabels() { return $this->getLabels(); } /** * Returns the config of the form * * @return array */ public function getFromConfig() { return $this->_formConfig; } /** * Sets the config of the form * * @param array $config * @return void */ public function setFromConfig(array $config) { $this->_formConfig = $config; } /** * Returns labels * * @return array */ public function getLabels() { $labels = array(); if (!empty($this->_formConfig['elements'])) { foreach ($this->_formConfig['elements'] as $name => $data) { if (isset($data['label'])) { $labels[$name] = $data['label']; } } } return $labels; } /** * Returns object of Form * * @return Form */ public function getForm() { if ($this->_form === null) { $this->_form = new $this->_formClass($this->_formConfig, $this); } return $this->_form; } }
/** * Config of the form * @var array */ protected $_formConfig = array( 'activeForm' => array( 'class' => 'CActiveForm', 'id' => 'registration_form', 'enableClientValidation' => true, 'clientOptions' => array( 'validateOnSubmit' => true ) ), 'elements' => array( .... 'name' => array( 'type' => 'text', 'label' => '' ), 'organization' => array( 'type' => 'text', 'label' => '' ), .... ), 'buttons' => array( 'register' => array( 'type' => 'submit', 'label' => '' ) ) );
$formModel = new RegistrationForm(); $form = $formModel->getForm(); echo $form;
$user = new User(); $user->setAttributes($formModel->getAttributes()); $result = $user->save();
protected $_formConfig = array( .... 'elements' => array( .... 'name' => array( 'type' => 'text', 'label' => '', 'layout' => "{input}\n{label}\n{hint}\n{error}" ), .... ),
/** * Automatically add a colon in time rendering * @var boolean */ public $autoAddColonForRender = true;
/** * Class name of form * @var string */ protected $_formClass = 'Form';
class Form extends CForm { /** * The name of the class for representing a form input element. * @var string */ public $inputElementClass = 'FormInputElement'; }
class FormInputElement extends CFormInputElement { /** * @var string the layout used to render label, input, hint and error. They correspond to the placeholders * "{label}", "{input}", "{hint}" and "{error}". */ public $layout = "{label}\n{input}\n{error}\n{hint}"; /** * Automatically add a colon in time rendering * @var boolean */ public $addColon = null; /** * Renders the label for this input. * * @return string */ public function renderLabel() { $model = $this->getParent()->getModel(); $addColon = $this->addColon !== null ? $this->addColon : $model instanceof FormModel && $model->autoAddColonForRender; $label = $addColon ? $this->getLabel() . ':' : $this->getLabel(); $options = array( 'label' => $label, 'required' => $this->getRequired() ); if (!empty($this->attributes['id'])) { $options['for'] = $this->attributes['id']; } return CHtml::activeLabel($this->getParent()->getModel(), $this->name, $options); } }
protected $_formConfig = array( .... 'elements' => array( .... 'name' => array( 'type' => 'text', 'label' => '', 'addColon' => true ), .... ),
Source: https://habr.com/ru/post/155127/
All Articles