Skip to content

Commit

Permalink
Merge pull request #38 from lakerobin/master
Browse files Browse the repository at this point in the history
Fixes "required" attribute bug
  • Loading branch information
netojose authored Sep 22, 2018
2 parents 1df01f4 + e987540 commit 5d14f8d
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
roadmap.md

/.idea
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,34 @@ Using locale, the package will look for a resources/lang/{CURRENT_LANG}/forms/us
{!!Form::text('name', 'Name')->required(false)!!}
```

### AutoFill

| Param | Type | Default | Description |
| ------- | ------ | ------- | ---------------- |
| $value | string | 'on' | autocomplte value|

see: https://html.spec.whatwg.org/multipage/forms.html#autofill

If no autocomplete value is specified on the form, html spec requires
a default value of 'on'. So, you must explicitly turn it off.

Autocomplete values will be automatically generated for fields with
single word names matching valid values (e.g. name, email, tel, organization). The
complete list is in the spec mentioned above.

```php
// Examples

// Switch off autocomplete for the form
{!!Form::open()->autocomplete('off')!!}

// Explicitly set a autocomplete value
{!!Form::text('mobile', 'Mobile Number')->autocomplete('tel')!!}

// Disable autocomplete for fields with valid names
{!!Form::text('name', 'Name')->autocomplete('off')!!}
```

### Id

| Param | Type | Default | Description |
Expand Down
79 changes: 73 additions & 6 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@

class FormBuilder {

/**
* List of allowed single word
* values for autocomplete attribute
*
* @var array
*/
private $_allowedAutoComplete;

/**
* Form input labels locale
*
* @var string
*/
private $_Flocale;

/**
* Form inline form flag
*
Expand Down Expand Up @@ -45,6 +53,18 @@ class FormBuilder {
*/
private $_FidPrefix;

/**
* Form autocomplete attribute
* @var string|null
*/
private $_Fautocomplete;

/**
* Input autocomplete attribute
* @var string|null
*/
private $_autocomplete;

/**
* Input meta data
*
Expand Down Expand Up @@ -189,6 +209,18 @@ public function __construct()
{
$this->_resetFlags();
$this->_resetFormFlags();

//All allowed single word autocomplete values. If the name of input field matches,
//they will be automatically set as autocomplete value
//Flip the array to make searches faster
$this->_allowedAutoComplete = array_flip(['name', 'honorific-prefix', 'given-name', 'additional-name',
'family-name', 'honorific-suffix', 'nickname', 'username', 'new-password', 'current-password',
'organization-title', 'organization', 'street-address', 'address-line1', 'address-line2', 'address-line3',
'address-level4', 'address-level3', 'address-level2', 'address-level1', 'country', 'country-name',
'postal-code', 'cc-name', 'cc-given-name', 'cc-additional-name', 'cc-family-name', 'cc-number', 'cc-exp',
'cc-exp-month', 'cc-exp-year', 'cc-csc', 'cc-type', 'transaction-currency', 'transaction-amount', 'language',
'bday', 'bday-day', 'bday-month', 'bday-year', 'sex', 'url', 'photo', 'tel', 'tel-country-code', 'tel-national',
'tel-area-code', 'tel-local', 'tel-local-prefix', 'tel-local-suffix', 'tel-extension', 'email', 'impp']);
}

/**
Expand Down Expand Up @@ -233,6 +265,10 @@ public function open(): string
$props['class'] = 'form-inline';
}

if (!is_null($this->_Fautocomplete)) {
$props['autocomplete'] = $this->_Fautocomplete;
}

$attrs = $this->_buildAttrs($props, ['class-form-control']);

$ret = '<form ' . $attrs . '>';
Expand Down Expand Up @@ -349,6 +385,26 @@ public function email(): string
return $this->_renderInput('email');
}

/**
* Return a tel input tag
*
* @return string
*/
public function tel(): string
{
return $this->_renderInput('tel');
}

/**
* Return a url input tag
*
* @return string
*/
public function url(): string
{
return $this->_renderInput('url');
}

/**
* Return a number input tag
*
Expand Down Expand Up @@ -562,6 +618,7 @@ private function _getLabel(): string
* Return a string with HTML element attributes
*
* @param array $props
* @param array $ignore
* @return string
*/
private function _buildAttrs(array $props = [], array $ignore = []): string
Expand All @@ -571,7 +628,13 @@ private function _buildAttrs(array $props = [], array $ignore = []): string

$props['type'] = $this->_type;
$props['name'] = $this->_name;
$props['autocomplete'] = $props['name'];

if (!is_null($this->_autocomplete)) {
$props['autocomplete'] = $this->_autocomplete;
} else if (isset($this->_allowedAutoComplete[$props['name']])) {
$props['autocomplete'] = $props['name'];
}

$props['id'] = $this->_getId();
$props['class'] = isset($props['class']) ? $props['class'] : '';

Expand All @@ -587,6 +650,10 @@ private function _buildAttrs(array $props = [], array $ignore = []): string
$props['aria-describedby'] = $this->_getIdHelp();
}

if($this->_required === true) {
$props['required'] = "required";
}

switch($this->_type) {
case 'file':
$formControlClass = 'form-control-file';
Expand All @@ -599,8 +666,6 @@ private function _buildAttrs(array $props = [], array $ignore = []): string
break;
}

$formControlClass = $this->_type == 'file' ? 'form-control-file' : 'form-control';

if (!$props['class'] && !in_array('class-form-control', $ignore)) {
$props['class'] = $formControlClass;
}
Expand Down Expand Up @@ -794,7 +859,7 @@ private function _renderCheckboxOrRadio(): string
/**
* Return a input with a wrapper HTML markup
*
* @param type $field
* @param string $field
* @return string
*/
private function _renderWarpperCommomField(string $field): string
Expand All @@ -820,7 +885,7 @@ private function _renderWarpperCommomField(string $field): string
*
* @param string $prefix
* @param string $sufix
* @return string|mull
* @return string|null
*/
private function _getValidationFieldMessage(string $prefix = '<div class="invalid-feedback">', string $sufix = '</div>')
{
Expand Down Expand Up @@ -863,6 +928,7 @@ private function _resetFlags()
$this->_block = false;
$this->_value = null;
$this->_multiple = false;
$this->_autocomplete = null;
}

/**
Expand All @@ -877,6 +943,7 @@ private function _resetFormFlags()
$this->_FinlineForm = false;
$this->_Fdata = null;
$this->_FidPrefix = '';
$this->_Fautocomplete = null;
}

}
Loading

0 comments on commit 5d14f8d

Please sign in to comment.