Forms ===== Forms allows to handle user data. Forms contains such features: * Fieldsets. * Conditions. * Elements and containers based (OOP objects). * Fast methods for elements. * Views as partial (separated logic for elements, containers, etc). * Multiply entities support. * Validation according to form validation and entities validation. * Simple entity form. * Text form. * File form. Form example: .. code-block:: php * @copyright 2013-2014 PhalconEye Team * @license New BSD License * @link http://phalconeye.com/ */ class Performance extends CoreForm { /** * Initialize form. * * @return void */ public function initialize() { $this->setTitle('Performance settings'); $this->addContentFieldSet() ->addText('prefix', 'Cache prefix', 'Example: "pe_"', 'pe_') ->addText( 'lifetime', 'Cache lifetime', 'This determines how long the system will keep cached data before reloading it from the database server. A shorter cache lifetime causes greater database server CPU usage, however the data will be more current.', 86400 ) ->addSelect( 'adapter', 'Cache adapter', 'Cache type. Where cache will be stored.', [ 0 => 'File', 1 => 'Memcached', 2 => 'APC', 3 => 'Mongo' ], 0 ) /** * File options */ ->addText('cacheDir', 'Files location', null, ROOT_PATH . '/app/var/cache/data/') /** * Memcached options. */ ->addText('host', 'Memcached host', null, '127.0.0.1') ->addText('port', 'Memcached port', null, '11211') ->addCheckbox('persistent', 'Create a persistent connection to memcached?', null, 1, true, 0) /** * Mongo options. */ ->addText( 'server', 'A MongoDB connection string', null, 'mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]]' ) ->addText('db', 'Mongo database name', null, 'database') ->addText('collection', 'Mongo collection in the database', null, 'collection') /** * Other. */ ->addCheckbox('clear_cache', 'Clear cache', 'All system cache will be cleaned.', 1, false, 0); $this->addFooterFieldSet()->addButton('save'); $this->addFilter('lifetime', self::FILTER_INT); $this->_setConditions(); } /** * Validates the form. * * @param array $data Data to validate. * @param bool $skipEntityCreation Skip entity creation. * * @return boolean */ public function isValid($data = null, $skipEntityCreation = false) { if (!$data) { $data = $this->getDI()->getRequest()->getPost(); } if (isset($data['adapter']) && $data['adapter'] == '0') { if (empty($data['cacheDir']) || !is_dir($data['cacheDir'])) { $this->addError('Files location isn\'t correct!'); return false; } } return parent::isValid($data, $skipEntityCreation); } /** * Set form conditions. * * @return void */ protected function _setConditions() { $content = $this->getFieldSet(self::FIELDSET_CONTENT); /** * Files conditions. */ $content->setCondition('cacheDir', 'adapter', 0); /** * Memcached conditions. */ $content->setCondition('host', 'adapter', 1); $content->setCondition('port', 'adapter', 1); $content->setCondition('persistent', 'adapter', 1); /** * Mongo conditions. */ $content->setCondition('server', 'adapter', 3); $content->setCondition('db', 'adapter', 3); $content->setCondition('collection', 'adapter', 3); } } Structure --------- Root form class is abstract. So you can't create it directly. Also it has abstract methods, that identifies form rendering feature. That's why there is some simple form structure: .. code-block:: text AbstractForm | CoreForm EntityForm (trait) | |------- FileForm | |------- TextForm Core form implements all necessary methods: .. code-block:: php * @copyright 2013-2014 PhalconEye Team * @license New BSD License * @link http://phalconeye.com/ */ class CoreForm extends AbstractForm { const /** * Default layout path. */ LAYOUT_DEFAULT_PATH = 'partials/form/default'; use EntityForm; /** * Get layout view path. * * @return string */ public function getLayoutView() { return $this->_resolveView(self::LAYOUT_DEFAULT_PATH); } /** * Get element view path. * * @return string */ public function getElementView() { return $this->getLayoutView() . '/element'; } /** * Get errors view path. * * @return string */ public function getErrorsView() { return $this->getLayoutView() . '/errors'; } /** * Get notices view path. * * @return string */ public function getNoticesView() { return $this->getLayoutView() . '/notices'; } /** * Get fieldset view path. * * @return string */ public function getFieldSetView() { return $this->getLayoutView() . '/fieldSet'; } /** * Resolve view. * * @param string $view View path. * @param string $module Module name (capitalized). * * @return string */ protected function _resolveView($view, $module = 'Core') { return '../../' . $module . '/View/' . $view; } } Text and file form extended from it and used for text rendering and file uploading features respectively. Entity trait used for forms that must be created according to some entity. Read more about each form type below. Elements -------- Elements are objects and form/fieldset is a container for these objects. So you can add element to form by creating it and adding: .. code-block:: php add($el, 1001); But this is a bit hard. So, there are exists some methods for element creation: * addHtml * addButton * addButtonLink * addText * addTextArea * addCkEditor * addPassword * addHidden * addHeading * addFile * addRemoteFile * addCheckbox * addRadio * addMultiCheckbox * addSelect * addMultiSelect Default options of elements (not all allowed, and this is not a complete list, options can be added manually by element): +--------------+----------------------------------------------------------------------------------------------------------+ | Name | Description | +==============+==========================================================================================================+ | label | Label content for element | +--------------+----------------------------------------------------------------------------------------------------------+ | description | Description text for element | +--------------+----------------------------------------------------------------------------------------------------------+ | required | Mark element as required (you can't submit form without data for this element). | +--------------+----------------------------------------------------------------------------------------------------------+ | emptyAllowed | Mark element as required with non empty value (you can't submit form with empty string for this element).| +--------------+----------------------------------------------------------------------------------------------------------+ | ignore | Ignore element in validation and values, ignores it at backend, it will be skipped. Example: buttons. | +--------------+----------------------------------------------------------------------------------------------------------+ | htmlTemplate | Html template for element. | +--------------+----------------------------------------------------------------------------------------------------------+ | defaultValue | Default value of element. Example: checkbox, user set (un)checked state, but default value is '1'. | +--------------+----------------------------------------------------------------------------------------------------------+ Non-default options: +---------------+-----------------+------------+---------------------------------------------------------------------------------------------------------------+ | Element Name | Option Name | Type |Description | +===============+=================+============+===============================================================================================================+ | Button | isSubmit | Boolean | Flag, that adds submit feature to the button. If it 'false' - button will not be able to submit the form. | +---------------+-----------------+------------+---------------------------------------------------------------------------------------------------------------+ | Checkbox | checked | Mixed | If something is set to this option (true, 'checked', etc) an additional attribute checked="checked" will be | | | | | added. If it is null, nothing will be added. | +---------------+-----------------+------------+---------------------------------------------------------------------------------------------------------------+ | CkEditor | elementOptions | Array | Array of options for CkEditor control. | +---------------+-----------------+------------+---------------------------------------------------------------------------------------------------------------+ | File | isImage | Boolean | Flag for image, if it is true, control will add additional url checks for value of this control. | | | | | Also some additional check can be applied for this control, marked as image. | +---------------+-----------------+------------+---------------------------------------------------------------------------------------------------------------+ | Heading | tag | String | Tag of heading. By default: 'h2'. | +---------------+-----------------+------------+---------------------------------------------------------------------------------------------------------------+ | MultiCheckbox,| elementOptions | Array | Associated array of key=>value, data for control. | | Radio, | | | | | Select | | | | | +-----------------+------------+---------------------------------------------------------------------------------------------------------------+ | | disabledOptions | Array | Array of keys that must be marked, as disabled (css attr). | | +-----------------+------------+---------------------------------------------------------------------------------------------------------------+ | | using | Resultset | Phalcon ResultSet, Model::findAll(). | +---------------+-----------------+------------+---------------------------------------------------------------------------------------------------------------+ | Select | hasEmptyValue | Boolean | Flag for empty value by default, this is empty select option. | +---------------+-----------------+------------+---------------------------------------------------------------------------------------------------------------+ List of all elements, their options and attributes: +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | Name | Description | Allowed Options | Default Options | Default Attributes | +==============+=====================================+==========================+==========================+==========================================================+ | Button | Button element. | 'htmlTemplate', 'label', | 'isSubmit' => true | * 'id' => $this->getName() | | | | 'isSubmit' | | * 'name' => $this->getName() | | | | | | * 'required' => true/false | | | | | | | | | | | | If 'isSubmit' == true | | | | | | | | | | | | * 'type' => 'submit' | | | | | | * 'class' => 'btn btn-primary' | | | | | | | | | | | | else | | | | | | | | | | | | * 'class' => 'btn' | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | ButtonLink | Button as link, e.g.: back link. | 'htmlTemplate', 'label' | --- | * 'id' => $this->getName() | | | | | | * 'name' => $this->getName() | | | | | | * 'required' => true/false | | | | | | * 'class' => 'btn form_link_button' | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | Checkbox | Html input of type "checkbox". | all default options, | --- | * 'id' => $this->getName() | | | | 'checked' | | * 'name' => $this->getName() | | | | | | * 'required' => true/false | | | | | | * 'class' => '' | | | | | | * 'type' => 'checkbox' | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | CkEditor | CkEditor control. | all default options, | --- | * 'id' => $this->getName() | | | | 'elementOptions' | | * 'name' => $this->getName() | | | | | | * 'required' => true/false | | | | | | * 'class' => 'form-control' | | | | | | * 'data-widget' => 'ckeditor' | | | | | | * 'data-name' => $this->getName() | | | | | | * 'data-options' => 'elementOptions' option | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | File | Html input of type "file". | all default options, | --- | * 'id' => $this->getName() | | | | 'isImage' | | * 'name' => $this->getName() | | | | | | * 'required' => true/false | | | | | | * 'class' => 'form-control' | | | | | | * 'type' => 'file' | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | Heading | Simple text output as heading. | 'htmlTemplate', 'tag' | 'tag' => 'h2' | * 'id' => $this->getName() | | | | | | * 'name' => $this->getName() | | | | | | * 'required' => true/false | | | | | | * 'class' => 'form_element_heading' | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | Hidden | Html input of type "hidden". | all default options. | --- | * 'id' => $this->getName() | | | | | | * 'name' => $this->getName() | | | | | | * 'required' => true/false | | | | | | * 'class' => 'form-control' | | | | | | * 'type' => 'hidden' | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | Html | Render raw html. It's value is html.| --- | --- | --- | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | MultiCheckbox| Multi checkbox control. | all default options, | --- | * 'id' => $this->getName() | | | | 'elementOptions', | | * 'name' => $this->getName() | | | | 'disabledOptions', | | * 'required' => true/false | | | | 'using' | | * 'class' => '' | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | Password | Html input of type "password". | all default options. | --- | * 'id' => $this->getName() | | | | | | * 'name' => $this->getName() | | | | | | * 'required' => true/false | | | | | | * 'class' => 'form-control' | | | | | | * 'type' => 'password' | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | Radio | Radiobox control. | all default options, | --- | * 'id' => $this->getName() | | | | 'elementOptions', | | * 'name' => $this->getName() | | | | 'disabledOptions', | | * 'required' => true/false | | | | 'using' | | * 'class' => '' | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | RemoteFile | File, located at file storage, | all default options, | --- | --- | | | opens modal dialog in Pydio for | 'buttonTitle' | | | | | file selection. Select url to file. | | | | +--------------+-------------------------------------+--------------------------+--------------------------+----------------------------------------------------------+ | Select | Html