mirror of
https://github.com/Oreolek/ifhub.club.git
synced 2024-06-01 07:38:11 +03:00
Merge pull request #330 from livestreet/new-struct
Черновой вариант конвертации правил валидации в js
This commit is contained in:
commit
c5d539f92c
|
@ -60,10 +60,11 @@
|
|||
|
||||
{* Заголовок топика *}
|
||||
{include file='forms/form.field.text.tpl'
|
||||
sFieldName = 'topic_title'
|
||||
sFieldRules = 'required="true" rangelength="[2,200]"'
|
||||
sFieldNote = $aLang.topic_create_title_notice
|
||||
sFieldLabel = $aLang.topic_create_title}
|
||||
sFieldName = 'topic_title'
|
||||
sFieldEntity = 'ModuleTopic_EntityTopic'
|
||||
sFieldEntityScenario = 'topic'
|
||||
sFieldNote = $aLang.topic_create_title_notice
|
||||
sFieldLabel = $aLang.topic_create_title}
|
||||
|
||||
|
||||
{block name='add_topic_form_text_before'}{/block}
|
||||
|
|
|
@ -12,6 +12,13 @@
|
|||
{* Правила валидации *}
|
||||
{$aFieldRules = " "|explode:$sFieldRules}
|
||||
|
||||
{if $sFieldEntity}
|
||||
{if !$sFieldEntityField}
|
||||
{$sFieldEntityField=$sFieldName}
|
||||
{/if}
|
||||
{field_make_rule entity=$sFieldEntity field=$sFieldEntityField scenario=$sFieldEntityScenario assign=aFieldRules}
|
||||
{/if}
|
||||
|
||||
{block name='field_before'}{/block}
|
||||
|
||||
<div class="form-field {if $bFieldInline}form-field-inline{/if} {block name='field_classes'}{/block} {if $bFieldNoMargin}m-0{/if}">
|
||||
|
|
|
@ -221,6 +221,14 @@ abstract class Entity extends LsObject {
|
|||
public function _getPrimaryKeyValue() {
|
||||
return $this->_getDataOne($this->_getPrimaryKey());
|
||||
}
|
||||
/**
|
||||
* Возвращает список правил для валидации
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function _getValidateRules() {
|
||||
return $this->aValidateRules;
|
||||
}
|
||||
/**
|
||||
* Выполняет валидацию данных сущности
|
||||
* Если $aFields=null, то выполняется валидация по всем полям из $this->aValidateRules, иначе по пересечению
|
||||
|
|
|
@ -197,5 +197,13 @@ abstract class ModuleValidate_EntityValidator extends Entity {
|
|||
call_user_func_array(array($this->oEntityCurrent,'set'.func_camelize($sField)),array($sValue));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Возвращает тип валидатора
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTypeValidator() {
|
||||
return func_underscore(str_ireplace('ModuleValidate_EntityValidator','',get_class($this)));
|
||||
}
|
||||
}
|
||||
?>
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
/*-------------------------------------------------------
|
||||
*
|
||||
* LiveStreet Engine Social Networking
|
||||
* Copyright © 2008 Mzhelskiy Maxim
|
||||
*
|
||||
*--------------------------------------------------------
|
||||
*
|
||||
* Official site: www.livestreet.ru
|
||||
* Contact e-mail: rus.engine@gmail.com
|
||||
*
|
||||
* GNU General Public License, version 2:
|
||||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*
|
||||
---------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Конвертирует PHP правила валидации в JS
|
||||
*
|
||||
* @param array $params
|
||||
* @param Smarty $smarty
|
||||
* @return string
|
||||
*/
|
||||
function smarty_function_field_make_rule($params, &$smarty) {
|
||||
$aParamsReq=array('entity','field');
|
||||
foreach($aParamsReq as $sParam) {
|
||||
if (!array_key_exists($sParam, $params)) {
|
||||
trigger_error("json: missing '{$sParam}' parameter",E_USER_WARNING);
|
||||
return;
|
||||
}
|
||||
}
|
||||
$aResult=array();
|
||||
$sScenario=isset($params['scenario']) ? $params['scenario'] : '';
|
||||
$sField=$params['field'];
|
||||
$oEntity=$params['entity'];
|
||||
if (!($oEntity instanceof Entity)) {
|
||||
$oEntity=Engine::GetEntity($oEntity);
|
||||
}
|
||||
|
||||
$aEntityRules=$oEntity->_getValidateRules();
|
||||
foreach($aEntityRules as $aEntityRule) {
|
||||
$oValidator=$oEntity->Validate_CreateValidator($aEntityRule[1],$oEntity,$aEntityRule[0],array_slice($aEntityRule,2));
|
||||
if (in_array($sField,$oValidator->fields) and (!$sScenario or isset($oValidator->on[$sScenario]))) {
|
||||
$sType=$oValidator->getTypeValidator();
|
||||
/**
|
||||
* Конвертация строкового валидатора
|
||||
*/
|
||||
if ($sType=='string') {
|
||||
$aResult[]='rangelength="['.$oValidator->min.','.$oValidator->max.']" ';
|
||||
if (!$oValidator->allowEmpty) {
|
||||
$aResult[]='required="true" ';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($params['assign'])) {
|
||||
$smarty->assign($params['assign'], $aResult);
|
||||
} else {
|
||||
return trim(join(' ',$aResult));
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in a new issue