Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel 4 form inputs for Number and Telephone
<?php
/***
* If you prefer automatic updates or using packages, this is available as a composer
* package: https://github.com/smalldogs/html5inputs
*
* Thank you to @silentcoast for the adding the support of non-valued attributes (such as `required`)
*
* This file allows you to use the other 11 HTML elements the same way as text, email, and URL.
*
* 1. Create the folder /app/misc, or use your preferred directory
* 2. Copy this form_macros.php into the directory
* 3. Include this file with `require app_path().'/misc/form_macros.php';` or in composer.json
* "autoload": {
* "psr-0": {
* "Helpers": "app/misc"
* }
* }
*
* This file manually creates each macro, allowing you to customize any given input to add defaults
* or restrictions.
*
* *Note: The only alias which differs from the element input type is datetime-local, as the dash breaks
* the alias. Instead, create the element using Input::datetimelocal()
***/
/**
* Color input - http://www.w3.org/TR/html-markup/input.color.html
*
* The input element with a type attribute whose value is "color" represents a color-well control, for
* setting the element’s value to a string representing a simple color.
*
* Value - A string exactly seven characters long, consisting of the following parts, in exactly the
* following order:
* A "#" character.
* Six characters in the range 0–9, a–f, and A–F.
*
* Note - Color keywords (for example, strings such as “red” or “green”) are not allowed.
*/
Form::macro('color', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="color" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
/**
* Date input - http://www.w3.org/TR/html-markup/input.date.html
*
* The input element with a type attribute whose value is "date" represents a control for setting the
* element’s value to a string representing a date.
*
* Value - A valid full-date as defined in [RFC 3339], with the additional qualification
* that the year component is four or more digits representing a number greater than 0.
*/
Form::macro('date', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="date" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
/**
* Datetime input - http://www.w3.org/TR/html-markup/input.datetime.html
*
* The input element with a type attribute whose value is "datetime" represents a control
* for setting the element’s value to a string representing a global date and time (with
* timezone information).
*
* Value - A valid date-time as defined in [RFC 3339], with these additional qualifications:
* 1. The literal letters T and Z in the date/time syntax must always be uppercase
* 2. The date-fullyear production is instead defined as four or more digits representing a
* number greater than 0
*/
Form::macro('datetime', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="datetime" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
/**
* Datetime-local input - http://www.w3.org/TR/html-markup/input.datetime-local.html
*
* The input element with a type attribute whose value is "datetime-local" represents a control
* for setting the element’s value to a string representing a local date and time (with no
* timezone information).
*
* Value - A string representing a local date and time. The following parts, in exactly the
* following order:
* 1. A date.
* 2. The literal string "T".
* 3. A time.
*/
Form::macro('datetimelocal', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="datetime-local" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
/**
* Month input - http://www.w3.org/TR/html-markup/input.month.html
*
* The input element with a type attribute whose value is "month" represents a control for
* setting the element’s value to a string representing a month.
*
* Value - A string representing a month. The following parts, in exactly the following order:
* 1. Four or more digits representing a number greater than 0.
* 2. The literal string "-".
* 3. Two digits, representing the month month, in the range 1 ≤ month, ≤ 12.
*/
Form::macro('month', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="month" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
/**
* Number input - http://www.w3.org/TR/html-markup/input.number.html
*
* The input element with a type attribute whose value is "number" represents a precise control for
* setting the element’s value to a string representing a number.
*
* Value - A string representing a number.
*/
Form::macro('number', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="number" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
/**
* Range input - http://www.w3.org/TR/html-markup/input.range.html
*
* The input element with a type attribute whose value is "range" represents an imprecise control for
* setting the element’s value to a string representing a number.
*
* Value - A string representing a number.
*/
Form::macro('range', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="range" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
/**
* Search input - http://www.w3.org/TR/html-markup/input.search.html
*
* The input element with a type attribute whose value is "search" represents a one-line plain-text
* edit control for entering one or more search terms.
*
* Value - Any string that contains no line feed (U+000A, “LF”) or carriage return (U+000D, “CR”)
* characters.
*/
Form::macro('search', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="search" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
/**
* Tel input - http://www.w3.org/TR/html-markup/input.tel.html
*
* The input element with a type attribute whose value is "tel" represents a one-line plain-text edit
* control for entering a telephone number.
*
* Value - Any string that contains no line feed (U+000A, “LF”) or carriage return (U+000D, “CR”)
* characters.
*/
Form::macro('tel', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="tel" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
/**
* Time input - http://www.w3.org/TR/html-markup/input.time.html
*
* The input element with a type attribute whose value is "time" represents a control for setting the
* element’s value to a string representing a time (with no timezone information).
*
* Value - A string representing a time (with no timezone information). A valid partial-time as defined
* in [RFC 3339].
*/
Form::macro('time', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="time" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
/**
* Week input - http://www.w3.org/TR/html-markup/input.week.html
*
* The input element with a type attribute whose value is "week" represents a control for setting the
* element’s value to a string representing a week.
*
* Value - A string representing a week. The following parts, in exactly the following order:
* 1. Four or more digits representing year year, where year > 0.
* 2. The literal string "-W".
* 3. Two digits, representing the week week, in the range 1 ≤ week ≤ maxweek, where maxweek is either
* 52 or 53, depending on the particular year.
*/
Form::macro('week', function($name, $default = NULL, $attrs = array())
{
$item = '<input type="week" name="'. $name .'" ';
if ($default) $item .= 'value="'. $default .'" ';
// If Id not explicitly set, use name (Id is needed to associate with labels)
if (! isset($attrs['id'])) $item .= 'id="' . $name .'" ';
if (is_array($attrs))
{
foreach ($attrs as $a => $v)
{
$item .= ($a !== 0 ? $a . '="' : null) . $v .'" ';
}
}
$item .= ">";
return $item;
});
@austin-reynolds-10

This comment has been minimized.

Copy link

commented Jun 26, 2014

In each of the foreach loops that list out the attributes I have changed mine to this:

foreach ($attrs as $a => $v){
        if($a === 0)
            $item .= $v . ' ';
        else
            $item .= $a .'="'. $v .'" ';
}

This way we can pass in things such as just 'required'.

@piotr-cz

This comment has been minimized.

Copy link

commented Jun 30, 2014

We could probably utilize native method using Facade

return  '<input' . HTML::attributes($attrs) . '/>';
@sergiopvilar

This comment has been minimized.

Copy link

commented Jul 16, 2014

The file is not automatically loaded as said in the post, what I can do to fix this?

@rubinoske

This comment has been minimized.

Copy link

commented Jul 17, 2014

add require app_path().'/misc/form_macros.php'; to the global.php file. probably not the best solution but it worked for me

@brwnll

This comment has been minimized.

Copy link
Owner Author

commented Nov 20, 2014

Sorry for the silence. I assumed GitHub would notify me if people were commenting on here, apparently it does not.

Thanks @silentcoast, I've implemented your update, good idea.

Sorry @sergiovilar, @rubinoske is right, I missed a step. Including it in your global.php file is probably easiest, but you can also load it via composer (added to the notes in the head of the file).

I've also created a package for this to easy install, which is now available at https://github.com/smalldogs/html5inputs.

@ghost

This comment has been minimized.

Copy link

commented Dec 15, 2014

The elements should automatically set their ID attributes, both to follow the pattern established by the other Laravel elements, and to ensure that the Form::labels work as intended. That said, this file has really saved me some serious annoyance.

@brwnll

This comment has been minimized.

Copy link
Owner Author

commented Dec 15, 2014

Good point @ajseidl. I've updated the file to use then element's name also as the Id if the Id is not explicitly defined in the attributes array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.