4

I am using CakePHP 2.2.4.

I am using the Form Helper to create a form. I need to have a form input with no name attribute.

Is this possible with the formhelper or should I just use HTML for creating this form?

eg in HTML:

<input type="text" maxlength="20" autocomplete="off" class="card-number stripe-sensitive required" />

Basically Am I able to do the above using the formhelper in CakePHP ?

Thanks.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Softinio
  • 638
  • 1
  • 6
  • 19

2 Answers2

11

You can overrule any property in the $options array, which is the second argument to the input() method. So technically you could do:

echo $this->Form->input('Model.field', array(
    'label' => false,
    'div' => false,
    'name' => false,
    'maxlength' => 20,
    'autocomplete' => 'off',
    'class' => 'card-number stripe-sensitive'
));

But please be aware the dropping the name attribute makes the entire field useless if you want to do anything with it's data in your controller/model, as the $this->data array gets it's names from the name attribute of your input fields.

Oldskool
  • 34,211
  • 7
  • 53
  • 66
1

CakePHP needs the name attribute to be able to know what is being submitted by the form. I'm not sure I follow why you would want there to be no name attribute.

If you are concerned that a named input will be passing something to a save method you could always use unset in your controller to remove it from $this->request->data before saving/validation.

Otherwise, you can manually add the markup to your view, but again not sure why you would want an unnamed input element.

drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
  • 1
    Reason I need an unnamed input element is I am using stripe.com for taking online credit card payments using their stripe.js for PCI compliance. For this they require the form you use to collect credit card information to have no input name. – Softinio Jan 16 '13 at 17:44
  • The input name is only needed to *read* regular form submissions. It can be perfectly optional in any other scenario: read-only display, JavaScript manipulation, AJAX submissions, third-party processing... – Álvaro González Dec 14 '18 at 08:24