PHP code example of carlos-andres / nova-html-field

1. Go to this page and download the library: Download carlos-andres/nova-html-field library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

carlos-andres / nova-html-field example snippets


use Vendor\NovaHtmlField\HtmlField;

// Static content
HtmlField::make('Notice')
    ->content('<p style="color: #059669;">Settings saved successfully</p>');

// Dynamic from model
HtmlField::make('Preview')
    ->html(fn ($model) => '<strong>'.e($model->title).'</strong>');

// From model attribute
HtmlField::make('Description', 'html_content');

HtmlField::make('Info Banner')
    ->content('
        <div style="background: #dbeafe; border-radius: 8px; padding: 16px;">
            <h3 style="margin: 0 0 4px 0; font-weight: 600; color: #1e40af;">
                Configuration
            </h3>
            <p style="margin: 0; font-size: 14px; color: #3b82f6;">
                Manage your settings below
            </p>
        </div>
    ')
    ->onlyOnForms();

// Status badge that changes based on model state
HtmlField::make('Status')
    ->html(fn ($model) => '
        <span style="
            background: '.($model->is_active ? '#d1fae5' : '#fee2e2').';
            color: '.($model->is_active ? '#065f46' : '#991b1b').';
            padding: 4px 12px;
            border-radius: 9999px;
            font-size: 12px;
            font-weight: 500;
        ">
            '.($model->is_active ? 'Active' : 'Inactive').'
        </span>
    ')
    ->onlyOnIndex();

// Image preview
HtmlField::make('Thumbnail')
    ->html(fn ($model) => $model->image_url
        ? '<img src="'.e($model->image_url).'" style="max-width: 100px; border-radius: 4px;">'
        : '<span style="color: #9ca3af;">No image</span>'
    );

// Direct attribute (sanitized automatically)
HtmlField::make('Body', 'html_content');

// With transform callback
HtmlField::make('Formatted', 'raw_content', function ($value) {
    return '<div style="white-space: pre-wrap;">'.e($value).'</div>';
});

HtmlField::make('Details')
    ->content('<p>Only visible on detail view</p>')
    ->onlyOnDetail();

HtmlField::make('Summary')
    ->html(fn ($m) => $m->summary_html)
    ->showOnIndex()
    ->hideFromDetail();

HtmlField::make('Form Help')
    ->content('<p style="color: #6b7280;">Fill in all 

// Show only for admins
HtmlField::make('Admin Panel')
    ->content('<div>Admin-only content</div>')
    ->when(fn ($request) => $request->user()->isAdmin());

// Hide for admins
HtmlField::make('User Notice')
    ->content('<p>Contact admin for changes</p>')
    ->unless(fn ($request) => $request->user()->isAdmin());

HtmlField::make('Alert')
    ->content('
        <div style="
            background: #fef3c7;
            border: 1px solid #f59e0b;
            border-radius: 8px;
            padding: 12px 16px;
            display: flex;
            align-items: center;
            gap: 8px;
        ">
            <span style="font-size: 18px;">Warning</span>
            <span style="color: #92400e;">Please review before saving</span>
        </div>
    ');

// This may NOT work (classes might not exist in Nova's CSS)
HtmlField::make('Card')
    ->content('<div class="bg-blue-100 p-4 rounded-lg">Hello</div>');

// This WILL work (inline styles always render)
HtmlField::make('Card')
    ->content('<div style="background: #dbeafe; padding: 16px; border-radius: 8px;">Hello</div>');

// Using emoji (works)
HtmlField::make('Files')
    ->content('<h3>📁 Files Section</h3>');

HtmlField::make('Images')
    ->content('<h3>🖼️ Images Section</h3>');

// SVG will be stripped (won't work without disabling sanitization)
HtmlField::make('Files')
    ->content('<svg>...</svg> Files Section');

// Good - escaped
HtmlField::make('Title')
    ->html(fn ($m) => '<strong>'.e($m->title).'</strong>');

// Bad - XSS vulnerable
HtmlField::make('Title')
    ->html(fn ($m) => '<strong>'.$m->title.'</strong>');

// Only for content you completely control
HtmlField::make('Trusted HTML')
    ->html(fn ($m) => $m->trusted_html)
    ->withoutSanitization();

// Restrict allowed elements
HtmlField::make('Simple')
    ->html(fn ($m) => $m->html)
    ->purifierConfig([
        'HTML.Allowed' => 'p,b,i,a[href]',
    ]);

// Allow target="_blank" on links
HtmlField::make('Links')
    ->html(fn ($m) => $m->html)
    ->purifierConfig([
        'Attr.AllowedFrameTargets' => ['_blank'],
    ]);