Skip to main content

UpdateFlowRequest

Form request class for validating Flow update data. This request handles partial updates with context-aware validation, ensuring translation uniqueness while allowing selective field updates.

Namespace

JobMetric\Flow\Http\Requests\Flow\UpdateFlowRequest

Overview

The UpdateFlowRequest validates incoming data when updating an existing Flow entity. Unlike StoreFlowRequest, all fields are optional (sometimes), allowing partial updates while maintaining data integrity.

Key Features

  • Partial Updates: All fields are optional, allowing selective updates
  • Context-Aware Validation: Uses flow_id from context to validate uniqueness
  • Static Rules Method: Provides rulesFor() for programmatic validation
  • Translation Uniqueness: Validates translation names excluding current flow

Validation Rules

Optional Fields

All fields use sometimes rule, meaning they're only validated if present:

FieldRuleDescription
translationsometimes|arrayMulti-language translation data
translation.{locale}sometimes|arrayTranslation data for specific locale
translation.{locale}.namerequired|stringFlow name (validated if locale provided)
subject_typesometimes|string|max:255Model class name
subject_scopesometimes|nullable|string|max:255Scope identifier
subject_collectionsometimes|nullable|string|max:255Collection identifier
versionsometimes|integer|min:1Version number
is_defaultsometimes|booleanDefault flow flag
statussometimes|booleanActive/inactive status
active_fromsometimes|nullable|dateStart date
active_tosometimes|nullable|dateEnd date
channelsometimes|nullable|string|max:64Channel identifier
orderingsometimes|integer|min:0Display order
rollout_pctsometimes|nullable|integer|between:0,100Rollout percentage
environmentsometimes|nullable|string|max:64Environment identifier
translation.{locale}.descriptionnullable|stringOptional description

Context Management

The request supports external context injection via setContext():

$request = new UpdateFlowRequest();
$request->setContext(['flow_id' => 123]);

This allows validation to exclude the current flow from uniqueness checks.

Static Rules Method

rulesFor()

Provides programmatic validation without instantiating the request:

$input = [
'translation' => [
'en' => ['name' => 'Updated Name'],
],
'status' => false,
];

$context = ['flow_id' => 123];

$rules = UpdateFlowRequest::rulesFor($input, $context);

Parameters:

  • array $input: Input data to validate
  • array $context = []: Context data (e.g., flow_id)

Returns: array Validation rules

Cross-Field Validation

Same as StoreFlowRequest, validates that active_from is before or equal to active_to:

// ✅ Valid
'active_from' => '2024-01-01',
'active_to' => '2024-12-31',

// ❌ Invalid
'active_from' => '2024-12-31',
'active_to' => '2024-01-01',

Translation Validation

Name Uniqueness with Context

When updating translation names, the validation excludes the current flow:

// If updating flow ID 123
// The name "My Flow" is valid even if another flow has it
// UNLESS flow 123 already has "My Flow" in that locale

The TranslationFieldExistRule uses the flow_id from context to exclude the current record.

Usage Examples

Basic Update

use JobMetric\Flow\Http\Requests\Flow\UpdateFlowRequest;
use JobMetric\Flow\Facades\Flow;

public function update(UpdateFlowRequest $request, $id)
{
$validated = $request->validated();

$flow = Flow::update($id, $validated);

return response()->json($flow);
}

Partial Update - Status Only

$requestData = [
'status' => false, // Deactivate flow
];

Partial Update - Translation Only

$requestData = [
'translation' => [
'en' => [
'name' => 'Updated Workflow Name',
'description' => 'New description',
],
],
];

Update Multiple Fields

$requestData = [
'translation' => [
'en' => ['name' => 'Updated Name'],
],
'status' => true,
'is_default' => false,
'ordering' => 5,
'rollout_pct' => 50,
];

Update Active Window

$requestData = [
'active_from' => '2024-06-01',
'active_to' => '2024-08-31',
];

Using Static Method

use JobMetric\Flow\Http\Requests\Flow\UpdateFlowRequest;

// Validate programmatically
$input = ['status' => false];
$context = ['flow_id' => 123];

$rules = UpdateFlowRequest::rulesFor($input, $context);

// Use with validator
$validator = Validator::make($input, $rules);
if ($validator->fails()) {
// Handle errors
}

Context Injection

In Controller

public function update(UpdateFlowRequest $request, $id)
{
// Context is automatically set by Flow service
// But you can also set it manually:
$request->setContext(['flow_id' => $id]);

$flow = Flow::update($id, $request->validated());

return response()->json($flow);
}

In Service Layer

use JobMetric\Flow\Http\Requests\Flow\UpdateFlowRequest;

public function updateFlow($id, array $data)
{
$request = new UpdateFlowRequest();
$request->setContext(['flow_id' => $id]);
$request->merge($data);

$validated = $request->validated();

// Update flow...
}

Error Handling

Validation Errors

{
"message": "The given data was invalid.",
"errors": {
"translation.en.name": [
"A flow with this name already exists."
],
"active_from": [
"The active from must be before active to."
]
}
}

Handling in Controllers

public function update(UpdateFlowRequest $request, $id)
{
try {
$flow = Flow::update($id, $request->validated());
return response()->json($flow);
} catch (\Illuminate\Validation\ValidationException $e) {
return response()->json([
'message' => 'Validation failed',
'errors' => $e->errors()
], 422);
}
}

Differences from StoreFlowRequest

FeatureStoreFlowRequestUpdateFlowRequest
Field Rulesrequired for translationssometimes for all fields
Context SupportNoYes (flow_id in context)
Static MethodNoYes (rulesFor())
Uniqueness CheckChecks all flowsExcludes current flow
Use CaseCreating new flowsUpdating existing flows

Best Practices

  1. Use Context for Uniqueness: Always provide flow_id in context

    $request->setContext(['flow_id' => $id]);
  2. Validate Only Changed Fields: Only send fields that need updating

    // ✅ Good - Only update what changed
    ['status' => false]

    // ❌ Bad - Sending all fields
    ['translation' => [...], 'status' => false, 'ordering' => 0, ...]
  3. Handle Partial Translations: Only provide locales you're updating

    // ✅ Good - Only update English
    ['translation' => ['en' => ['name' => 'New Name']]]
  4. Validate Date Ranges: Ensure logical date ranges when updating

    // ✅ Good
    ['active_from' => '2024-01-01', 'active_to' => '2024-12-31']