0

I'm trying to use an external JS file in my twig. The goal is just to verify client's input. When I put directly my script in my Transfert.html.twig , my script is well executed but when I used an external file with assetic nothing happen. The link created by assetic is good(I can see my script when i click on it in my web page source code).but firebug says

"SyntaxError: expected expression, got '<'
<script type="text/javascript">" "ReferenceError: verifyMontant is not defined"

I registred my bundle into app/config/config.yml: " bundles: [FASTTransfertBundle]", so I guess no problem form here

Now this is my code: Transfert.html.twig:

{# src/FAST/TransfertBundle/Resources/views/Default/Transfert.html.twig #}
{% extends "FASTTransfertBundle::layout.html.twig" %}
{% block title %}{{ parent() }} - Index{% endblock %}
{% block body %}
    {{ form_label(form.montant) }} {{ form_widget(form.montant,{'attr':{'onblur':'verifyMontant(this)'}}) }}
    {% javascripts '@FASTTransfertBundle/Resources/public/javascript/verifyTransfert.js' %}
        <script type="text/javascript" src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}

And this is my javascript file :

<script type="text/javascript">
//color if wrong
function changeColor(field,error)
{
    if(error)
        field.style.backgroundColor = "#fba";
    else
        field.style.backgroundColor = "";
}

function verifyMontant(field)
{
    var montant=  field.value.replace(/\D+/g,'');
    var regex = /^\-?[0-9]*\.?[0-9]+$/;
    if(!regex.test(field.value)){
        changeColor(field, true);
        return false;
    }
    else if(montant.length != 11){
        changeColor(field, true);
        return false;
    }
    else{
        changeColor(field,false);
        return true;
    }
}
</script>

1 Answer 1

3

You must remove

<script type="text/javascript">

from your javascript file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.