form_email()

function form_email(string $name, ?string $value = null, array $attributes = []): string

Description

Generates an HTML email input field with type="email". Provides built-in browser email validation.

Parameters

ParameterTypeDescription
$namestringThe name attribute for the input element.
$valuestring|null(optional) The email address value.
$attributesarray(optional) HTML attributes for the input element. Defaults to an empty array ([]).

Return Value

TypeDescription
stringAn HTML email input element.

Example #1: Basic Email Input

PHP
echo form_email('contact_email', '[email protected]');

// Output:
// <input type="email" name="contact_email" value="[email protected]">

Example #2: Email Input with Validation Attributes

PHP
$attributes = [
    'placeholder' => '[email protected]',
    'required' => 'required',
    'multiple' => 'multiple'
];
echo form_email('email_addresses', '', $attributes);

// Output:
// <input type="email" name="email_addresses" placeholder="[email protected]" required="required" multiple="multiple">

Most browsers provide basic email format validation, but always validate server-side.

  • Use the multiple attribute to allow multiple email addresses separated by commas.
  • Add required attribute for HTML5 client-side validation.
  • Browser validation varies; implement server-side validation with PHP's filter_var($email, FILTER_VALIDATE_EMAIL) or by using the validation module that is included with Trongate.
  • For consistent behavior, always validate email addresses on the server before processing.