Guest User

Untitled

a guest
Jan 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. public function buildForm(array $form, FormStateInterface $form_state) {
  2.  
  3. $form['#attributes'] = array('enctype' => "multipart/form-data");
  4.  
  5. $form['pdf_file'] = array(
  6. '#type' => 'file',
  7. '#title' => $this->t('PDF File'),
  8. '#description' => $this->t('PDF files only'),
  9. //'#multiple' => TRUE,
  10. '#required' => TRUE,
  11. );
  12.  
  13. $form['submit'] = array(
  14. '#type' => 'submit',
  15. '#value' => $this->t('Save'),
  16. );
  17.  
  18. return $form;
  19. }
  20.  
  21. public function buildForm(array $form, FormStateInterface $form_state) {
  22.  
  23. $form['#attributes'] = array('enctype' => "multipart/form-data");
  24.  
  25. $form['pdf_file'] = array(
  26. '#type' => 'file',
  27. '#title' => $this->t('PDF File'),
  28. '#description' => $this->t('PDF files only'),
  29. //'#multiple' => TRUE,
  30. );
  31.  
  32. $form['submit'] = array(
  33. '#type' => 'submit',
  34. '#value' => $this->t('Save'),
  35. );
  36.  
  37. return $form;
  38. }
  39.  
  40. $( "#target" ).submit(function( event ) {
  41. if( $(".pdf-file-field").files.length == 0 ){
  42. alert("Please add a file");
  43. event.preventDefault();
  44. }
  45. });
  46.  
  47. namespace DrupalmymoduleForm;
  48.  
  49. use DrupalCoreFormFormBase;
  50. use DrupalCoreFormFormStateInterface;
  51.  
  52. /**
  53. * Class ExampleFileManagedForm.
  54. *
  55. * @package DrupalmymoduleForm
  56. */
  57. class ExampleFileManagedForm extends FormBase {
  58.  
  59.  
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getFormId() {
  64. return 'example_file_managed_form';
  65. }
  66.  
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function buildForm(array $form, FormStateInterface $form_state) {
  71.  
  72.  
  73. $form['pdf'] = array(
  74. '#type' => 'managed_file',
  75. '#title' => $this->t('Image'),
  76. '#upload_location' => 'public://pdf/',
  77. '#upload_validators' => array(
  78. 'file_validate_extensions' => array('pdf'),
  79. ),
  80. '#required' => TRUE,
  81. );
  82.  
  83. $form['submit'] = [
  84. '#type' => 'submit',
  85. '#value' => t('Submit'),
  86. ];
  87.  
  88. return $form;
  89. }
  90.  
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function validateForm(array &$form, FormStateInterface $form_state) {
  95. parent::validateForm($form, $form_state);
  96. }
  97.  
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function submitForm(array &$form, FormStateInterface $form_state) {
  102. dump($form_state->getValue('pdf')); die();
  103.  
  104. }
  105.  
  106. }
  107.  
  108. array:1 [▼
  109. 0 => "467"
  110. ]
Add Comment
Please, Sign In to add comment