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

jimlab56999
jimlab56999

Hello,
I`m extending plugin with custom fields
Add validation, but always get validation error message,
I was trying 2 methods(commented) but without success.

 public function boot()
    {
        RealtyModel::extend(function ($model) {
            $model->hasOne['addproperties'] = ['Eduard\AddProperties\Models\AddProperties'];
            $model->rules = ['floor' => 'required'];
            // $model->bindEvent('model.beforeCreate', function () use ($model) {
            //     $model->rules = ['floor' => 'required'];
            // });
        });

I have in my class

class Plugin extends PluginBase
{
    use \October\Rain\Database\Traits\Validation;

Is there other decision?
Thanks

mjauvin
mjauvin

The validation trait needs to be added to the RealtyModel

jimlab56999
jimlab56999

I have...

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

It works properly only for original fields, but not for extended
It looks like validator do not receive data from input field and alway stay not valid

mjauvin
mjauvin

where did you add the "floor" property?

jimlab56999
jimlab56999

In my Plugin.php of my extended plugin

class Plugin extends PluginBase
{
    use \October\Rain\Database\Traits\Validation;

....

 public function boot()
    {
        RealtyModel::extend(function ($model) {
            $model->hasOne['addproperties'] = ['Eduard\AddProperties\Models\AddProperties'];
            $model->rules = ['floor' => 'required'];
            // $model->bindEvent('model.beforeCreate', function () use ($model) {
            //     $model->rules = ['floor' => 'required'];
            // });
        });

Last updated

mjauvin
mjauvin

I see the validation rule, not the field definition

jimlab56999
mjauvin
jimlab56999
jimlab56999

O sorry don`t catch what you mean

 public function boot()
    {
        RealtyModel::extend(function ($model) {
            $model->hasOne['addproperties'] = ['Eduard\AddProperties\Models\AddProperties'];
            $model->rules = ['floor' => 'required'];
            // $model->bindEvent('model.beforeCreate', function () use ($model) {
            //     $model->rules = ['floor' => 'required'];
            // });
        });

        RealtyControllers::extendListColumns(function ($list, $model) {
            if (!$model instanceof RealtyModel) {
                return;
            }
            $list->addColumns([
                'building' => [
                    'label' => 'Building',
                ],
            ]);
        });

        RealtyControllers::extendFormFields(function ($form, $model, $context) {
            if (!$model instanceof RealtyModel) {
                return;
            }
            if (!$model->exists) {
                return;
            }

            AddPropertiesModel::getFromRealty($model);

            $form->addTabFields([
                'addproperties[floor]' => [
                    'label' => 'Floor',
                    'tab' => 'Standart Property',
                    'type' => 'number',
                    'span' => 'storm',
                    'cssClass' => 'col-xs-2',
                    'required' => true,
                ],
               //cut some fields...
           ]);
        });
    }
mjauvin
mjauvin

You're adding a validation rule for a field that has not been defined... unless it's already in the Model you're extending?

jimlab56999
jimlab56999

Ok, how should I define this fields in the Model?
Please give me an example or link
I have no idea from the docs...

jimlab56999
jimlab56999

I found this

$model->addFillable(['floor']);

but it doesn't help
mjauvin could you please give me advise

mjauvin
mjauvin

As Luke Towers pointed out on Slack, you are defining the validation rules in the wrong model...

Since floor is a property of the AddProperties Model, that's where you need to set the rule.

mjauvin
mjauvin
AddPropertiesModel::extend(function ($model) {
   $model->rules['floor'] = 'required';
});
jimlab56999
jimlab56999

Thank you this works fine. I thought only about extending primary model :)

1-15 of 15

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