R
R
Roquie2014-07-12 01:09:40
PHP
Roquie, 2014-07-12 01:09:40

What is the benefit of creating html forms in php classes?

Why create forms in php classes and describe each field there?
I don’t understand what the profit is if the same thing is calmly written in html, except that if you write the forms “by hand”, the web application page will load a little faster.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
L
Lorem Ipsum, 2014-07-12
@nobodynoone

The simplest example is fast and convenient validation, for example, in django it is done like this

class AuthenticationForm(forms.Form):
    username = forms.CharField(max_length=254)
    password = forms.CharField(label=_("Password"), widget=forms.PasswordInput)

Can be used in code like this
form = AuthenticationForm(request.POST) #передаю в форму POST запросы, можно и GET и вообще все что угодно. 
if AuthenticationForm.is_valid(): # если форма прошла валидацию, то делаем что то 
    pass

This is the simplest and most understandable example, which may not persuade you to use forms, but nevertheless, it will make you understand that forms are not made in order to be.
In general, in django, forms, like models, are an indispensable thing that quite often saves dozens of lines of code.
If you can use forms, use them!

A
Alexey, 2014-07-12
@rdifb0

For automation, this is done, because the form still needs to be checked, saved and / or sent. In this case, only the form is described by hands, and everything else is automatic.

S
Sergey, 2014-07-12
Protko @Fesor

The reasons are banal - you have a form in HTML - consider the presentation layer, and business logic - models and services. Forms in the form of classes - the bridge between the two layers. It takes care of all those tedious things like binding values, binding form values ​​on the model, transforming data from model format to presentation format (let's say the date model field can be transformed into several date and time inputs). Usually all these things can be reused from project to project, due to which the value of such things increases dramatically. Well, yes, the logic of processing forms in this regard does not depend on the presentation in any way.
Alas, I am not aware of the implementation of forms in the falcon, I worked closely only with Symfony Forms from what is more serious. A typical opinion about such components is about the same as yours, why, it’s complicated, simple things are too complicated ... But when it comes to the fact that after a year of development, you need to quickly add some functionality to all forms, for example ... recently they asked here - how to save the previous value and do it automatically, such things save. You can implement one simple extension, in fact, a decorator over inputs, specify for which types it should be used, and there already fence your logic. As a result, the time to implement new functionality is reduced in proportion to the size of the application. On serious projects without such things, life is generally hard and sad.
In a word, by choosing simplicity of implementation and rejection of all these unnecessary forms of builders, you lose control over the flexibility of the system and increase the time for adding new functionality in the future.

V
Vitaly Zheltyakov, 2014-07-12
@VitaZheltyakov

The reason is banal - less writing. Although it is also not small in classes, it is still smaller and easier to modify.

R
Roquie, 2014-07-12
@Roquie

What is the time saving if for complex forms you have to create your own elements in special classes?
docs.phalconphp.com/ru/latest/reference/tutorial-i...
https://github.com/phalcon/invo/blob/master/app/li...
Yes, and how, for example, using this
https:/ /github.com/phalcon/vokuro/blob/master/app/...
Create such a simple form
getbootstrap.com/css/#forms
__
that's me to make a normal skeleton from form tags, using such solutions is really easy , fast and convenient. But it’s more difficult to move beyond the form skeleton somewhere than to write it in html.
For example, the result of executing
https://github.com/phalcon/vokuro/blob/master/app/...
will be like this (roughly...):

<form action="/" method="post">
        <label for="email">Email</label>
        <input type="text" name="email" placeholder="Email"/>
        <label for="password">Password</label>
        <input type="password" name="password" placeholder="Password"/>
        <label for="remember">
            <input type="checkbox" name="remember" checked/>
            Remember
        </label>

        <input type="hidden" name="csrf" value="token ..."/>
        <input type="submit" name="go" class="btn btn-success"/>
</form>

This is the usual standard frame.
And if the form is like in bootstrap
<form role="form">
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
  </div>
  <div class="form-group">
    <label for="exampleInputFile">File input</label>
    <input type="file" id="exampleInputFile">
    <p class="help-block">Example block-level help text here.</p>
  </div>
  <div class="checkbox">
    <label>
      <input type="checkbox"> Check me out
    </label>
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

then, as I understand it, divs inside the form will have to be added with lines in the class, which complicates code support rather than simplifies it.
How to be?

M
mistergonza, 2014-07-24
@mistergonza

I will answer in one word - "Inheritance".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question