Filling a readonly input with Select2 in Laravel

0

A client asked me to enter an invoice you can select the identity card of the provider, and depending on it fill a field with the same name, and if you are not registered, you can register.

Then I implemented Select2 for the ID search, and created a input field to be automatically filled in when selecting the provider's ID. In case the provider does not exist, I implemented a modal that will contain a form to register a new provider. All this mounted on Laravel 5.4.

My doubts arise next;

1.- How can I get the input filled with the name of the provider based on the DNI selected in Select2?

2.- In case the provider is not registered in the system How can I display the modal to register the provider automatically?

The DNI and the names are logically in the same table of the BD.

    
asked by Gonzalo 22.09.2017 в 06:18
source

1 answer

1

If I have not misunderstood it, what you want is, once the ID field is filled in, look for the name of the person to whom the DNI belongs. In case there is not an associated person that opens a modal no?

For this you could use a onchange event on the input on which you put the DNI. Within the event, make an AJAX call to your server (make an endpoint that tells you if there is a user that has that DNI) and depending on the AJAX response and fill the field or sample the modal.

We assume that the DNI input has id="dni_field" the code you could use would be something like this (using JQuery ):

$('#dni_field').onchange(function(){
    if(comprobarDNIValido()){//comprobamos si el DNI introducido es válido
        $.get( "ruta/aTuEndPoint.php", function(data)     
            /*
            /* Aqui ya es cuestion de que, o devuelvas en el mensaje que no 
            /* hay datos, o que sea mediante el codigo de respuesta de la petición.
            */

            if(data != "No hay datos"){
                //Asumimos el campo a rellenar con id="nombre_field"
                $("#nombre_field").val(data);
            }else{
                mostrarModal();
            }

        });
    }
});

The documentation regarding Get requests via Jquery can be found here .

p>     
answered by 22.09.2017 в 08:33