Пример #1
0
 public function test()
 {
     $column = new Param('name');
     $column->required(1);
     ok($column->required);
     $column->default('John');
     is('John', $column->default);
 }
 public function testText()
 {
     $column = new Param('account');
     $column->required(1);
     $field = new BootstrapFieldView($column, array('labelClass' => 'col-lg-2', 'inputWrapperClass' => 'col-lg-10'));
     $field->setWidgetAttributes(array('placeholder' => "ariel123", 'readoly' => "", 'autocomplete' => "off"));
     $html = $field->render();
     $xml = simplexml_load_string($html);
     is('form-group formkit-widget-textinput', (string) $xml->attributes()['class']);
     $label = $xml->label;
     is('col-lg-2', (string) $label->attributes()['class']);
     is('* Account', (string) $label[0]);
     $div = $xml->div;
     is('col-lg-10', (string) $div->attributes()['class']);
     $input = $div->input->attributes();
     is('formkit-widget formkit-widget-text form-control', $input->class);
     is('account', $input->name);
     is('ariel123', $input->placeholder);
     is('off', $input->autocomplete);
 }
Пример #3
0
 public function testText()
 {
     $column = new Param('name');
     $column->required(1);
     $column->default('John');
     $field = new DivFieldView($column);
     $html = $field->render();
     $xml = simplexml_load_string($html);
     is('v-field formkit-widget-textinput', (string) $xml->attributes()['class']);
     $div = $xml->div[0];
     is('label', (string) $div->attributes()['class']);
     is('* Name', (string) $div->label);
     $label = $div->label;
     is('formkit-widget formkit-label formkit-widget-label', (string) $label->attributes()['class']);
     $div = $xml->div[1];
     is('input', (string) $div->attributes()['class']);
     $input = $div->input->attributes();
     is('formkit-widget formkit-widget-text', $input->class);
     is('name', $input->name);
     is('text', $input->type);
     is('John', $input->value);
 }
Пример #4
0
 public function validate($value)
 {
     $ret = (array) parent::validate($value);
     if ($ret[0] == false) {
         return $ret;
     }
     // Consider required and optional situations.
     if ($fileArg = $this->action->request->file($this->name)) {
         $file = UploadedFile::createFromArray($fileArg);
         // If valid extensions are specified, pass to uploaded file to check the extension
         if ($this->validExtensions) {
             if (!$file->validateExtension($this->validExtensions)) {
                 // return array(false, __('Invalid File Extension: %1' . $this->name ) );
                 return array(false, $this->action->messagePool->translate('Invalid File Extension: %1'), $this->name);
             }
         }
         if ($this->sizeLimit) {
             if (!$file->validateSize($this->sizeLimit)) {
                 return array(false, $this->action->messagePool->translate("The uploaded file exceeds the size limitation. %1 KB ", futil_prettysize($this->sizeLimit)));
             }
         }
     }
     return true;
 }
Пример #5
0
 public function validate($value)
 {
     $ret = (array) parent::validate($value);
     if (false === $ret[0]) {
         return $ret;
     }
     $requireUploadMove = false;
     $uploadedFile = $this->_findUploadedFile($this->name, $requireUploadMove);
     if ($uploadedFile && $uploadedFile->hasError()) {
         return [false, $uploadedFile->getUserErrorMessage()];
     }
     $file = $this->action->request->file($this->name);
     if (!empty($file) && $file['name'] && $file['type']) {
         $uploadedFile = UploadedFile::createFromArray($file);
         if ($this->validExtensions) {
             if (!$uploadedFile->validateExtension($this->validExtensions)) {
                 return [false, _('Invalid file extension: ') . $uploadedFile->getExtension()];
             }
         }
         if ($this->sizeLimit) {
             if (!$uploadedFile->validateSize($this->sizeLimit)) {
                 return [false, _("The uploaded file exceeds the size limitation. ") . futil_prettysize($this->sizeLimit * 1024)];
             }
         }
     } else {
         if ($this->required) {
             return [false, "Field {$this->name} is required."];
         }
     }
     return true;
 }
