Validation errors (attribute name => array of errors)
Attribute hints are mainly used for display purpose. For example, given an attribute
isPublic
, we can declare a hint Whether the post should be visible for not logged in users
,
which provides user-friendly description of the attribute meaning and can be displayed to end users.
Eg:-
export class Event extends Model {
getAttributeHints() {
return {
firstName: 'First Name',
...
}
};
Returns the attribute names that are subject to validation in the current scenario.
active attribute names
Removes errors for all attributes or a single attribute.
attribute name. Use null to remove errors for all attributes.
Returns the validators applicable.
the validators applicable.
Returns the text hint for the specified attribute.
string the attribute hint
Returns the text label for the specified attribute.
If the attribute looks like relatedModel.attribute
, then the attribute will be received from the related model.
the attribute name
the attribute label
Attribute labels are mainly used for display purpose. For example, given an attribute
firstName
, we can declare a label First Name
which is more user-friendly and can
be displayed to end users.
Eg:-
export class Event extends Model {
getAttributeLabels() {
return {
firstName: 'First Name',
...
}
};
Returns the first error of the specified attribute.
the error message. Null is returned if no error.
Returns the form name that this model class should use.
The form name is mainly used by ActiveForm to determine how to name the input fields for the attributes in a model. If the form name is "A" and an attribute name is "b", then the corresponding input name would be "A[b]". If the form name is an empty string, then the input name would be "b".
The purpose of the above naming schema is that for forms which contain multiple different models, the attributes of each model are grouped in sub-arrays of the POST-data and it is easier to differentiate between them.
By default, this method returns the model class name as the form name. You may override it when the model is used in different forms.
the form name of this model class.
Returns a value indicating whether there is any validation error.
name. Use null to check all attributes.
whether there is any error.
Returns a value indicating whether the attribute is required.
whether the attribute is required
Populates the model with input data.
This method provides a convenient shortcut for:
if (request.body['FormName]) {
model.setAttributeValues(request.body['FormName]);
if (model.save()) {
// handle success
}
}
which, with load()
can be written as:
if (model.load(request.body) && model.save()) {
// handle success
}
load()
gets the 'FormName'
from the model's getFormName method (which you may override), unless the
$formName
parameter is given. If the form name is empty, load()
populates the model with the whole of data
,
instead of data['FormName']
.
Note, that the data being populated is subject to the safety check by setAttributeValues.
whether load()
found the expected form in data
.
Searches the DB with the params provided.
The search params in a key-value format like: { status:1, title: 'My Titile', ...}
the ActiveDataProvider which can provide the relevant results.
Sets the attribute values in a massive way.
attribute values (name => value) to be assigned to the model.
Performs the data validation.
This method executes the validation rules. Errors found during the validation can be retrieved via Model._errors and [[getFirstError()]].
attribute name or list of attribute names that should be validated. If this parameter is empty, it means any attribute listed in the applicable validation rules should be validated.
whether to call clearErrors before performing validation
whether the validation is successful without any error.
Returns the primary key for this Model class.
By default, this returns id
Generated using TypeDoc
Model is the base class for classes representing relational data in terms of objects.
Model extends from your ORM's Model native model class(eg:- for Sequelize,
sequelize.Model
) and hence have all the features and behaviors included in the ORM's model class.As an example, say that the
Customer
Model class is associated with thecustomer
table. This would mean that the class'sname
attribute is automatically mapped to thename
column incustomer
table. Thanks to Model, assuming the variablecustomer
is an object of typeCustomer
, to get the value of thename
column for the table row, you can use the expressioncustomer.name
. In this example, Model is providing an object-oriented interface for accessing data stored in the database. But Model provides much more functionality than this.All the models used for Pwoli components should be extended from Model
To declare a Model class you need to extend Model
Important Note: If your ORM is Sequelize, you should perform additional steps like defining the model attributes and options for initializing each model like below:
Class instances are obtained in one of two ways:
new
operator to create a new, empty objectBelow is an example showing some typical usage of Model:
If your ORM is Sequelize, please refer https://sequelize.org/master/manual/model-basics.html
Mahesh S Warrier https://github.com/codespede