【问题标题】:Is there a proper Drupal 7 form api managed_file tutorial?是否有合适的 Drupal 7 form api managed_file 教程?
【发布时间】:2012-07-17 02:22:10
【问题描述】:

我在互联网上搜索了半个小时,但没有找到。

我想使用 D7 中的 managed_file form api 来允许上传图片文件;更具体地说,我认为“#upload_validators”属性可以解决问题(如果可能,在上传之前验证文件扩展名;或者至少在验证阶段验证,但不在提交函数中验证)。我检查了示例模块中的 image_example 和 file_example,但找不到正确的用法。

所以我想知道是否有关于 managed_file 的适当教程?非常感谢。

更新:我在从 file.field.inc 搜索 drupal 目录后看到了一个示例,并按照示例编写了如下代码:

$form['file_upload'] = array(
  '#type'   => "managed_file",
  '#title'  => t("Upload"),
  '#descripion' => t("Only Image Files are allowed."),
  '#progress_indicator' => "bar",
  '#upload_location'    => "public://img/dish",
  "#upload_validators"  => array("file_validate_extensions" => "png gif jpg"),
);

这解决了问题。

【问题讨论】:

  • @nmc:感谢您的回复。如果我早点刷新此页面并看到您的回复.. 我可以节省 20 分钟..
  • 正如接受的答案中正确显示的那样,文件扩展名必须为array("png gif jpg"),否则您将收到通知,并且验证不会按照您的预期进行。

标签: drupal drupal-7


【解决方案1】:

这是一个使用中的managed_file 字段示例,其中包括取自https://drupal.stackexchange.com/a/5630/1103#upload_validators

$form['picture']['file'] = array(
  '#type' => 'managed_file',
  '#title' => t('picture'),
  '#description' => t('Allowed extensions: gif png jpg jpeg'),
  '#default_value' => (isset($foo->picture->fid) ? $foo->picture->fid : ''),
  '#upload_location' => variable_get('picture_upload_location'),
  '#upload_validators' => array(
    'file_validate_extensions' => array('gif png jpg jpeg'),
    // Pass the maximum file size in bytes
    'file_validate_size' => array(MAX_FILE_SIZE*1024*1024),
  ),
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    相关资源
    最近更新 更多