public static function formValidator($formObj, $errBox = "sbl_errmsg") { $include = <<<INC <script type="text/javascript" src="%s" charset="UTF-8"></script> INC; $model = $formObj->getModel(); $columns = $model->getColumns(); $errMsgs = Sabel_Db_Validate_Config::getMessages(); $lNames = Sabel_Db_Model_Localize::getColumnNames($model->getName()); $data = array("data" => array(), "errors" => $errMsgs); foreach ($columns as $c) { $name = $c->name; if (isset($lNames[$c->name])) { $c->name = $lNames[$c->name]; } $data["data"][$name] = array_change_key_case((array) $c, CASE_UPPER); } $buf = array(); $buf[] = sprintf($include, linkto("js/helpers/FormValidator.js")); $buf[] = "\n"; $buf[] = '<script type="text/javascript">'; $buf[] = 'new Sabel.PHP.FormValidator(' . json_encode($data) . ');'; $buf[] = '</script>'; return join($buf, "") . "\n"; }
protected function doCustomValidate(Sabel_Db_Validate_Config $config, $columns, $mdlName = null) { if ($mdlName === null) { $mdlName = $this->mdlName; } if ($config->has($mdlName)) { $columnConfigs = $config->get($mdlName)->getColumns(); foreach ($columnConfigs as $key => $columnConfig) { if (strpos($key, "*") !== false) { $regex = "^" . str_replace("*", ".*", $key); foreach (array_keys($columns) as $colName) { if (preg_match("/{$regex}/", $colName)) { foreach ($columnConfig->getValidators() as $validator) { $this->_doCustomValidate($config, $colName, $validator); } } } } elseif (isset($columns[$key])) { foreach ($columnConfig->getValidators() as $validator) { $this->_doCustomValidate($config, $key, $validator); } } } } $parent = get_parent_class($mdlName); if ($parent !== false && $parent !== "Sabel_Db_Model") { $this->doCustomValidate($config, $columns, $parent); } }
<?php class Config_Database implements Sabel_Config { public function configure() { $params = array("default" => array("package" => "sabel.db.mysql", "charset" => "utf8", "host" => XOOPS_DB_HOST, "port" => 3306, "database" => XOOPS_DB_NAME, "user" => XOOPS_DB_USER, "password" => XOOPS_DB_PASS)); return $params; } } Sabel_Db_Validate_Config::setMessages(array("maxlength" => "%NAME%は%MAX%文字以下で入力してください", "minlength" => "%NAME%は%MIN%文字以上で入力してください", "maximum" => "%NAME%は%MAX%以上を入力してください", "minimum" => "%NAME%は%MIN%以下を入力してください", "nullable" => "%NAME%を入力してください", "numeric" => "%NAME%は数値で入力してください", "type" => "%NAME%の形式が不正です", "unique" => "%NAME%'%VALUE%'は既に使用されています"));
/** * @param array $messages * * @return void */ public static function setMessages(array $messages) { self::$messages = $messages; }