Content


NTL
Core
CDbModel

CDbModel


This is a base class for models that are not connected to database (like Active Record models) but requires attributes and validation functionality.

You can find more information in comments below.

class CDbModel :public CReleasable
{
public:
    using ErrorInfos = std::vector<ErrorInfo>;
    using ErrorList = std::map<String, ErrorInfos>;
protected:
    bool AddError(const String& attribute, const String& error, const int code); // Add error to specified attribute 
    virtual bool BeforeValidate(); // Called before validation. Return false to fail validation 
    virtual void AfterValidate(); // Called after successful validation 
    
    template<class ValidatorType> ReleasablePtr<ValidatorType> Rule(IDbAttribute *attribute, ReleasablePtr<ValidatorType> validator); // Add validator to an attribute 
    template<class ValidatorType> ReleasablePtr<ValidatorType> Rule(const String &attribute, ReleasablePtr<ValidatorType> validator); // Add validator to an attribute (by attribute name) 
    template<class ValidatorType> ReleasablePtr<ValidatorType> Rule(const StringArray &attributes, ReleasablePtr<ValidatorType> validator); // Add validator to attributes (by attribute names) 

    CDbModel();
public:
    String const &Scenario; // Current scenario. "DEFAULT" by default 

    CAttributes Attributes; // Collection of model attributes 

    bool hasAttribute(const String& name) const; // Check whether attribute with specified name exists 
    DbValue getAttribute(const String& name) const; // Return attribute value by name. If not found, results Type is Null 
    DbValue getOldAttribute(const String& name) const; // Return attribute old value by name. If not found, results Type is Null 
    bool hasErrors() const; // Return whether there are any errors after the last validation 
    bool hasErrors(const String &attribute) const; // Return whether there are any errors related to the specified attribute after the last validation 
    const ErrorList& getErrors() const; // Return list of all errors 
    ErrorInfos getErrors(const String& attribute) const; // Return list of all errors related to specified attribute 
    ErrorInfo getFirstError() const; // Return first error. If not found returns ErrorInfo{ 0, L"" } 
    ErrorInfo getFirstError(const String &attribute) const; // Return first error for specified attribute.If not found returns ErrorInfo{ 0, L"" } 

    bool setAttribute(const String& name, const DbValue& value); // Set attribute by name. If not found, returns false 
    bool setScenario(const String &value); // Set scenario name 

    virtual bool Validate(const StringArray& attributes = EmptyStringArray); // Run validation. "attributes" argument may be used to specify which attributes should be validated. If it is empty, all attributes will be validated. Return false is any validation fails 
};
Namespace: nitisa
Include: Nitisa/Db/DbModel.h