This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

thiago33406
thiago33406

I have created a job posting model, but implementing the validation trait has proven to be quite the challenge.

Following documentation: https://octobercms.com/docs/database/traits#validation

class Job extends Model
{
    use \October\Rain\Database\Traits\Validation;

    /**
     * @var array Validation rules
     */
    public $rules = [
         'title' => 'required'
    ];

     public $customMessages = [
         'required' => ':attribute is required.',
     ];

When adding title to the rules I am not able to save a record anymore. All I get is an alert saying "error".

When trying to create a new record I still get the same error alert but with a console message saying "Missing required fields", even though the title field was filled out before I tried to save.

I saw in another forum post that the fix was to add, but I saw no difference.

public function formBeforeSave($model)
    {
        $model->validate();
    }

I was able to add my own validation by adding to the models beforeValidate method, but I was wondering how I could correct this behaviour.

public function beforeValidate()
    {
        if (!$this->title)
        {
            throw new ValidationException([
               'title' => 'Posting title is required!'
            ]);
        }
}
JeffGoldblum
JeffGoldblum

Are you sure the title field is actually present? What's your fields.yaml?

thiago33406
thiago33406

Edit Other settings: CMS - 1.0.464 | PHP - 7.2.22

I made sure the title field was present in the yaml file, and also tried using

public $rules = [
         'Job[title]' => 'required'
    ];

Here is the beginning of my fields.yaml

tabs:
    fields:
        title:
            label: 'Position Title:'
            span: auto
            required: 1
            type: text
            tab: 'General Information'
       close_date:
            label: 'Posting Close Date:'
            mode: date
            span: auto
            default: now
            required: 1
            type: datepicker
            tab: 'General Information'
           ...

Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.