Esempio n. 1
0
 /**
  * Get any auto rules base on table schema
  * @param array $rules
  * @param array $config
  * @param string $attribute
  * @return array
  */
 public function getAutoRules($rules, $config, $attribute = '')
 {
     static $columns = null;
     $data = [];
     switch ($attribute) {
         case 'id':
         case 'created_at':
         case 'createdAt':
         case 'modified_at':
         case 'modifiedAt':
             return $data;
             break;
     }
     $type = isset($config['active']['type']) ? $config['active']['type'] : '';
     if ($type == InputField::INPUT_STATIC) {
         return $data;
     }
     $columns = $columns === null ? self::getTableSchema()->columns : $columns;
     if (isset($columns[$attribute])) {
         $spec = isset($columns[$attribute]) ? $columns[$attribute] : false;
         $max = -1;
         $noTrim = false;
         $defaultOnEmpty = null;
         $isString = true;
         $isMultiple = false;
         $dateFormat = '';
         $addRules = [];
         $isInteger = false;
         switch ($spec->type) {
             case 'int':
             case 'integer':
             case 'tinyint':
             case 'smallint':
             case 'mediumint':
             case '__bigint':
                 // not included here as cast as will have been cast as a string
                 $isInteger = true;
                 break;
             default:
         }
         if ($type == '') {
             $type = InputField::getDefaultInputFieldType($attribute, $spec, isset($config['active']) ? $config['active'] : null);
             if ($type == InputField::INPUT_STATIC) {
                 return $data;
             }
         }
         switch ($type) {
             case InputField::INPUT_TEXT:
             case InputField::INPUT_PASSWORD:
                 $max = isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size;
                 break;
             case InputField::INPUT_PASSWORD_STRENGTH:
                 //wlchere - alternative or implement correctly again
                 //$max = (isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size);
                 //$addRules[] = ['check' => [StrengthValidator::className()], 'rule' => [StrengthValidator::className(), 'preset' => 'normal', 'hasUser' => false, 'hasEmail' => false, 'userAttribute' => false]];
                 break;
             case InputField::INPUT_INTEGER:
                 if ($spec) {
                     $max = isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size;
                     $unsigned = $spec->unsigned;
                 } else {
                     $max = $config['active']['options']['maxlength'];
                     $unsigned = !isset($config['active']['options']['data-inputmask']['allowMinus']) || !$config['active']['options']['data-inputmask']['allowMinus'] ? true : false;
                 }
                 $maxValue = pow(10, $max) - 1;
                 $minValue = $unsigned ? 0 : -1 * $maxValue;
                 $isString = false;
                 $defaultOnEmpty = 0;
                 if ($maxValue > 2147483646) {
                     // bigint for example is cast as a string
                 } else {
                     $addRules[] = ['check' => [FilterValidator::className(), 'filter' => 'intval'], 'rule' => [FilterValidator::className(), 'filter' => 'intval']];
                 }
                 $addRules[] = ['check' => ['integer'], 'rule' => ['integer', 'max' => $maxValue, 'min' => $minValue]];
                 if (!$unsigned) {
                     $max++;
                 }
                 break;
             case InputField::INPUT_DECIMAL:
                 if ($spec) {
                     $max = isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size + 1;
                     // allow for decimal point
                     $unsigned = $spec->unsigned;
                     $scale = $spec->scale;
                 } else {
                     $max = $config['active']['options']['maxlength'];
                     $unsigned = !isset($config['active']['options']['data-inputmask']['allowMinus']) || !$config['active']['options']['data-inputmask']['allowMinus'] ? true : false;
                     $scale = isset($config['active']['options']['data-inputmask']['digits']) ? $config['active']['options']['data-inputmask']['digits'] : 2;
                 }
                 $maxValue = pow(10, $max - 1) - 0.01;
                 $minValue = $unsigned ? 0 : -1 * $maxValue;
                 $noTrim = true;
                 $defaultOnEmpty = '0.00';
                 $addRules[] = ['check' => ['double'], 'rule' => ['double', 'max' => $maxValue, 'min' => $minValue]];
                 $addRules[] = ['check' => [FilterValidator::className()], 'rule' => [FilterValidator::className(), 'filter' => 'number_format', 'args' => [$scale, '.', '']]];
                 if (!$unsigned) {
                     $max++;
                 }
                 break;
             case InputField::INPUT_TEXTAREA:
                 $max = isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size;
                 break;
             case InputField::INPUT_CHECKBOX:
             case InputField::INPUT_CHECKBOX_BASIC:
             case InputField::INPUT_CHECKBOX_SWITCH:
             case InputField::INPUT_CHECKBOX_ICHECK:
                 $isString = false;
                 $addRules[] = ['check' => ['boolean'], 'rule' => ['boolean']];
                 $defaultOnEmpty = 0;
                 if ($spec && $isInteger) {
                     if ($spec->size == 1) {
                         $addRules[] = ['check' => [FilterValidator::className(), 'filter' => 'intval'], 'rule' => [FilterValidator::className(), 'filter' => 'intval']];
                     }
                 }
                 break;
             case InputField::INPUT_DATE:
                 $defaultOnEmpty = Tools::DATE_DB_EMPTY;
                 $max = 10;
                 $dateFormat = 'Y-m-d';
                 break;
             case InputField::INPUT_DATETIME:
                 $defaultOnEmpty = Tools::DATE_TIME_DB_EMPTY;
                 $max = 19;
                 $dateFormat = 'Y-m-d H:i:s';
                 break;
             case InputField::INPUT_TIME:
                 $defaultOnEmpty = Tools::TIME_DB_EMPTY;
                 $max = 8;
                 $dateFormat = 'H:i:s';
                 break;
             case InputField::INPUT_YEAR:
                 $defaultOnEmpty = Tools::YEAR_DB_EMPTY;
                 $max = 4;
                 $dateFormat = 'Y';
                 $addRules[] = ['check' => ['integer'], 'rule' => ['integer', 'max' => Tools::YEAR_DB_MAX, 'min' => Tools::YEAR_DB_EMPTY]];
                 break;
             case InputField::INPUT_COLOR:
                 $max = !$spec || $spec->size > 21 ? 21 : $spec->size;
                 break;
             case InputField::INPUT_MINI_COLORS:
                 $max = !$spec || $spec->size > 25 ? 25 : $spec->size;
                 break;
             case InputField::INPUT_SELECT_PICKER_MULTI:
             case InputField::INPUT_CHECKBOX_LIST:
             case InputField::INPUT_CHECKBOX_LIST_ICHECK:
             case InputField::INPUT_MULTISELECT:
             case InputField::INPUT_SELECT2_MULTI:
                 $isMultiple = true;
                 break;
             case InputField::INPUT_SELECT2:
             case InputField::INPUT_DROPDOWN_LIST:
             case InputField::INPUT_LIST_BOX:
             case InputField::INPUT_SELECT_PICKER:
             case InputField::INPUT_SELECT_SPLITTER:
                 if (isset($config['active']['options']['multiple']) && $config['active']['options']['multiple']) {
                     $isMultiple = true;
                 } else {
                     if ($spec && $isInteger) {
                         $defaultOnEmpty = '0';
                         $addRules[] = ['check' => [FilterValidator::className(), 'filter' => 'intval'], 'rule' => [FilterValidator::className(), 'filter' => 'intval']];
                     }
                 }
                 break;
             case InputField::INPUT_SELECT2_TAGS:
                 // received as a pipe delimited string
                 $max = isset($config['active']['options']['maxlength']) ? $config['active']['options']['maxlength'] : $spec->size;
                 break;
             case InputField::INPUT_RADIO_LIST:
             case InputField::INPUT_RADIO_LIST_ICHECK:
             case InputField::INPUT_RADIO:
                 break;
             case InputField::INPUT_EDITOR_CK:
             case InputField::INPUT_EDITOR_BS_WYSIHTML5:
             case InputField::INPUT_EDITOR_BS_SUMMERNOTE:
                 break;
             default:
                 break;
         }
         if ($isMultiple) {
             // apply default conversion of multiple selections array into pipe delimited string
             $addRules[] = ['check' => [FilterValidator::className()], 'rule' => [FilterValidator::className(), 'filter' => 'implode', 'makeArray' => true, 'argsFirst' => true, 'args' => ['|']]];
         }
         if ($defaultOnEmpty !== null) {
             if (!$this->checkHasRule($rules, $attribute, 'default', 'value')) {
                 $data[] = ['default', 'value' => $defaultOnEmpty];
             }
         }
         if ($isString && $max != -1) {
             if (!$this->checkHasRule($rules, $attribute, 'string', 'max')) {
                 if (isset($config['active']['options']['maxlength'])) {
                     $max = $config['active']['options']['maxlength'];
                 }
                 $data[] = ['string', 'max' => $max ? intval($max) : $spec->size];
             }
             if (!$noTrim && !$this->checkHasRule($rules, $attribute, 'filter', 'filter', 'trim')) {
                 $data[] = ['filter', 'filter' => 'trim'];
             }
         }
         if ($dateFormat != '') {
             if (!$this->checkHasRule($rules, $attribute, 'date')) {
                 $data[] = [DateValidator::className(), 'format' => 'php:' . $dateFormat];
             }
         }
         if ($addRules) {
             foreach ($addRules as $rule) {
                 $skipRule = false;
                 if (isset($rule['check']) && $rule['check']) {
                     if (is_array($rule['check']) && $this->checkHasRule($rules, $attribute, $rule['check'][0], isset($rule['check'][1]) ? $rule['check'][1] : '', isset($rule['check'][2]) ? $rule['check'][2] : null)) {
                         $skipRule = true;
                     } elseif ($this->checkHasRule($rules, $attribute, $rule['check'])) {
                         $skipRule = true;
                     }
                 }
                 if (!$skipRule) {
                     $data[] = isset($rule['rule']) ? $rule['rule'] : $rule;
                 }
             }
         }
     }
     return $data;
 }
Esempio n. 2
0
 /**
  * Amend options array suitable for passing to widgets
  *
  * @param string $type
  * @param array $options
  * @return array
  */
 public function convertOptionsForWidgets($type, $options)
 {
     if (InputField::getIsWidgetFromFieldType($type)) {
         // we need to present the options differently
         $newOptions = $options;
         $widgetOptions = ArrayHelper::getValue($options, 'widgetOptions', []);
         unset($newOptions['widgetOptions']);
         $options = $widgetOptions;
         $options['options'] = $newOptions;
     }
     return $options;
 }