本文整理汇总了PHP中FileField::setValue方法的典型用法代码示例。如果您正苦于以下问题:PHP FileField::setValue方法的具体用法?PHP FileField::setValue怎么用?PHP FileField::setValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileField
的用法示例。
在下文中一共展示了FileField::setValue方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testUploadMissingRequiredFile
/**
* Test different scenarii for a failed upload : an error occured, no files where provided
*/
public function testUploadMissingRequiredFile()
{
$form = new Form(new Controller(), 'Form', new FieldSet($fileField = new FileField('cv', 'Upload your CV')), new FieldSet(), new RequiredFields('cv'));
// All fields are filled but for some reason an error occured when uploading the file => fails
$fileFieldValue = array('name' => 'aCV.txt', 'type' => 'application/octet-stream', 'tmp_name' => '/private/var/tmp/phpzTQbqP', 'error' => 1, 'size' => 3471);
$fileField->setValue($fileFieldValue);
$this->assertFalse($form->validate(), 'An error occured when uploading a file, but the validator returned true');
// We pass an empty set of parameters for the uploaded file => fails
$fileFieldValue = array();
$fileField->setValue($fileFieldValue);
$this->assertFalse($form->validate(), 'An empty array was passed as parameter for an uploaded file, but the validator returned true');
// We pass an null value for the uploaded file => fails
$fileFieldValue = null;
$fileField->setValue($fileFieldValue);
$this->assertFalse($form->validate(), 'A null value was passed as parameter for an uploaded file, but the validator returned true');
}
示例2: setValue
public function setValue($value, $data = null)
{
if ($value) {
$this->processValue($value);
} elseif ($data) {
if (is_array($data)) {
if (isset($data[$this->name])) {
$this->processValue($data[$this->name]);
}
} else {
$field = $this->name . 'ID';
if ($data->{$field}) {
$this->fileID = $data->{$field};
}
}
}
return parent::setValue($value);
}
示例3: setValue
/**
* Loads the related record values into this field. UploadField can be uploaded
* in one of three ways:
*
* - By passing in a list of file IDs in the $value parameter (an array with a single
* key 'Files', with the value being the actual array of IDs).
* - By passing in an explicit list of File objects in the $record parameter, and
* leaving $value blank.
* - By passing in a dataobject in the $record parameter, from which file objects
* will be extracting using the field name as the relation field.
*
* Each of these methods will update both the items (list of File objects) and the
* field value (list of file ID values).
*
* @param array $value Array of submitted form data, if submitting from a form
* @param array|DataObject|SS_List $record Full source record, either as a DataObject,
* SS_List of items, or an array of submitted form data
* @return UploadField Self reference
*/
public function setValue($value, $record = null)
{
// If we're not passed a value directly, we can attempt to infer the field
// value from the second parameter by inspecting its relations
$items = new ArrayList();
// Determine format of presented data
if (empty($value) && $record) {
// If a record is given as a second parameter, but no submitted values,
// then we should inspect this instead for the form values
if ($record instanceof DataObject && $record->hasMethod($this->getName())) {
// If given a dataobject use reflection to extract details
$data = $record->{$this->getName()}();
if ($data instanceof DataObject) {
// If has_one, add sole item to default list
$items->push($data);
} elseif ($data instanceof SS_List) {
// For many_many and has_many relations we can use the relation list directly
$items = $data;
}
} elseif ($record instanceof SS_List) {
// If directly passing a list then save the items directly
$items = $record;
}
} elseif (!empty($value['Files'])) {
// If value is given as an array (such as a posted form), extract File IDs from this
$class = $this->getRelationAutosetClass();
$items = DataObject::get($class)->byIDs($value['Files']);
}
// If javascript is disabled, direct file upload (non-html5 style) can
// trigger a single or multiple file submission. Note that this may be
// included in addition to re-submitted File IDs as above, so these
// should be added to the list instead of operated on independently.
if ($uploadedFiles = $this->extractUploadedFileData($value)) {
foreach ($uploadedFiles as $tempFile) {
$file = $this->saveTemporaryFile($tempFile, $error);
if ($file) {
$items->add($file);
} else {
throw new ValidationException($error);
}
}
}
// Filter items by what's allowed to be viewed
$filteredItems = new ArrayList();
$fileIDs = array();
foreach ($items as $file) {
if ($file->exists() && $file->canView()) {
$filteredItems->push($file);
$fileIDs[] = $file->ID;
}
}
// Filter and cache updated item list
$this->items = $filteredItems;
// Same format as posted form values for this field. Also ensures that
// $this->setValue($this->getValue()); is non-destructive
$value = $fileIDs ? array('Files' => $fileIDs) : null;
// Set value using parent
return parent::setValue($value, $record);
}
示例4: setValue
public function setValue($value, $record = null)
{
// Extract value from underlying record
if (empty($value) && $this->getName() && $record instanceof DataObject) {
$name = $this->getName();
$value = $record->{$name};
}
// Convert asset container to tuple value
if ($value instanceof AssetContainer) {
if ($value->exists()) {
$value = array('Filename' => $value->getFilename(), 'Hash' => $value->getHash(), 'Variant' => $value->getVariant());
} else {
$value = null;
}
}
// If javascript is disabled, direct file upload (non-html5 style) can
// trigger a single or multiple file submission. Note that this may be
// included in addition to re-submitted File IDs as above, so these
// should be added to the list instead of operated on independently.
if ($uploadedFile = $this->extractUploadedFileData($value)) {
$value = $this->saveTemporaryFile($uploadedFile, $error);
if (!$value) {
throw new ValidationException($error);
}
}
// Set value using parent
return parent::setValue($value, $record);
}