0

I have used this code in my form to manage the field id_pdt that is a foreign key so i will put a text from bar code and then search in mu custom table to find if the sku exists in table product and then save in table the id of this product but i get this error Cannot read property 'value' of null and here is my code

              <?php
             namespace BO\Paquet\Block\Adminhtml\Paquet\Edit;

            use \Magento\Backend\Block\Widget\Form\Generic;

         class Form extends Generic
        {

/**
 * @var \Magento\Store\Model\System\Store
 */
protected $_systemStore;


/**
 * @var \Magento\Store\Model\System\Store
 */
protected $_produit;

/**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Framework\Registry $registry
 * @param \Magento\Framework\Data\FormFactory $formFactory
 * @param \Magento\Store\Model\System\Store $systemStore
 * @param \BO\Prix\Model\Source\Family $family
 * @param \BO\Prix\Model\Source\Choix $choix
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Store\Model\System\Store $systemStore,
       /*        \BO\Paquet\Model\Source\Produit $produit,*/
    array $data = []
) {
    $this->_systemStore = $systemStore;


    parent::__construct($context, $registry, $formFactory, $data);
}

/**
 * Init form
 *
 * @return void
 */
protected function _construct()
{
    parent::_construct();
    $this->setId('paquet_form');
    $this->setTitle(__('Packet Informations'));
}

/**
 * Prepare form
 *
 * @return $this
 */
protected function _prepareForm()
{
    /** @var \BO\Prix\Model\Prix $model */
    $model = $this->_coreRegistry->registry('paquet_paquet');

    /** @var \Magento\Framework\Data\Form $form */
    $form = $this->_formFactory->create(
        ['data' => ['id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post']]
    );

    $form->setHtmlIdPrefix('prix_');

    $fieldset = $form->addFieldset(
        'base_fieldset',
        ['legend' => __('General Information'), 'class' => 'fieldset-wide']
    );

    if ($model->getId()) {
        $fieldset->addField('id_paq', 'hidden', ['name' => 'id_paq']);
    }
       // code a barre - Type Text
    $fieldset->addField(
        'code_a_barre',
        'text',
        ['name' => 'code_a_barre', 'label' => __('Barcode'), 'title' => __('Barcode'), 'required' => true]
    );

    //  designation paquet- Type Text

    $fieldset->addField(
        'designation_paq',
        'text',
        ['name' => 'designation_paq', 'label' => __('Packet Designation'), 'title' => __('Packet Designation'), 'required' => true]
    );

    $fieldset->addField(
        'id_pdt',
        'text',
        [
            'name' => 'id_pdt',
            'label' => __('Code Article'),
            'title' => __('Code Article'),
            'required' => true,
            'class' => 'validate-number'

        ]
    )->setAfterElementHtml('
    <script>
        require([
             "jquery",
        ], function($){
            $(document).ready(function () {
                  var input_value = document.getElementById("code_a_barre").value;
                  var res = input_value.substring(0,9);
                  document.getElementById("id_pdt").value = res ;

            });
          });
   </script>
');


    // quantity
    $fieldset->addField(
        'quantity_paq',
        'text',
        ['name' => 'quantity_paq', 'label' => __('Quantity'), 'title' => __('Quantity'), 'required' => true, 'class' => 'validate-number']
    );

    // poids estimé
    $fieldset->addField(
        'poids_paq',
        'text',
        ['name' => 'poids_paq', 'label' => __('Estimated Weight'), 'title' => __('Estimated Weight'), 'required' => true, 'class' => 'validate-number']
    );

    // Reservé
    $fieldset->addField(
        'reservé',
        'text',
        ['name' => 'reservé', 'label' => __('Reserved'), 'title' => __('Reserved'), 'required' => true, 'class' => 'validate-number']
    );

    // in stock
    $fieldset->addField(
        'in_stock',
        'text',
        ['name' => 'in_stock', 'label' => __('IN stock'), 'title' => __('IN stock'), 'required' => true, 'class' => 'validate-number']
    );






    $form->setValues($model->getData());
    $form->setUseContainer(true);
    $this->setForm($form);

    return parent::_prepareForm();
       }
      }
1

document.getElementById("code_a_barre") is null

That would suggest there isn't an element on the page which has id="code_a_barre"

As you are using jQuery you could use $('#code_a_barre')

But also remember that magento 2 uses require-js. So you need something like this:

require(['jquery'], function($){
    $(document).ready(function () {
        var input = $("#code_a_barre");
        console.log(input_value); // see what comes back
        // $("#code_a_barre").val(); for value
    });
});
  • how to use require_js and how to call this js in my form in backend and thanks for your replay – Developper Magento May 17 at 12:19
  • You've done it. Sorry made my example clearer. Just means that if you are passing jquery as $ you've got to use $("#code_a_barre"). – Dominic Xigen May 17 at 12:26

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.