Getting Data from Users

Yii 2 Getting Data from Users

Getting Data from Users Yii 2


Content of the «Getting Data from Users» section:


Articles section «Getting Data from Users»:

Extending yii2 ActiveForm on the Client Side
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Extending ActiveForm on the Client Side

The [[yii\widgets\ActiveForm]] widget comes with a set of JavaScript methods that are used for client validation. Its implementation is very flexible and allows you to extend it in different ways. In the following these are described.

Getting Data for Multiple Models in Yii 2
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Getting Data for Multiple Models

When dealing with some complex data, it is possible that you may need to use multiple different models to collect the user input. For example, assuming the user login information is stored in the user table while the user profile information is stored in the profile table, you may want to collect the input data about a user through a User model and a Profile model. With the Yii model and form support, you can solve this problem in a way that is not much different from handling a single model.

Collecting tabular input in Yii 2
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Collecting tabular input

Sometimes you need to handle multiple models of the same kind in a single form. For example, multiple settings, where each setting is stored as a name-value pair and is represented by a Setting active record model. This kind of form is also often referred to as "tabular input". In contrast to this, handling different models of different kind, is handled in the section Complex Forms with Multiple Models.

Uploading Files in Yii 2
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Uploading Files

Uploading files in Yii is usually done with the help of [[yii\web\UploadedFile]] which encapsulates each uploaded file as an UploadedFile object. Combined with [[yii\widgets\ActiveForm]] and models, you can easily implement a secure file uploading mechanism.

Validating Input in Yii 2
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Validating Input

As a rule of thumb, you should never trust the data received from end users and should always validate it before putting it to good use.

Given a model populated with user inputs, you can validate the inputs by calling the [[yii\base\Model::validate()]] method. The method will return a boolean value indicating whether the validation succeeded or not. If not, you may get the error messages from the [[yii\base\Model::errors]] property. For example,

Creating Forms in Yii 2
Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

Creating Forms

  • ActiveRecord based forms: ActiveForm
    • Wrapping with begin() and end()
    • ActiveField
  • Creating Lists
    • Creating a drop-down list
    • Creating a radio list
    • Creating a checkbox List
  • Working with Pjax
    • Values in Submit Buttons and File Upload
  • Further Reading