mercredi 25 janvier 2017

Laravel old() for checkbox

I have created a checkToggle() method so I can use in the view file.

It is not working as expected.

if (! function_exists('checkToggle')) {
    function checkToggle($data, $name, $checked = false)
    {
        if ($checked) {
            return old($name, (isset($data) && $data[$name] == true) ? 'checked' : (isset($data) ? '' : 'checked'));
        }

        return old($name, (isset($data) && $data[$name] == true) ? 'checked' : '');
    }
}

Html:

<input type="checkbox"  name="has_email" id="has_email" value="1"/>

If user press submit and validation failed, it should repopulate to be checked or not. It will also check if $data[$name] is exist from the database. However, if it does exist from the database (value: 1) and user unchecked from the page and submit a page - if validation failed - it should stay unchecked.

The third parameter checkToggle($item, 'has_email', true) mean it should be checked by default (before pressing submit) and $data[$name] would not be exist (for create page)



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ksjYX2
via IFTTT

Aucun commentaire:

Enregistrer un commentaire