2

I am using cakephp form helper and I want to set the input attribute autoFocus. The best I can get is autoFocus="autoFocus" which is not valid. I need to set it to just autoFocus.

<input type="text" name="fname" autofocus>

My code is:

echo $this->Form->input('title', array('class'=>'form-control','autofocus'));
Keith Power
  • 13,891
  • 22
  • 66
  • 135
  • 1
    @TommyDo answer is correct, as said by [dereuromark](http://www.dereuromark.de/2012/03/01/some-new-crazy-cakephp-tricks/#autofocus), part of CakePHP dev team. – Choma Feb 06 '15 at 12:38
  • @Choma: thank for it. I am just begin to work with CakePHP. – TommyDo Feb 09 '15 at 07:59

1 Answers1

3

Try this

<?php
    echo $this->Form->create('Staff', array('action' => 'login'));
    echo $this->Form->inputs(array(
        'legend' => __(''),
        'username' => array('autofocus'=>'autofocus'),
        'password'
    ));
    echo $this->Form->end('Login');
?>
TommyDo
  • 663
  • 8
  • 23
  • 1
    The question is labeled for cakephp-2.5, but I've just verified that `['autofocus' => 'autofocus']` also works for 3.3.3 – Kyle Challis Nov 04 '16 at 17:22
  • It's simply add autofocus HTML attribute to HTML tag. So I think it will work for every version. Thank for you verified. – TommyDo Nov 09 '16 at 09:12
  • You can also just set `'autofocus' => true` and cake will do the rest (since Cake 1.2) – elboletaire Jul 04 '17 at 20:09