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!'
]);
}
}