Skip to content

Latest commit

 

History

History
111 lines (79 loc) · 2.77 KB

File metadata and controls

111 lines (79 loc) · 2.77 KB

Quick start

Validate markup context against a declared HTTP action intent in a few lines.

Goal

After this guide you can call NativeActionRules::validate() and interpret ValidationResult for navigate, submit, delete, and download intents.

Prerequisites

Installation completed.

1. Pick an intent

use Symfinity\UiAction\ActionIntent;

$intent = ActionIntent::Navigate;

Cases: Navigate, Submit, Delete, Download (string values: navigate, submit, delete, download).

2. Describe the markup context

ActionMarkupContext captures the element tag, attributes, and optional form parent — not parsed HTML:

use Symfinity\UiAction\ActionMarkupContext;

$context = new ActionMarkupContext(
    tag: 'a',
    attributes: ['href' => '/reports'],
);

For submit/delete, set form context:

$context = new ActionMarkupContext(
    tag: 'button',
    attributes: ['type' => 'submit'],
    parentIsForm: true,
    formMethod: 'post',
);

3. Validate

use Symfinity\UiAction\NativeActionRules;

$rules = new NativeActionRules();
$result = $rules->validate($intent, $context);

if (!$result->valid) {
    foreach ($result->violations as $violation) {
        echo $violation->code . ': ' . $violation->message . PHP_EOL;
    }
}

Complete example — delete via POST form

<?php

require 'vendor/autoload.php';

use Symfinity\UiAction\ActionIntent;
use Symfinity\UiAction\ActionMarkupContext;
use Symfinity\UiAction\NativeActionRules;

$rules = new NativeActionRules();

$validDelete = $rules->validate(
    ActionIntent::Delete,
    new ActionMarkupContext(
        tag: 'button',
        attributes: ['type' => 'submit'],
        parentIsForm: true,
        formMethod: 'post',
        hasCsrfField: true,
    ),
);

$invalidGetLink = $rules->validate(
    ActionIntent::Delete,
    new ActionMarkupContext('a', ['href' => '/items/1']),
);

assert($validDelete->valid);
assert(!$invalidGetLink->valid);
assert($invalidGetLink->violations[0]->code === 'delete.forbidden_get');

Intent cheat sheet

Intent Valid shape (v0)
Navigate <a href="…">
Submit <button type="submit"> inside <form method="post">
Delete POST form + type="submit" button; no GET link, no _method=DELETE
Download <a href="…" download>

Details: Native action semantics. Violation codes: Validation API.

Next steps