Chihuahua Enthusiast
Chihuahua Enthusiast

Reputation: 1580

CakePHP - Ignore form Input Field with Name Attribute

I have a CakePHP form with radio buttons that enhance the frontend by toggling form sections on and off. To make the radio buttons work, I need a name attribute that binds them together.

However, my problem is that CakePHP POSTs any input field with a name attribute, so the radio button above causes errors when submitting the form because I don't have a toggle-0 field in my model.

Is there a way to prevent CakePHP from posting the radio button values? Can I achieve this in the frontend or backend?

This seems like a very simple thing to do (Like in C# MVC) but I can't seem to find any information on the Cake Cookbook.

Relevant Radio Button:

<input id="toggle-on-0" class="selection-toggle selection-toggle-left" name="toggle-0" value="false" type="radio">
<label for="toggle-on-0" class="selection-btn">On</label>
<input id="toggle-off-0" class="selection-toggle selection-toggle-right" name="toggle-0" value="true" type="radio">
<label for="toggle-off-0" class="selection-btn">Off</label>

Let me know if I can clarify the question or if you need the code for my add() method.

Upvotes: 0

Views: 593

Answers (1)

Sher Afgan
Sher Afgan

Reputation: 1234

It's not a CakePHP issue. By using both the value and name attribute you have made your radio control successful. As mentioned in the HTML specification, radio input will now be a part of form data, and will be sent to the action on form submission.

Solutions:

1: Your only solution on the server side is to remove radio input data from request object if it's present.

2: Remove the name attribute and change your frontend logic.

3: Send AJAX request with only required POST data.

Upvotes: 1

Related Questions