orchidsoftware / crud

Simplify the process of building CRUD (Create, Read, Update, Delete) functionality in Laravel using the features of Orchid.
https://orchid.software
MIT License
137 stars 34 forks source link

Setting Value() on "select" fields that are called "active" doesn't work #41

Closed ozthegreat closed 3 years ago

ozthegreat commented 3 years ago

Hey. I have the following code for creating a select field. However no matter what I do it always defaults the "disabled". However, as soon as I change then name from 'active' to anything else it works just fine. I haven't dug too far into the code but I suspect there's an attribute called 'active' that might be messing it up or something. Thanks.

  Select::make('active')
      ->title('Status')
      ->options([
          'enabled'  => 'Enabled',
          'disabled' => 'Disabled',
      ])
      ->value('enabled'),
ozthegreat commented 3 years ago

Ok little update. The problem is I have a getActiveAttribute() method set on the model which returns "disabled" by default. I'm guessing Orchid CRUD initiates an empty model and is using that value for the select and it's overriding the one we implicitly set with ->value() if that makes sense.

tabuna commented 3 years ago

I'm guessing Orchid CRUD initiates an empty model and is using that value for the select and it's overriding the one we implicitly set with ->value() if that makes sense.

Hi @ozthegreat. We create an empty model so that the default data is displayed:

class Post extends Model
{
    $attributes = [
        'price' => 0.0,
    ];
}

When you go to the creation page, the value will be substituted in the field with the same name.

Input::make('price')

The problem is I have a getActiveAttribute() method set on the model, which returns "disabled" by default.

Yes, modifier functions work the same way. It was intended that way, and there is no mistake about it.

ozthegreat commented 3 years ago

thanks for replying so quickly! Ok I think I'm with you. So on the model, if we had this?

getPriceAttribute($value) {
   return '555';
}

Then created the form like thus:

Input::make('price')->value('999');

Empty forms will always show "555"? And there's no way to override that with the form builder?

Edit. As a workaround what I've done is this, which seems to work ok:

getPriceAttribute($value) {
if ( is_null($value) ) {
    return '999';
}
   return '555';
}
tabuna commented 3 years ago

It works like this. You are creating some object:

$object = Input::make('price')->value('999');

At this point, the value that is there is 999. But then the values are bound from the model. This is hidden and is not written explicitly:

$object->value($model->attribute)

Accordingly, then this value will be the same asset in the model.

And there's no way to override that with the form builder?

There are you can use events, for example:

Input::make('price')->addBeforeRender(function () {
    $this->value(999)
});

This will overwrite the value before displaying. But I highly recommend defining such things in the model and not through an event.

ozthegreat commented 3 years ago

Perfect thanks. That's exactly what I want. Yeah unfortunately in this particular case I want the form value to be different to the default model value. Thanks very much for your help!!