0

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.

1 Answer 1

1

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.

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

2 Comments

Thank you for your response. The first one seems to be the best solution, although I believe the POST errors on the validator first. I will implement and accept your answer when I get it working.
I've managed to use $this->Form->unlockField('toggle-0'); to prevent validation errors. Then, I remove those fields from $_POST entirely in the controller - so (1) worked.

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.