function modelControllerExtension(ModelControllerInterface $modelController) { $request = $modelController->getRequest(); $files = $request->getUploadedFiles(); $uploads = []; // Check if extension is applicable to the current request. foreach ($files as $fieldName => $file) { if (str_endsWith($fieldName, ImageField::FILE_FIELD_SUFFIX)) { // Note: slashes are converted to dots, which delimit path segments to nested fields. See the Field component. $fieldName = str_replace('/', '.', str_segmentsStripLast($fieldName, '_')); $uploads[$fieldName] = $file; } } if ($uploads) { $modelController->onSave(-1, function () use($uploads, $modelController) { /** @var UploadedFileInterface $file */ foreach ($uploads as $fieldName => $file) { list($targetModel, $prop) = $modelController->getTarget($fieldName); $err = $file->getError(); if ($err == UPLOAD_ERR_OK) { static::newUpload($targetModel, $prop, $file); } else { if ($err == UPLOAD_ERR_NO_FILE) { static::noUpload($targetModel, $prop); } else { throw new FlashMessageException("Error {$err}", FlashType::ERROR, "Error uploading file"); } } } }); } }
protected function preRender() { $prop = $this->props; // Output a hidden checkbox that will submit an empty value if the visible checkbox is not checked. // Does not apply to checkboxes of array fields. if (exists($prop->name) && !str_endsWith($prop->name, '[]')) { echo "<input type=checkbox name=\"{$prop->name}\" value=\"\" checked style=\"display:none\">"; } $id = property($prop, 'id'); if ($id) { $prop->containerId = $prop->id . 'Container'; } echo "<!--CHECKBOX-->"; parent::preRender(); }
protected function render() { $prop = $this->props; $class = self::CSS_CLASS; $name = either($prop->name, $prop->id); $this->beginContent(); echo html([when(!str_endsWith($prop->name, '[]'), h('input', ['type' => 'checkbox', 'name' => $name, 'value' => '', 'checked' => true, 'style' => 'display:none'])), h('input', ['type' => 'checkbox', 'id' => $prop->id, 'name' => $name, 'class' => enum(' ', "{$class}-checkbox", "{$class}-" . ($prop->customColor ? substr($prop->customColor, 1) : $prop->color)), 'value' => $prop->value, 'checked' => $prop->checked || isset($prop->testValue) && $prop->value === $prop->testValue, 'disabled' => $prop->disabled, 'autofocus' => $prop->autofocus, 'onclick' => $prop->script]), h('label', ['for' => $prop->id, 'class' => "{$class}-label", 'data-off' => $prop->labelOff, 'data-on' => $prop->labelOn, 'title' => $prop->tooltip])]); }