How to take the url of different forms and validate via javascript

Asked

Viewed 58 times

0

Currently I am doing so and every time I submit the form the alert returns error even with the fields filled correctly

$('.form-ajax').on('submit', function(e) {
        e.preventDefault();

        var form = $('#formnews');

        $.ajax({
            method: 'POST',
            url: form.attr('action'),
            async: true,
            data: form.serialize(),
            cache: false,
            success: function(result) {
                if (result.success) {
                    swal('Bom trabalho!', 'Sua inscrição foi efetuada com sucesso!', 'success');
                    $('#formnews').val('');
                } else {
                    swal('Alguma coisa errada', result.message, 'warning');
                }
            },
            error: function(data) {
                swal('Erro!', 'Ocorreu um erro', 'error');
            }
        })
    });
<form action="{{ route('newsletter.post.ajax') }}" method="POST" id="formnews">
                    {{ csrf_field() }}
                    <div class="field has-addons">
                        <div class="control is-expanded has-icons-left has-icons-right">
                            <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                                <p class="control is-expanded has-icons-left has-icons-right">
                                    <input id="email_newsletter" type="email_newsletter" class="input" name="email_newsletter" value="{{ old('email_newsletter') }}" placeholder="Seu melhor email" required>
                                        @if ($errors->has('email'))
                                            <span class="help-block">
                                                <strong>{{ $errors->first('email') }}</strong>
                                            </span>
                                        @endif
                                    <span class="icon is-small is-left">
                                            <i class="fa fa-envelope"></i>
                                    </span>
                                    <span class="icon is-small is-right">
                                             <i class="fa fa-check"></i>
                                    </span>
                                </p>
                            </div>
                        </div>
                        <div class="control">
                            <button type="submit" class="button is-warning">
                                Cadastrar
                            </button>
                        </div>
                    </div>
               </form>

1 answer

1

Well, without the full code I can only make assumptions, but at first I think you might be the problem in the if test (result.Success).

How do you validate the fields via code? For the result to be equal to TRUE you must determine some tests, however, they do not seem to occur at any time.

So I think the code will always return error even with the fields filled correctly.

  • When I use only id it validates normally, the problem is that I have other names on the page and I can’t use the same id to validate so I tried using class but it doesn’t work

  • Sorry, but I didn’t understand at what point the use or not of the same id would interfere with the result validation problem. Maybe the problem is another. A good practice is to always create different Id for each form. It would be helpful if you made more snippets of your source code available.

  • <form name="formcontato" class="form-horizontal form-ajax" method="POST" action="{{ route('page.student.post post') }}">

  • I have two formulas one on each page but I need to pass them through a validation via java script and return a message with switalert more as do the same java script validate two different formularies

  • <form action="{{ route('newsletter.post.ajax') }}" method="POST" id="formnews"> this code is from the second form

  • Ok, but where the validation of fields happens?

  • i had seen a way to envez to catch id or class pick the url of the page so I could use validate the form based on its url and not on its id

  • the validation happens in the form itself

  • I did some research but unfortunately I couldn’t find anything relevant. Frankly, I find it unusual not to opt for the use of Id, since it would greatly facilitate the work. I feel I can not help beyond this.

  • I found the problem was by id even the problem was the logic of my controller that was wrong

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.