nova-polymorphic-field icon indicating copy to clipboard operation
nova-polymorphic-field copied to clipboard

rules('required') is ignored

Open horaceho opened this issue 6 years ago • 8 comments

When saving a PolymorphicField::make('Type') with a Text::make('Client')->rules('required') field, the rules('required') is ignored...

horaceho avatar Oct 20 '18 03:10 horaceho

Possibly the solution in the Nova Dependency Container could resolve this issue? https://github.com/epartment/nova-dependency-container/issues/15

jasonlav avatar Nov 03 '18 01:11 jasonlav

The solution used by nova-dependency-container suggested by @jasonlav seems to work for me. I just create a custom HasPolymorphicFields trait and use it instead of the one used by the package.

UPDATE This solution DOESN'T works. Validation rules are correctly applied but the store fails because Nova try to store the polymorphic fields also in the resource that contains them.

trait HasPolymorphicFields
{
    /**
     * @param NovaRequest $request
     * @return FieldCollection
     */
    public function availableFields(NovaRequest $request)
    {
        $fields = $this->fields($request);
        $availableFields = [];

        foreach ($fields as $field) {
            if ($field instanceof PolymorphicField) {
                $availableFields[] = $field;

                foreach ($field->meta['types'] as $type) {
                    if ($this->doesRouteRequireChildFields()) {
                        $availableFields = array_merge($availableFields, $type['fields']);
                    }
                }
            } else {
                $availableFields[] = $field;
            }
        }

        return new FieldCollection(array_values($this->filter($availableFields)));
    }

    /**
     * @return bool
     */
    protected function requestIsAssociateRequest() : bool
    {
        return ends_with(Route::currentRouteAction(), 'AssociatableController@index');
    }

    /**
     * @return bool
     */
    protected function doesRouteRequireChildFields() : bool
    {
        return ends_with(Route::currentRouteAction(), 'AssociatableController@index')
            || ends_with(Route::currentRouteAction(), 'ResourceStoreController@handle')
            || ends_with(Route::currentRouteAction(), 'ResourceUpdateController@handle');
    }
}

aviuz80 avatar Nov 16 '18 09:11 aviuz80

+1

jonathandemoor avatar Dec 19 '18 09:12 jonathandemoor

I created a PR to fix validation problems for Nova Dependency Container . https://github.com/epartment/nova-dependency-container/pull/37

Hope that helps.

tufankilicaslan avatar Dec 25 '18 11:12 tufankilicaslan

+1

carlosjac avatar Dec 29 '18 02:12 carlosjac

+1

HUN-Julian avatar Apr 05 '19 05:04 HUN-Julian

What about this issue? It still exists.

david-kristin avatar Mar 03 '20 19:03 david-kristin

+1 - Still an issue.

marsdk avatar Apr 15 '20 11:04 marsdk