1

i have current_stock variable which is updated by ajax request everytime i clicked Edit button. after the ajax request success, it will popup a modal like this enter image description here

im using jQuery validation to validate the form. one of the rules is max. im trying to set the max value equal to current_stock variable. im trying this :

$("#form_detail").validate({
    rules: {
        jumlah: {
            required: true,
            number: true,
            max: current_stock
        }
    },
    messages: {
        jumlah: {
            required: "Jumlah tidak boleh kosong.",
            number: "Jumlah hanya dapat diisi dengan angka.",
            max: "Tidak bisa melebihi sisa Stok"
        }
    },
    submitHandler: function(form) {
        save_edit();
    }
});

but it always says the current_stock variable is 0. i tried to print the variable using the console, the variable value is right. but the value in the validation is always 0. this is where i initialize the current_stok variable

 <script type="text/javascript">
        var base_url = "<?php echo base_url()?>";
        var current_stock = 0;
 </script>

this is the function that make an ajax request and popup the modal

    function edit_jumlah(id_penjualan, id_barang) {
    $.ajax({
        url: base_url + "index.php/transaksi/get_by_id/" + id_penjualan + "/" + id_barang,
        type: "GET",
        dataType: "JSON",
        success: function(data) {
            $('[name="harga"]').val(data.harga_jual);
            $('[name="id_penjualan"]').val(data.id_penjualan);
            $('[name="id_barang_modal"]').val(data.id_barang);
            $('[name="nama_barang"]').val(data.nama_barang);
            $('[name="jumlah"]').val(data.jumlah);
            $('[name="sisa_stok"]').val(data.stok);
            current_stock = data.stok;
            $("#modal_detail").modal("show");
        },
        error: function(jqXHR, textStatus, errorThrown) {

        }
    })
}

and this is the validate function

 $("#form_detail").validate({
        rules: {
            jumlah: {
                required: true,
                number: true,
                max: sisa_stok
            }
        },
        messages: {
            jumlah: {
                required: "Jumlah tidak boleh kosong.",
                number: "Jumlah hanya dapat diisi dengan angka."
            }
        },
        submitHandler: function(form) {
            save_edit();
        }
    });
9
  • How/when are you setting current_stock? Commented May 21, 2018 at 16:28
  • when click the Edit button, the Edit button also trigger the modal Commented May 21, 2018 at 16:35
  • Must be a scope issue. Post all the code refering to the modal and varible setting. Commented May 21, 2018 at 16:38
  • What are you trying to achieve with this line max: current_stock? Does the max change dynamically depending on the value of current_stock? If you're trying to validate the current_stock to make sure it is less than a max value, you need to provide the max value on that like, e.g. max: 50. Commented May 21, 2018 at 16:41
  • @muecas i edited the post with the code reference to modal and variable Commented May 21, 2018 at 18:00

2 Answers 2

1

The problem is that jQuery validator uses the variable value, and not its reference: at the moment of the validation initialization the value of curent_stock is 0, so that will be the value passed to the validator, and not a reference to the variable.

To achieve that, you should modify the field rule each time the ajax call has been completed, instead of:

current_stock = data.stok;

In your success function, change it to:

$('[name="jumlah"]').rules('add', {
    required: true,
    number: true,
    max: data.stok,
    messages: {
        required: "Jumlah tidak boleh kosong.",
        number: "Jumlah hanya dapat diisi dengan angka.",
        max: "Tidak bisa melebihi sisa Stok"
    },
});
Sign up to request clarification or add additional context in comments.

3 Comments

@MochamadRamdannyLukman Great! Mark the answer as valid then. Thanks!
but can i write whole validate code in the ajax success? cause i getting trouble to set the message..
@MochamadRamdannyLukman getting trouble setting that specific field validation message? Let me edit my anwer.
0

As muecas explained in his answer:

The problem is that jQuery validator uses the variable value, and not its reference: at the moment of the validation initialization the value of curent_stock is 0, so that will be the value passed to the validator, and not a reference to the variable.

However, you don't need to rewrite all the rules on the element to make it work. That's too much code redundancy and a violation of the DRY (Don't Repeat Yourself) principle. All you need to do is update the max rule with the new value of data.stok:

In $.ajax.success, change this line:

current_stock = data.stok;

to:

$('#jumlah').rules('add', {
  max: data.stok
});

Unless you need it for something else, you don't need the variable current_stock. In your $("#form_detail").validate, simply give `max a static value:

max: 0

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.