0

I have a function in a controller that when executed, check for existence of a record in a table and returns that record. This function is used as validation of a model inside the same controller. For example:

public function somethingValidate() {

    $someVariable = SomeTable::find()->where(['some_id' => $someVariable])->one();
    if ($model) {
        return $model;
    } else {
         return false;

Here is the validation part of controller:

public function actionSave() {
    $model = new TestModel() {
           if ($this->somethingValidate()) {
                try {
                  --- REST OF THE CODE ---

How can i now, pass $someVariable variable into a TestModel and manipulate the data either on save or beforeSave.

Any assistance is greatly appreciated.

2
  • Please correct your example first for better understanding. What is $model there? Where is $someVariable taken from? Commented Feb 18, 2017 at 8:36
  • $model is an actual model being saved with RESTful call, the $someVariable is taken from different table, different model and is used only for validation. If record exists, there is a id field referenced in $model table, where I need to save $someVariable->id Due to the nature of the project, I'm unable to provide better code example, sorry. Commented Feb 18, 2017 at 8:51

1 Answer 1

1

For example, an Investment amount should be more or equal with the Proposal's min_investment value. Below is the beforeValidate() function in Investment model.

public function beforeValidate()
    {
        parent::beforeValidate();

        $proposal = Proposal::findOne($this->proposal_id);

        if ($this->amount < $proposal->min_investment)
        {
            $this->addError('amount', Yii::t('app', 'Minimum amount should be ' . $proposal->min_investment));
            return FALSE;
        }

        return TRUE;
    }

Is that what you're trying to achieve?

Sign up to request clarification or add additional context in comments.

2 Comments

That is basically it. Now, what I really need is after the validation is successful, I need to save $proposal->id to another model.
I still don't understand the logic of your application. If I post my Proposal model full code which have afterSave() function, could it be more useful for you? Inside the afterSave() function, I create another row/data to ProposalLog model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.