Пример #6
0
 /**
  * Translate LazyRecord RuntimeColumn to ActionKit param object.
  *
  * @param RuntimeColumn $column
  * @param BaseModel $record
  * @return Param;
  */
 public static function toParam(RuntimeColumn $column, BaseModel $record = null, Action $action = null)
 {
     $name = $column->name;
     $param = new Param($name, $action);
     if ($column->isa) {
         $param->isa($column->isa);
     }
     // Convert notNull to required
     // required() is basically the same as notNull but provides extra
     // software validation.
     // When creating records, the primary key with auto-increment support is not required.
     // However when updating records, the primary key is required for updating the existing record..
     if ($column->notNull) {
         if ($action && $column->primary) {
             if ($action instanceof CreateRecordAction) {
                 // autoIncrement is not defined, then it requires the input value.
                 if ($column->autoIncrement) {
                     $param->required = false;
                 } else {
                     $param->required = true;
                 }
             } else {
                 if ($action instanceof UpdateRecordAction || $action instanceof DeleteRecordAction) {
                     // primary key column is required to update/delete records.
                     $param->required = true;
                 }
             }
         } else {
             $param->required = true;
         }
     }
     foreach ($column->attributes as $k => $v) {
         // if the model column validator is not compatible with action validator
         if ($k === 'validator' || $v instanceof Raw) {
             continue;
         }
         $param->{$k} = $v;
     }
     // if we got record, load the value from it.
     if ($record) {
         // $val = $record->{$name};
         // $val = $val instanceof BaseModel ? $val->dataKeyValue() : $val;
         $val = $record->getValue($name);
         $param->value = $val;
         // XXX: should get default value (from column definition)
         //      default value is only used in create action.
     } else {
         $default = $column->getDefaultValue();
         if (!$default instanceof Raw) {
             $param->value = $default;
         }
     }
     // convert related collection model to validValues
     if ($param->refer && !$param->validValues) {
         if (class_exists($param->refer, true)) {
             $referClass = $param->refer;
             // it's a `has many`-like relationship
             if (is_subclass_of($referClass, 'LazyRecord\\BaseCollection', true)) {
                 $collection = new $referClass();
                 $options = array();
                 foreach ($collection as $item) {
                     $label = method_exists($item, 'dataLabel') ? $item->dataLabel() : $item->id;
                     $options[$label] = $item->dataKeyValue();
                 }
                 $param->validValues = $options;
             } elseif (is_subclass_of($referClass, 'LazyRecord\\BaseModel', true)) {
                 // it's a `belongs-to`-like relationship
                 $class = $referClass . 'Collection';
                 $collection = new $class();
                 $options = array();
                 foreach ($collection as $item) {
                     $label = method_exists($item, 'dataLabel') ? $item->dataLabel() : $item->id;
                     $options[$label] = $item->dataKeyValue();
                 }
                 $param->validValues = $options;
             } elseif (is_subclass_of($referClass, 'LazyRecord\\Schema\\DeclareSchema', true)) {
                 $schema = new $referClass();
                 $collection = $schema->newCollection();
                 $options = array();
                 foreach ($collection as $item) {
                     $label = method_exists($item, 'dataLabel') ? $item->dataLabel() : $item->id;
                     $options[$label] = $item->dataKeyValue();
                 }
                 $param->validValues = $options;
             } else {
                 throw new Exception('Unsupported refer type');
             }
         } elseif ($relation = $record->getSchema()->getRelation($param->refer)) {
             // so it's a relationship reference
             // TODO: implement this
             throw new Exception('Unsupported refer type');
         }
     }
     //  Convert column type to param type.
     // copy widget attributes
     if ($column->widgetClass) {
         $param->widgetClass = $column->widgetClass;
     }
     if ($column->widgetAttributes) {
         $param->widgetAttributes = $column->widgetAttributes;
     }
     if ($column->immutable) {
         $param->widgetAttributes['readonly'] = 'readonly';
     }
     if ($column->renderAs) {
         $param->renderAs($column->renderAs);
     } elseif ($param->validValues || $param->validPairs || $param->optionValues) {
         $param->renderAs('SelectInput');
     } elseif ($param->name === 'id') {
         $param->renderAs('HiddenInput');
     } else {
         // guess input widget from data type
         $typeMapping = array('date' => 'DateInput', 'datetime' => 'DateTimeInput', 'text' => 'TextareaInput');
         if (isset($typeMapping[$param->type])) {
             $param->renderAs($typeMapping[$param->type]);
         } else {
             $param->renderAs('TextInput');
         }
     }
     return $param;
 }