Attach library to custom form I'm building a custom module (say 'mycalc') with a custom block with a custom form inside.
Now I want to attach a library to the form and add some styling and live manipulation.

I created the mycalc.libraries.yml like this:

myform:
  version: 1.x
  css:
    theme:
      css/mycalc.css: {}
  js:
    js/mycalc.js: {}
  dependencies:
    - core/jquery


I added the js/mycalc.js and css/mycalc.css files with few basic lines.
Then in the src/Form/MyCalcForm.php (where is set the MyCalcForm class), i edited the buildForm method adding this just before the return():

$form['#attached']['library'][] = 'mycalc/myform';


But there's no js nor css loading.
What's wrong? Please help!
You need to attach library in your hook_form_alter():

/**
* Implements hook_form_alter().
*/

function &lt;YOUR_MODULE_NAME&gt;_form_alter(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){

    /*Attach library, for Blazy implementation you will need
       $form['#attached']['library'][] = 'blazy/load';
    */
    $form['#attached']['library'][] = '&lt;YOUR_THEME_NAME&gt;/&lt;library_name&gt;';
    $form['#attached']['library'][] = '&lt;YOUR_MODULE_NAME&gt;/&lt;library_name&gt;';
    /*Pass even variable from this function to JS settings*/
    $form['#attached']['drupalSettings']['&lt;YOUR_MODULE_NAME&gt;']['&lt;route_name&gt;'] ['variable'] = 'PHP Value To JS';

    return $form;
}


If you pass variable with above mentioned method - $form['#attached']['drupalSettings']['&lt;YOUR.... then that variable will be available in your JS file. 

(function ($, Drupal, window) {
  Drupal.behaviors.helpers = {
    attach: function (context, settings) {
      'use strict';

       console.log(settings);
       console.log(settings.variable); //Should log variable value
    }
  };
})(jQuery, Drupal, window);


Add javascript to form by module