1

Currently i have to validate my model before saving because of some custom validation rules setted on the model which won't run when using the EntriesService saveEntry method.

This causes a two step validation when errors arise, first you get the custom validation errors and after that if there is still something wrong you get the "base" validation errors.

Is this a normal behavior?

$model->validate();

After that i am saving the model like so:

$result = craft()->entries->saveEntry($model);

Custom rules on model:

public function rules()
{
    $rules = parent::rules();
    $rules[] = ['state', 'validateState'];
    $rules[] = ['vacancyId', 'validateId'];
    //$rules[] = ['vacancyId', 'numerical', 'unique' => false];
    $rules[] = ['office', 'validateOffice'];
    $rules[] = ['sector', 'validateSector'];

    return $rules;
}

Why are the custom validation rules not evaluated on the saveEntry method?

1

It's normal behavior... EntriesService->saveEntry() never calls validate() on the model directly, but does call validate() on the EntryRecord and if there are errors, add it back to the model and return.

So if you want to run custom validation rules before calling saveEntry you'll need to do that yourself and respond accordingly.

2
  • Thanks Brad, so there is no way to combine those two validations. Overruling rules with defaults Craft or Yii validators like the one commented out (which i can't get to work), are these validated on the EntryRecord method then or still separately? Thanks in clearing things up! – Jacob Apr 1 '16 at 11:13
  • They would be validated on the EntryRecord if that was the class you were extending. – Brad Bell Apr 1 '16 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.