Skip to content

Commit

Permalink
add a script to automatically refresh a form (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie-Lucas authored and lenybernard committed Oct 10, 2016
1 parent ea29d1a commit e02a271
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ For each kind of action (link or form), you have to add a data-toggle="ajax" to
</a>
If the link contains the data-form property, it will submit the form using the properties of the link.

### Auto-refresh a form

You can automatically send an ajax request to update your form when an input/select change, just add the data attribute "data-refreshonchange":

```html
<select name="category" data-refreshonchange="true">
<option value="transport">Transport</option>
<option value="structure">Stucture</option>
</select>
```

Then in your controller's action:
```php
if ($request->query->get('novalidate', false) === false) {
if ($form->isValid()) {
// form is valid
} else {
// avoid to display errors when novalidate
$form = $formFactory->createForm();
$form->setData($user);
}
}
```

For some reason, you would not refresh some parts of your form (for example an input type="file"). Then, add the data attribute 'data-ignoreonchange="$some_unique_id"'.

Extra features
---

Expand Down
5 changes: 4 additions & 1 deletion Resources/config/assetic_injector.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
{
"javascripts":
{
"foot": "@TroopersAjaxBundle/Resources/public/js/ajax.js",
"foot": [
"@TroopersAjaxBundle/Resources/public/js/ajax.js",
"@TroopersAjaxBundle/Resources/public/js/ajax-updateOnChange.js"
],
"bootstrap-extra": "@TroopersAjaxBundle/Resources/public/js/ajax-modal.js"
},
"stylesheets":
Expand Down
36 changes: 36 additions & 0 deletions Resources/public/js/ajax-updateOnChange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// For every change on inputs and select with attr data-refreshOnChange
$(document).on(
'change',
'select[data-refreshOnChange="true"], ' +
'input:checkbox[data-refreshOnChange="true"], ' +
'input:radio[data-refreshOnChange="true"]',
function(event) {
var form = $(this).parents('form');
var formdata = (window.FormData) ? new FormData(form[0]) : null;
var data = (formdata !== null) ? formdata : form.serialize();
var ignoredChanges = {};
$(form).find('[data-ignoreonchange]').each(function (idx, elem) {
var $elem = $(elem);
ignoredChanges[$elem.attr('data-ignoreonchange')] = $elem;
});
$.ajax({
url : form.attr('action') + (form.attr('action').split('?')[1] ? '&' : '?') + 'novalidate',
data : data,
type : $(form).attr('method'),
contentType : false,
dataType : 'json',
processData : false,
}).done(function(response){
if (typeof response === 'object' && response.hasOwnProperty("html")) {
form.html(response.html);
} else {
form.html(response);
}
$.each(ignoredChanges, function(index, elem){
$('[data-ignoreonchange="' + index + '"]').replaceWith(elem);
});
}).fail(function(response) {
console.log(response);
});
}
);

0 comments on commit e02a271

Please sign in to comment.