0

I am trying to add a file upload field to all story node forms. I tested the code and I am successfully getting the file upload field on the form.

$form['my_file_upload'] = array(
    '#attributes' => array('enctype' => 'multipart/form-data'),
  '#type' => 'file',
  '#title' => t('Image'),
  '#description' => t('Image should be of minimum 50x50px width and should be of type png or jpg'),
  '#collapsible' => TRUE,
  '#collapsed' => TRUE,
  '#size' => 48,
  '#weight' => 4,
);

Now the question is How do I validate the file and also how do I retrieve the file after it has passed validation?

(Site runs on Drupal 6)

  • 1
    why dont you use cck? – Mohammed Shameem Jan 20 '13 at 17:51
  • I am not not sure on how to use CCK in this scenario. – Binny Jan 20 '13 at 17:54
  • what are you trying to do... – Mohammed Shameem Jan 20 '13 at 17:54
  • I am trying to retrieve an image from a node create form and then use it in my code. – Binny Jan 20 '13 at 17:56
  • retrive an image and what do you want to do with.. – Mohammed Shameem Jan 20 '13 at 17:58
1

What you describe is what the Upload module does: In upload_form_alter() (an implementation of hook_form_alter()), it adds the form element to the node edit forms, and in the form submission handler it adds to the form (upload_node_form_submit()), it handles the uploaded files. With _upload_file_limits(), it checks the user didn't reach the assigned quota; with file_save_upload(), it checks the file passes the validators, and saves the file.

The Upload module, once enabled, allows you to enable the file upload for the content types. For example, for the Page content type, you can enable it at admin/content/node-type/page.

screenshot

In the node edit form, the form element the Upload module adds appears as in the following screenshot.

screenshot

If the Upload module is already enabled, and you need a upload form element more, than you should use the CCK module.

1

question is already asked here, Custom validation for a form?

but still solving your problem, you can add validate method in form alter .

$form[#validate][] = 'function_name_callback';

and then you can validate the form submission in that function

function function_name_callback($form, $form_state){
//validate here
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.