0

I'm using a Nova action to change a date. To validate the date change, I need to make sure the new date is after the old date. For some reason I'm having a really hard time accessing the old date (which is not a field, just a value attached to the user model). This is what I have:

public function fields()
{
    // Get old date. This works when the action loads.
    $oldShipDate = Carbon::parse($this->user->ship_date)

    return [
        Date::make('New Ship Date', 'new_ship_date')
          // Using withMeta so that calendar pops up with old ship date selected - this works
            ->withMeta(['value' => $oldShipDate])
            ->rules('required','after:' . $oldShipDate),
        }),

        // Other fields

    ];
}

I see in Nova 4.0 they've added a min() option that would fix all of this, but I'm on Nova 3.0. I've also tried a custom validation function and a custom rule, but I still can't access $this->user when the form is submitted. Is it possible to access ship_date on validation somehow? Last resort is to check the new date against the old date in the action logic, but that feels hacky.

1 Answer 1

0

About this type of validation or accessing data from action must you first use single line action only not bulk see below example is work correctly, also it's fix the null

Action File must be like this

class ChangeShipDate extends Action
{
    use InteractsWithQueue, Queueable;

    public function __construct(
        private Order $order,
    )
    {}
    
    
    public function handle(ActionFields $fields, Collection $models):?array
    {
        if(isset($fields->ship_date)){
            $order = $models->first();
            $order->ship_date = $fields->ship_date;
            $order->save();
            return Action::message(__('Ship Date Changed'));
        }
        return null;
    }


    public function fields(NovaRequest $request): array
    {
        if(isset($this->order->ship_date)){
                $oldShipDate = Carbon::parse($this->order?->ship_date);

                return [
                    Date::make('New Ship Date', 'new_ship_date')
                        ->withMeta(['value' => $oldShipDate])
                        ->rules('required','after:' . $oldShipDate),
                    // Other fields
                ];
        }
        return [];
    }


}

In Order Resource must call like this

public function actions(NovaRequest $request): array
{
    return [
        (new ChangeShipDate($this->getModel()))
            ->onlyOnDetail()
            ->onlyOnIndex(),

    ];
}

Note: If you run as multiple row it will not work just must be onDetail and Index to work with singleton action

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.