Hi, Habr!
Once, a year ago, I wrote about my small library, which facilitates the development of forms on sites. Recently I released the 3rd version, which, in fact, was rewritten from scratch to become more correct and more convenient. But in my article I will not repeat the README and DEMO , but rather I will show in practice how it helps to write less code.
But before that, I would like to give some information about the new version. In it, I got rid of the malsup jquery.form dependency to send files, and I divided one large repository into several small independent repositories, while retaining, at the same time, the general all-in-one assembly:
preventDefault()
. Therefore, in the case of an incorrect connection order, the form was first blocked and then validated, and if there were validation errors, the form could not be resubmitted. form-extra-events solves this problem; it provides several new types of form events, one of which ensures that the form is sent and this process cannot be interrupted. In addition, events start and end of form submission are generated, which is used in all other features of the library.From scripts we use only jQuery, bootstrap and paulzi-form.all.js. In this prototype, we do not use a single line of JS-code written specifically for the prototype site.
switch($action)
, instead of directing it to a specific action (for example, in Yii2). And if you open a modal window, you will see that the submit button had to be made outside the form itself, however it continues to function, since the form
attribute was specified to it. And most importantly, these attributes greatly help out in situations when you need to make a small form in a large form, which the HTML standard forbids.?proce_from=&proce_to=&tdp_from=&tdp_from=&line[]=i5
Using the library, if you add the data-submit-empty="false"
attribute to the form data-submit-empty="false"
empty fields will not be sent, and the result will be a more comprehensible URL:
?line[]=i5
data-lock="false"
data-loading-text
and data-loading-icon
attributes, which change the text of the button and add an icon. Also, form-loading
and btn-loading
classes are added to the form and button, this allows you to zastilizovat state through CSS.data-via="ajax"
attribute, and voila, the form is sent via AJAX. And not only that, those who have ever been involved in transferring files via AJAX know that this is not so easy, because support for sending files appeared only from XMLHttpRequrest 2. For this, third-party libraries are often included, which often require writing to the server parts of special handlers, store files in a temporary folder, and then when sending the form in a separate request, take them from there. In our case, there is practically no need to think about it - everything goes as if the form was sent in the standard way. If necessary, you can easily display the percentage of data sent by hooking to the uploadprogress
event. <span class="badge" data-insert-context="document" data-insert-to=".cart-block .badge" data-insert-mode="replaceWith">43</span> <div class="alert alert-success alert-dismissible" role="alert" data-insert-context="document" data-insert-to=".alert-fixed"> <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span></button> ! </div> <div class="cart-block-hover" data-insert-context="document" data-insert-to=".cart-block-hover" data-insert-mode="replaceWith"> <div class="row"> <div class="col-xs-6 text-left">AMD K6</div> <div class="col-xs-6">10 .</div> </div> <div class="row"> <div class="col-xs-6 text-left">Intel Celeron</div> <div class="col-xs-6">12 .</div> </div> <div class="row"> <div class="col-xs-6 text-left">Intel Core i7</div> <div class="col-xs-6">1 .</div> </div> <div class="row cart-block-total"> <div class="col-xs-6 text-left">:</div> <div class="col-xs-6"><span class="price"><span>117 000</span> â‚˝</span></div> </div> </div>
As you can see, the response consists of three html elements with data-insert-to
, data-insert-context
, data-insert-mode
attributes. These attributes define how and where to insert each element, for example, for the first span.badge
data-insert-context="document" data-insert-to=".cart-block .badge" data-insert-mode="replaceWith"
means that this element needs to replace the element .cart-block .badge
and the search zone with this selector - the whole document. All this allows you to carry out the above steps without a single line of code.
data-via
attribute that indicates how to send the form when you click on it.Source: https://habr.com/ru/post/306278/
All Articles