Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation groups (and buttons) #4

Closed
scottgutman opened this issue Dec 4, 2014 · 7 comments
Closed

Validation groups (and buttons) #4

scottgutman opened this issue Dec 4, 2014 · 7 comments

Comments

@scottgutman
Copy link

Thanks for this bundle, it's really useful.

I would like to know how I could use validation groups and buttons with this bundle.

For example, suppose that only part of a form is visible, and there are "next" and "previous" buttons.

How would I be able to do "group validation" on the next button, and all validation on submit?

I tried to just add some additional JS code, but the validator is out of scope since it's encapsulated in a function closure. Could we maybe add the option to have the validator in the global scope?

@boekkooi
Copy link
Owner

boekkooi commented Dec 5, 2014

Sorry but i'm not sure what you mean with this issue.
Are you having a problem using https://github.com/craue/CraueFormFlowBundle or a simular bundle? or are you displaying a full form and adding the next and prev buttons in the (twig) template?

If you are using a bundle to create the steps please let me know which one and is possible create a gist with a example of the code you are using.

If you are doing a prev/next in the html with one form then I don't think you need to use any validation groups or buttons. Since you can do all of this using javascript event handlers on the buttons.
If you need access to the validator you can use var validator = $('form').validate(); after the validate has been called by the generated javascript.
Depending on the defaults you use for your next button onclick you may need to set ignore to ':not(:visible)' and for the finish button change ignore to :hidden.
If you need any more help please create a gist with the code.

Just to clarify:

@scottgutman
Copy link
Author

Thank you for your help! I added the var validator = $('form').validate(); and that solved the problem of the scope errors. but.... valid() is always true for empty fields.
I made a js fiddle so you can see the example and code of form in action. http://jsfiddle.net/Ly6tzrc3/ Thank you again for making this bundle. I am new to symfony and its a huge help

@boekkooi
Copy link
Owner

boekkooi commented Dec 5, 2014

Ok i get your point you wish to change the active validation groups using javascript. This is currently not possible but can made possible.

Then again I'm pretty sure you should just not use validation groups in this way since jquery can ignore none visible fields. Removing the firstPanel validation group should make your code work.
Also change your nextPanel code to this once the groups are gone:

var nextPanel = function () {
    if ($('form').valid()) {
        $('.first').slideUp();
        $('.second').slideDown();
    }
}

@scottgutman
Copy link
Author

Well here's something funny. The code you suggested only works if all the js code is loaded in the <head>. When loaded in the <body>, $('form').valid() always returns true. Oy!

Here is the updated fiddle http://jsfiddle.net/scottgutman/gxm0twev/
you can change the script location with the dropdown in the upper left corner.

Why would it matter where the script is loaded?

@scottgutman
Copy link
Author

---EDIT: ignore this comment, as it's not exactly the problem.
I think it's been failing because the submit button is hidden. When I remove this part of the code,

form.find("*[name=\"scs_intakebundle_intake\x5Bsubmit\x5D\"]").click(function () {
        groups = {
            "Default": true,
            "firstPanel": true
        };
    });

Validation then works. I removed the validation groups, but this code is still generated for the Default group Is there a way to remove this section of code?

@scottgutman
Copy link
Author

I was looking into the file: form_validate.js.twig. I noticed that enforce_validation_groups was true even though all of them were removed.

Then in JqueryValidationExtention.php, I noticed that enforce_validation_groups is controlled by the number of buttons on the form: 'enforce_validation_groups' => count($context->getButtons()) > 0

Question: Won't every form have at least 1 submit button? So I changed the 0 to 1, and the group rules disappeared leaving a working script. I also, moved the endif statement below to encompass the form.find(...button etc.

@boekkooi
Copy link
Owner

boekkooi commented Dec 6, 2014

Thanks very much for all your feedback and research. I just pushed a bunch of fixed based on this issue.
It is now possible to change the validation groups using the validator using for example validator.settings.validation_groups['Defautl'] = true.
Also bug regarding enforce_validation_groups is fixed.

Please do a composer update (using dev-master) and good luck. If you encounter any more issues please report them 😄

@boekkooi boekkooi closed this as completed Dec 6, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants