CodeIgniter Forums
Add the correct HTML attributes to the form helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Add the correct HTML attributes to the form helper (/showthread.php?tid=82460)



Add the correct HTML attributes to the form helper - ozornick - 07-13-2022

Does anyone have time for a little change?
It would be correct for the form helper to set the attributes "required",  "disabled",  "autofocus", "readonly"
Basically it's boolean values output as 

Code:
<input type="text" required disabled autofocus readonly>

I think it's worth adding this feature to parse_form_attributes() function.
What for? It is not possible to easily set these options in form_input(), form_textarea(), etc.
Empty values are inserted. For HTML 5 this is wrong

PHP Code:
form_input([
  'name' => 'datebirth',
  'value' => set_value('datebirth'$dateBirth),
  'class' => 'form-control',
  'type' => 'date',
  'disabled' => empty($dateBirth) ? false true,
  'required' => false,
  'readonly' => false
])
// <input type="date" name="datebirth" value="" class="form-control" disabled="" required="" readonly=""> 



RE: Add the correct HTML attributes to the form helper - MGatner - 07-14-2022

I would gladly review a Pull Request from you Smile


RE: Add the correct HTML attributes to the form helper - ozornick - 07-16-2022

I had no experience in open source support  Rolleyes

If no one does it, I'll try it later


RE: Add the correct HTML attributes to the form helper - kenjis - 10-10-2022

It seems this is already fixed.

PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index()
    {
        helper('form');
        echo esc(form_input([
            'name'    => 'datebirth',
            'value'    => '',
            'class'    => 'form-control',
            'type'    => 'date',
            'required' => false,
            'readonly' => false,
        ]));
    }


I see
Code:
<input type="date" name="datebirth" value="" class="form-control" required readonly />

Wait, 'required' => false makes outputting required?

It seems no problem as HTML:
https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes


RE: Add the correct HTML attributes to the form helper - kenjis - 10-10-2022

(07-13-2022, 02:08 AM)ozornick Wrote:
PHP Code:
form_input([
  'name' => 'datebirth',
  'value' => set_value('datebirth'$dateBirth),
  'class' => 'form-control',
  'type' => 'date',
  'disabled' => empty($dateBirth) ? false true,
  'required' => false,
  'readonly' => false
])
// <input type="date" name="datebirth" value="" class="form-control" disabled="" required="" readonly=""> 

Cannot reproduce this.

It seems the feature to show boolean attributes has been since v4.0.0.
https://github.com/codeigniter4/CodeIgniter4/pull/1548/commits/d6fbe3e23675b9ffd2861de22f47a73ebc2af358