Skip to content

Commit

Permalink
Simplify field validation (#100)
Browse files Browse the repository at this point in the history
Solves #99, simplifies structure and method.
  • Loading branch information
OleVik authored and flaviocopes committed Apr 11, 2017
1 parent 80f13ef commit ad97a0b
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions templates/partials/simplesearch_searchbox.html.twig
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
<div class="search-wrapper">
<input class="search-input" type="text" placeholder="{{"PLUGIN_SIMPLESEARCH.SEARCH_PLACEHOLDER"|t}}" value="{{ query }}" data-search-input="{{ base_url }}{{ config.plugins.simplesearch.route == '@self' ? '' : (config.plugins.simplesearch.route == '/' ? '' : config.plugins.simplesearch.route) }}/query" />
{% if config.plugins.simplesearch.display_button %}
<button class="search-submit"><img src="{{ url('plugin://simplesearch/assets/search.svg') }}" /></button>
{% endif %}
<script>
var i, el;
var input = document.getElementsByClassName('search-input');
var searchInput = input[0].dataset.searchInput;
for(i=0; i<input.length; i++) {
el = input[i];
el.addEventListener('keypress', function(event){
if (event.keyCode == 13 && this.value.length >= {{ config.get('plugins.simplesearch.min_query_length', 3) }}) {
event.preventDefault();
window.location.href = searchInput + '{{ config.system.param_sep }}' + this.value;
}
}, false);
}
<form name="search" onSubmit="return validateSearch();">
<input
name="searchfield"
class="search-input"
type="text"
placeholder="{{"PLUGIN_SIMPLESEARCH.SEARCH_PLACEHOLDER"|t}}"
value="{{ query }}"
data-search-input="{{ base_url }}{{ config.plugins.simplesearch.route == '@self' ? '' : (config.plugins.simplesearch.route == '/' ? '' : config.plugins.simplesearch.route) }}/query"
/>
{% if config.plugins.simplesearch.display_button %}
var button = document.getElementsByClassName('search-submit');
for(i=0; i<input.length; i++) {
el = button[i];
el.addEventListener('click', function(event){
event.preventDefault();
window.location.href = searchInput + '{{ config.system.param_sep }}' + input[0].value;
});
}
<button type="submit" class="search-submit">
<img src="{{ url('plugin://simplesearch/assets/search.svg') }}" />
</button>
{% endif %}
</form>
<script>
function validateSearch() {
var input = document.forms["search"]["searchfield"];
var target = input.getAttribute('data-search-input');
if (input.value.length >= {{ config.get('plugins.simplesearch.min_query_length', 3) }}) {
event.preventDefault();
window.location.href = target + '{{ config.system.param_sep }}' + input.value;
} else {
event.preventDefault();
}
}
</script>
</div>

0 comments on commit ad97a0b

Please sign in to comment.