diff --git a/.gitignore b/.gitignore index 51cd273..52b0813 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea vendor composer.lock -composer.phar \ No newline at end of file +composer.phar +build/* \ No newline at end of file diff --git a/README.md b/README.md index a40a88c..aaef32c 100644 --- a/README.md +++ b/README.md @@ -158,12 +158,27 @@ This is all the necessary data to make a custom validation action. If you are disagree with an initial error output functionality, you can customize it by redefining the following function: +To redefine a global method: + ```js -FpJsFormValidatorFactory.showErrors = function(errors) { +FpJsFormValidatorFactory.showErrors = function(form, errors) { // put here your logic to show errors } +``` + +To redefine for a specified form: -// The "errors" parameter has the next structure: +```js +document.getElementById('specified_form_id').showErrors = function(form, errors) { + // put here your logic to show errors +} +``` + +The "form" parameter is the current HTMLFormElement element. + +The "errors" parameter is an objech which has the next structure: + +```js var errors = { user_gender: { // This is the DOM identifier of the current field type: 'choice', // This is the form type which you've set up in a form builder diff --git a/Resources/public/js/FpJsFormValidatorFactory.js b/Resources/public/js/FpJsFormValidatorFactory.js index d30151b..254c532 100644 --- a/Resources/public/js/FpJsFormValidatorFactory.js +++ b/Resources/public/js/FpJsFormValidatorFactory.js @@ -87,7 +87,7 @@ var FpJsFormValidatorFactory = new function() { } else { if (!isValid) { event.preventDefault(); - self.showErrors(model.getForm(), model.getMappedErrors()); + self.getMethodAndShowErrors(model); } self.postValidateEvent(model); } @@ -134,13 +134,26 @@ var FpJsFormValidatorFactory = new function() { callback(request.responseText, model.requests[requestId].owner); if (!model.countProcessedRequests()) { - self.showErrors(model.getForm(), model.getMappedErrors()); + self.getMethodAndShowErrors(model); self.postValidateEvent(model); } } }; }; + /** + * Returns the global or local method which shows errors + * + * @param {FpJsFormElement} model + */ + this.getMethodAndShowErrors = function(model) { + if (undefined !== model.getForm().showErrors) { + model.getForm().showErrors(model.getForm(), model.getMappedErrors()); + } else { + this.showErrors(model.getForm(), model.getMappedErrors()); + } + }; + /** * This event will be called after the synchronous or asynchronous form validation * diff --git a/Tests/Functional/JavascriptModelsTest.php b/Tests/Functional/JavascriptModelsTest.php index b6613a6..c256ec6 100644 --- a/Tests/Functional/JavascriptModelsTest.php +++ b/Tests/Functional/JavascriptModelsTest.php @@ -188,11 +188,25 @@ public function testDataTransformers() /** * Test onvalidate event listeners */ - public function testListeners() + public function testOnValidateListeners() { - $form = $this->getSubmittedForm('listeners'); + $form = $this->getSubmittedForm('listeners/onvalidate'); $errors = $this->getElementErrors($form->getParent()->findById('onvalidate_listeners_element')); $this->assertEquals(array('global_listener', 'local_listener'), $errors); } + + /** + * Test onvalidate event listeners + */ + public function testShowErrorListeners() + { + $form = $this->getSubmittedForm('listeners/global_errors'); + $errors = $this->getElementErrors($form->getParent()->findById('onvalidate_listeners_element')); + $this->assertEquals(array('errors_global_listener'), $errors); + + $form = $this->getSubmittedForm('listeners/local_errors'); + $errors = $this->getElementErrors($form->getParent()->findById('onvalidate_listeners_element')); + $this->assertEquals(array('errors_local_listener'), $errors); + } } \ No newline at end of file diff --git a/Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php b/Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php index 8d1efd1..9f4c1f0 100644 --- a/Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php +++ b/Tests/TestBundles/DefaultTestBundle/Controller/FunctionalTestsController.php @@ -207,9 +207,11 @@ public function transformersAction() /** * Check onvalidate listeners * + * @param $mode + * * @return \Symfony\Component\HttpFoundation\Response */ - public function onValidateListenersAction() + public function onValidateListenersAction($mode) { $builder = $this->createFormBuilder(null, array()); $builder @@ -224,8 +226,9 @@ public function onValidateListenersAction() return $this->render( 'DefaultTestBundle:FunctionalTests:index.html.twig', array( - 'form' => $builder->getForm(), - 'checkListeners' => true + 'form' => $builder->getForm(), + 'checkListeners' => true, + 'checkListenersMode' => $mode ) ); } diff --git a/Tests/TestBundles/DefaultTestBundle/Resources/config/routing.php b/Tests/TestBundles/DefaultTestBundle/Resources/config/routing.php index e04f59f..7ed5ece 100644 --- a/Tests/TestBundles/DefaultTestBundle/Resources/config/routing.php +++ b/Tests/TestBundles/DefaultTestBundle/Resources/config/routing.php @@ -59,7 +59,7 @@ $collection->add( 'fp_js_form_validator_test_listeners', new Route( - '/fp_js_form_validator/javascript_unit_test/listeners', + '/fp_js_form_validator/javascript_unit_test/listeners/{mode}', array( '_controller' => $controllerClass . '::onValidateListenersAction', ) diff --git a/Tests/TestBundles/DefaultTestBundle/Resources/views/FunctionalTests/index.html.twig b/Tests/TestBundles/DefaultTestBundle/Resources/views/FunctionalTests/index.html.twig index d5a6575..19f17ce 100644 --- a/Tests/TestBundles/DefaultTestBundle/Resources/views/FunctionalTests/index.html.twig +++ b/Tests/TestBundles/DefaultTestBundle/Resources/views/FunctionalTests/index.html.twig @@ -201,11 +201,22 @@ li.innerHTML = name; document.getElementById('onvalidate_listeners_list').appendChild(li); } - FpJsFormValidatorFactory.onvalidate = function(){ - addOnValidateListenerResult('global_listener') - }; - document.getElementsByTagName('form')[0].onvalidate = function(){ - addOnValidateListenerResult('local_listener') - }; + + {% if checkListenersMode == 'onvalidate' %} + FpJsFormValidatorFactory.onvalidate = function(){ + addOnValidateListenerResult('global_listener') + }; + document.getElementsByTagName('form')[0].onvalidate = function(){ + addOnValidateListenerResult('local_listener') + }; + {% elseif checkListenersMode == 'global_errors' %} + FpJsFormValidatorFactory.showErrors = function(){ + addOnValidateListenerResult('errors_global_listener') + }; + {% elseif checkListenersMode == 'local_errors' %} + document.getElementsByTagName('form')[0].showErrors = function(){ + addOnValidateListenerResult('errors_local_listener') + }; + {% endif %} {% endif %} \ No newline at end of file