0

I'm trying to do a bit of form validation in angular before I submit it. I'm using the ng-submit in my form, but no matter what I do, the form still submits... Here is what I currently have:

Rails:

<%= form_tag("/createListing", "ng-submit" => "formvalidation($event)") do %>
    ... form contents in here
    <button ng-disabled="!agree_terms" value="Start" type="submit">Submit</button>
<% end %>

Javascript:

$scope.formvalidation = function() {
    var errors = validationService.dosomevalidation();

    if (errors.length === 0) {
        $event.target.submit();
    } else {
        $scope.errors = errors;
        return false;
    }
}

My thought process is kind of how I would do it jQuery or vanilla javascript. Any ideas why the form would still submit?

Thanks in advance.

3
  • Shouldn't be using rails templating with Angular. Commented Oct 24, 2014 at 5:01
  • I have tried to not use the form_tag, but that didn't work. Commented Oct 24, 2014 at 6:04
  • @HappyCry If what you want is to create a confirm button after hitting submit in your form then you should use a directive to catch the submit event and fire the message, take a look a this: gist.github.com/asafge/7430497 hope it helps Commented Oct 24, 2014 at 9:23

1 Answer 1

1

I have my form validating correctly (using rails, angularjs, haml):

%form.nuevaRecetaForm{:name => 'newRecipeForm', :novalidate => '', 'ng-submit' => 'form.submit(newRecipeForm.$valid)'}
  = text_field :recipe, :title, placeholder: 'Título', :required => '', 'ng-model' => 'recipeTitle'

Don't need extra JS to validate de form. Each field you want to validate you just need to specify the rule in the field, in my case the field is required. Then in the form tag use the property $valid as you can see in the example and angular do the validations for you!

Any questions you have I've be pleased to answer to help you solve this problem!!

Sign up to request clarification or add additional context in comments.

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.