1

Below is the HTML and the JavaScript being used to display the dropdown only if one of the options from the preceding dropdown is selected. When I select the one that is linked the following dropdown it works while when I select the second option not linked to the following dropdown and click submit, it throws the error "An invalid form control with name='AdjustmentBuyerPrice' is not focusable". Please point out the mistake that I did in my code.

`{include file="header.tpl" page_name='Amazon Order Adjustment' extra_javascript='<script language="JavaScript" src="includes/update_shipping_info.js"></script>'}

{literal}
<style type="text/css">
#loading-icon {
    position: absolute; 
    top: 75px; 
    right: 250px; width: 
    32px; height: 32px;
    display: none; 
    background: url('/images/lightbox/loading.gif');
}
</style>
{/literal}

{if isset($tpl_error_msg) }
    <div id="message">{$tpl_error_msg}</div>
{/if}

{include file='view_order_snippet.tpl'}

<form name="amazon_order_adjustment" id="amazon_order_adjustment" method="post" action="amazon_order_adjustment.php?id={$id}&{$search_params}">
        <div class="row">
            <fieldset>
            <legend>Order Line Items</legend>
                <table id="table2" style="position: relative; float: left;">
                    <tr valign="top">
                        <th width="10%"></th>
                        <th width="10%">SKU</th>
                        <th width="30%">Item</th>
                        <th width="5%">Qty</th>
                        <th width="10%">Status</th>
                        <th width="15%">Ship Mode</th>
                        <th width="20%">Tracking#</th>
                    </tr>
                    {if !($update_shipping_info_flag)}
                        <tr>
                            <td colspan="7" align="center">No Items to display</td>
                        </tr>
                    {else}
                        {section name=lineitems loop=$tpl_order_list}
                            <tr id=row1 valign="top">
                                <td><input type="radio" name="check[]" value="{$tpl_order_list[lineitems].id}">
                                <input type="hidden" name="vendor_id_array[]" value="{$tpl_order_list[lineitems].vendor_fk}">
                                </td>
                                <td>{$tpl_order_list[lineitems].sku}
                                <td>{$tpl_order_list[lineitems].item_description}</td>
                                <td>{$tpl_order_list[lineitems].quantity}</td>
                                <td>{$tpl_order_list[lineitems].item_status}</td>
                                <td>{$tpl_order_list[lineitems].shipping_mode}</td>
                                {if $tpl_order_list[lineitems].shipping_tracking_no == ""}
                                <td>N/A</td>
                                {else}
                                <td>{$tpl_order_list[lineitems].shipping_tracking_no}</td>
                                {/if}
                            </tr>
                        {/section}
                    {/if}
                    <tr>
                        <td align="right" colspan="3">Action Type</td>
                        <td align="left" colspan="4">
                            <select id="action_type" name="action_type" required>   
                                <option value="">Select Action</option>
                                {html_options options=$tpl_action_type}
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" colspan="3">Enter Refund Amount</td>
                        <td align="left" colspan="4"><input type="number" step="1" min="" id="refund_amount" name="refund_amount" value="" required /></td>
                    </tr>
                    <tr>
                        <td align="right" colspan="3">Adjustment Reason</td>
                        <td align="left" colspan="4">
                            <select id="AdjustmentReason" name="AdjustmentReason" required> 
                                <option value="" selected="selected">Select Adjustment Reason</option>
                                {html_options options=$tpl_adjustment_reason}                           
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" colspan="3">Adjustment Type</td>
                        <td align="left" colspan="4">
                            <select id="adjustment_type" name="adjustment_type" required>   
                                <option value="" selected="selected">Select Adjustment Type</option>
                                {html_options options=$tpl_adjustment_type}                             
                            </select>
                        </td>
                    </tr>
                    <tr id="adjustment_buyer_price">
                        <td align="right" colspan="3">Adjustment Buyer Price Type</td>
                        <td align="left" colspan="4">
                            <select id="AdjustmentBuyerPrice" name="AdjustmentBuyerPrice" required> 
                                <option value="">Select Adjustment Buyer Price Type</option>
                                {html_options options=$tpl_adjustment_buyer_price}
                            </select>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </div>
        <div class="row">
            <input type="hidden" id="tpl_grand_total_box" name="tpl_grand_total_box" value="{$tpl_grand_total}">
            <input type="hidden" id="tpl_tax_box" name="tpl_tax_box" value="{$tpl_tax}">
            <input type="submit" id="save_button" name="submit_action" value="refund" class="button">
            <input type="submit" id="cancel_button" name="cancel_action" value="Cancel" class="button">
        </div>
    </div>
</form>
{literal}
<script type="text/javascript">
    $(document).ready(function() {
        $('#adjustment_buyer_price').hide(); 
        $("#adjustment_type").change(function () {
            var cur_option_val = $(this).val(); 
            if (cur_option_val == "ItemPriceAdjustments") {
                $('#adjustment_buyer_price').show(); 
                $('#AdjustmentBuyerPrice').attr("required", "required") //add required
            } else {
                $('#adjustment_buyer_price').hide();
                $('#AdjustmentBuyerPrice').removeAttr("required") //remove required.
            }
        });
    }); 
</script>
{/literal}
{include file="footer.tpl"}
CodeForGood
  • 767
  • 7
  • 30

1 Answers1

1

This is happening because you have AdjustmentBuyerPrice as required so when you have not selected value ItemPriceAdjustments its hidden and when you click on submit button that error shows .Instead you can remove required attribute when that select box is hidden else add required attribute .

Demo Code :

$(document).ready(function() {
  $('#adjustment_buyer_price').hide();
  $("#adjustment_type").change(function() {
    var cur_option_val = $(this).val();
    if (cur_option_val == "ItemPriceAdjustments") {
      $('#adjustment_buyer_price').show();
      $('#AdjustmentBuyerPrice').attr("required", "required") //add required
    } else {
      $('#adjustment_buyer_price').hide();
      $('#AdjustmentBuyerPrice').removeAttr("required") //remove
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="amazon_order_adjustment" id="amazon_order_adjustment" method="post" action="amazon_order_adjustment.php?id={$id}&{$search_params}">
  <div class="row">
    <fieldset>
      <legend>Order Line Items</legend>
      <table id="table2" style="position: relative; float: left;">
        <tr valign="top">
          <th width="10%"></th>
          <th width="10%">SKU</th>
          <th width="30%">Item</th>
          <th width="5%">Qty</th>
          <th width="10%">Status</th>
          <th width="15%">Ship Mode</th>
          <th width="20%">Tracking#</th>
        </tr>
        <tr>
          <td colspan="7" align="center">No Items to display</td>
        </tr>
        <tr id=row1 valign="top">
          <td><input type="radio" name="check[]" value="1">
            <input type="hidden" name="vendor_id_array[]" value="2">
          </td>
          <td>A
            <td>B</td>
            <td>5</td>
            <td>ok</td>
            <td>htm</td>
            <td>N/A</td>


        </tr>

        <tr>
          <td align="right" colspan="3">Action Type</td>
          <td align="left" colspan="4">
            <select id="action_type" name="action_type" required>
              <option value="">Select Action</option>
              <option value="">A</option>
            </select>
          </td>
        </tr>
        <tr>
          <td align="right" colspan="3">Enter Refund Amount</td>
          <td align="left" colspan="4"><input type="number" step="1" min="" id="refund_amount" name="refund_amount" value="" required /></td>
        </tr>
        <tr>
          <td align="right" colspan="3">Adjustment Reason</td>
          <td align="left" colspan="4">
            <select id="AdjustmentReason" name="AdjustmentReason" required>
              <option value="" selected="selected">Select Adjustment Reason</option>
              <option value="">A</option>
            </select>
          </td>
        </tr>
        <tr>
          <td align="right" colspan="3">Adjustment Type</td>
          <td align="left" colspan="4">
            <select id="adjustment_type" name="adjustment_type" required>
              <option value="" selected="selected">Select Adjustment Type</option>
              <option value="ItemPriceAdjustments">ItemPriceAdjustments</option>
              <option value="ItemPriceAdjustments1">5</option>

            </select>
          </td>
        </tr>
        <tr id="adjustment_buyer_price">
          <td align="right" colspan="3">Adjustment Buyer Price Type</td>
          <td align="left" colspan="4">
            <!--remove required from here-->
            <select id="AdjustmentBuyerPrice" name="AdjustmentBuyerPrice">
              <option value="">Select Adjustment Buyer Price Type</option>
              <option value="">A</option>

            </select>
          </td>
        </tr>
      </table>
    </fieldset>
  </div>
  <input type="submit" id="save_button" name="submit_action" value="refund" class="button">

</form>
Swati
  • 28,069
  • 4
  • 21
  • 41
  • Why is required twice in this line `$('#AdjustmentBuyerPrice').attr("required", "required") //add required` – CodeForGood Feb 23 '21 at 06:30
  • you need `attributename` and `value` to set in attr so `required` is `attributename` and `required` is `value` and it will show like this : `required=required`. There are other way also to set attr check [this](https://stackoverflow.com/questions/19166685/jquery-add-required-to-input-fields) out . – Swati Feb 23 '21 at 06:38
  • When clicking the cancel button now, I get the same console error `An invalid form control with name='AdjustmentBuyerPrice' is not focusable`. Let me past the full code now for you to have a look at. – CodeForGood Feb 23 '21 at 07:25
  • because your `name='AdjustmentBuyerPrice'` has `required` attribute by default remove that from html as well. – Swati Feb 23 '21 at 07:49
  • Removed the attribute. Now Cancel button works only if I select every dropdown, otherwise it doesn't work. It throws required field error message. – CodeForGood Feb 23 '21 at 08:22
  • why you have `type="submit"` for reset button as well? – Swati Feb 23 '21 at 12:25
  • To redirect the page in the PHP script if the submit value is "Cancel" Am I doing anything wrong with Cancel button here? Please advice – CodeForGood Feb 23 '21 at 14:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229132/discussion-between-swati-and-codeforgood). – Swati Feb 24 '21 at 04:01