Note that there are some explanatory texts on larger screens.

plurals
  1. POunchecked checkbox with "Input::old()"
    primarykey
    data
    text
    <p>I'm creating a checkbox in a view:</p> <pre><code>Form::checkbox('test', 'true', Input::old('test', true)); </code></pre> <p>After unchecking the box, submitting the form and being redirected back:</p> <pre><code>return Redirect::back()-&gt;withInput()-&gt;withErrors($validation); </code></pre> <p>The checkbox is still checked - presumably because <code>'test'</code> doesn't exist in <code>"Input::old"</code> so it is reverting back to the default value.</p> <p>Any suggestions on how best to achieve this?</p> <p><strong>Edit:</strong></p> <p>I've come up with a somewhat hacky way of getting the results I want:</p> <pre><code>$isChecked = true; // The default state // Check if the form has been returned "withInput" if(Input::old()) { $isChecked = Input::old('test', false); // If the field has old data available use that, otherwise it is unchecked } echo Form::checkbox('test', 'true', $isChecked); </code></pre> <p>I'll write this into a function for reuse, but it seems a bit painful so any comments/suggestions would be great</p> <p><strong>Edit 2</strong> Sorry if not clear - to clarify:</p> <ol> <li>Form is loaded with checkbox checked</li> <li>I uncheck the checkbox and submit the form</li> <li>The form doesn't validate so I'm returned to the form</li> <li>The checkbox has re-checked itself - I would expect it to be unchecked, as per the user submitted data</li> </ol> <p>I assume this happens because if a checkbox isn't checked the element isn't included in the post data</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload