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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@bfavaretto this will not add a wrapping div since 'div' is set to false.
@BorislavSabev I said that before Oldskool added that to the answer.
@bfavaretto ok, ok. No hard feelings man. Just clarifying for the other guy that's going to look at this in the future. ;)
@BorislavSabev No problem! I removed that comment to avoid further confusion.
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.

2 Comments

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.
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.