0
I have this function to check the state registration and CNPJ/CPF fields and if they are right it performs the submit
, only that in the submit
, he is not respecting the required of the fields ViewModel
$('#FornecedorNovo').submit(function (e) {
e.preventDefault();
var url = "/Fornecedor/VerificaInscricao";
var Insc = $("#InscricaoEstadual").val();
var Tipo = $("#TipoPessoa").val();
var Isento = $("#InscricaoIsento").prop('checked');
var form = this,
$form = $(form); // Salvamos o formulário atual em uma variável
$.ajax({
url: url,
data: { insc: Insc, isento: Isento },
datatype: "json",
type: "POST",
success: function (data) {
if (data.resultado == true) {
$("#messageI").html(data.mensagem);
if (data.mensagem != 'O campo Inscrição é obrigatório.') {
$("#InscricaoEstadual").val('');
$("#InscricaoEstadual").focus();
}
} else {
var url1 = "/Fornecedor/VerificaDocumento";
var Documento = $("#Documento").val();
$.ajax({
url: url1,
data: { documento: Documento, tipo: Tipo },
datatype: "json",
type: "POST",
success: function (data) {
if (data.resultado == true) {
$("#message").html(data.mensagem);
$("#Documento").val('');
$("#Documento").focus();
}
else {
$form.off('submit').submit();
}
}
});
}
}
})
});
Wouldn’t it be right for him to do Submit only if the fields were correct? Because if the field State Registration and Document are in agreement, it sends anyway the data.
The
required
does not validate the fields, it just forces the user to type anything.– Sam
So I need them to be filled, and this way, ignore.
– Mariana
you are calling the Submit on the button? using required input did not work?
– adventistaam
I set it this way:
[Required(ErrorMessage = "O campo {0} é obrigatorio.")]
 [StringLength(150)]
 public string Nome { get; set; }
in thedataannotations
– Mariana
Ta using some framework: Laravel, Ionic, Angular?
– adventistaam
No, I work with ASP CORE, and I used the
ViewModel
to inform the required, @adventistaam when the field is not filled in, for example the name, which isrequired
, but the two of this above rule are correct, it sends, ignoring the mandatory completion of the field.– Mariana
you are checking the Asp side request with Modelstate.Isvalid?
– Lucas Miranda
@Lucasmiranda.
– Mariana
in my view what is happening is that required checks only for null, when you take with jquery the val it comes empty, but does not come null ("")
– Lucas Miranda
It appears the messages that the field is required and sends normally, so I believe that the problem is not this, only that as has this part in the code
$form.off('submit').submit();
he is ignoring the required fields.– Mariana
I won’t be able to help you much because it’s not very clear what you want to do with your request, but I can tell you that this tangle of requests and calling the form at the end is confusing, but still I think the problem is there on the side of the Asp, tries to add in your required this here: [Required(Allowemptystrings=false)] [Displayformat(Convertemptystringtonull=false)]
– Lucas Miranda
I do the validation on
dataannotations
, but when the data of thisfunction
above are correct and he falls into$form.off('submit').submit();
it does not validate the fields, but if I take this part, or put onlyreturn true;
it validates the fields, but does not do Submit.– Mariana
I saw that you do the validation with date Annotation, but you did not put anything to validate empty
– Lucas Miranda
@Lucasmiranda took the test and the same thing happens, in this part of the function in
submit
I can’t verify?– Mariana