Guest User

Untitled

a guest
Jul 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. {!!Form::model($fichatecnica,['route'=>['fichatecnica.update',$fichatecnica->idFichaTecnica],'method'=>'PUT', 'id'=>'form-fichatecnica'])!!}
  2.  
  3. <form method="POST" action="url/fichatecnica/1234" accept-charset="UTF-8" id="form-fichatecnica">
  4. <input name="_method" type="hidden" value="PUT">
  5. <input id="token" type="hidden" value="{{ csrf_token() }}">
  6.  
  7. {!!Form::submit('Modificar',["class"=>"btn btn-warning", "onclick"=>'validarFormulario(event, 'fichatecnica', 'modal', 'modalFichaTecnica');'])!!}
  8.  
  9. function validarFormulario(event, formulario, tipo = 'vista', modal = '') {
  10.  
  11. $("#msj").html('');
  12. route = "http://" + location.host + "/" + formulario;
  13.  
  14.  
  15.  
  16. token = $("#token").val();
  17. data = $('#form-' + formulario).serialize();
  18. sw = true;
  19.  
  20. $.ajax({
  21. async: false,
  22. url: route,
  23. headers: { 'X-CSRF-TOKEN': token },
  24. type: 'POST',
  25. dataType: 'json',
  26. data: data,
  27.  
  28. beforeSend: function(data) {
  29. // console.log(data);
  30. },
  31.  
  32. success: function(data) {
  33. console.log(data);
  34. },
  35. error: function(respuesta) {
  36.  
  37. if (typeof respuesta.responseJSON !== 'undefined') {
  38. // Concatenamos un mensaje con listas para mostrarle al usuario los errores de validación
  39. var mensaje = 'Por favor verifique los siguientes valores <br><ul>';
  40. $.each(respuesta.responseJSON, function(index, value) {
  41. mensaje += '<li>' + value + '</li>';
  42. });
  43. mensaje += '</ul>';
  44.  
  45. // ubicamos el formulario al proncipio para que el usuario vea las validaciones
  46. document.body.scrollTop = 0; // Safari
  47. document.documentElement.scrollTop = 0; // Chrome, Firefox, IE y Opera
  48.  
  49. // ubicamos el mensaje en el div de errores
  50. $("#msj").html(mensaje);
  51. $("#msj-error").fadeIn();
  52.  
  53. // cambiamos el Switch para indicar que existen errores
  54. sw = false;
  55. }
  56.  
  57. }
  58.  
  59. });
  60.  
  61. // si la validación devuelve errores detenemos la accion del formulario para que no intente guardar
  62. if (sw == false) {
  63. event.preventDefault();
  64. } else {
  65. // si es un modal, debemoscerrar el modal y ejecutar la recarga del formulario padre, sino, no hacemos nada
  66. if (tipo == 'modal') {
  67. event.preventDefault();
  68. window.parent.$('#' + modal).modal('hide');
  69. window.parent.location.reload();
  70. }
  71. }
  72. }
  73.  
  74. Route::resource('fichatecnica','FichaTecnicaController');
Add Comment
Please, Sign In to add comment