Search code examples
phpsmartyprestashopprestashop-1.7

using prestashop's 1.7 smarty url tag in a from action property


I'm using prestashop 1.7.2.1 to build a module with a front controller.

what I'm trying to do is to add the smarty{url} tag to the action property of the form. the problem is that once I submit the form all the get parameters that are provided in action property of the from are erased. this is a normal behaviour in html.

this is my code:

<form id="car-type-form" action="{url entity='module' name='tuxinmodcartype' controller='cartypeproducts'}" method="get">
    <div id="company-name-input-form-group" class="form-group row">
        <label for="company-name-input" class="col-sm-2 col-form-label">Company</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" id="company-name-input" name="company_name" placeholder="Company" aria-label="Company" required="required"/>
        </div>
    </div>
...
</form>

In general I can paste that smarty {url} tag to a variable. on submit to add the values of the form fields dynamically to the variable I created and use it to redirect instead of allowing the form to submit.

I just don't know if this is the best solution.

maybe there is something that I missed.

any ideas?

thank you


Solution

  • for now what I did is to add hidden input elements to the form based on the created url string.

    I have this on the first line of my template file:

    <script type="text/javascript">
        var carTypeProductsUrl='{url entity='module' name='tuxinmodcartype' controller='cartypeproducts'}';
    </script>
    

    and on the submit function I added the following code:

    ...
    if (isError) {
                event.preventDefault();
            } else {
                $('.hidden-form-params').remove();
                var params = carTypeProductsUrl.substr(carTypeProductsUrl.indexOf('?')+1).split('&');
                params.forEach((paramStr)=>{
                    var paramsArray = paramStr.split('=');
                    const paramKey = paramsArray[0];
                    const paramvalue = paramsArray[1];
                    $('#car-type-form').append(`<input type="hidden" name="${paramKey}" value="${paramvalue}" class="hidden-form-params" />`);
                });
            }
    

    this method feels a bit.. hacky?! :) I just want to make sure this is the way to go