Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
vendor
composer.lock
composer.phar
composer.phar
build/*
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions Resources/public/js/FpJsFormValidatorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
*
Expand Down
18 changes: 16 additions & 2 deletions Tests/Functional/JavascriptModelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
</script>
{% endif %}