saluei's avatar

get list of all old value in *.blade.php

{{old('element_name'}} command return the old value of element in page , so in form validation if validation failed I can show the user , value entered and form element value remain.

how i could get the list of all old value in form ? may be something like this (old function without parameter)

<script>

var list_of_all_old_value = {{  old()  }};

</script>

0 likes
7 replies
saluei's avatar

thank's jlrdw I have java script function that get the list of all select box on the form with options of each select box and fill all select box , in addition that function get the model object from controller (*.php) to select proper option in each select box, problem is when user change the one select box options to something else if validation failed , keep selected option on the form (keep old value), thing is that I write just one function to use in all form , so I want to retrieve the list of all old selected option

$(document).ready(function () {
        var selboxinfo ={!! $SelBoxInfo !!};//key value array [select box name=>[array of option] ]
        var UpdateEntObj ={!! $ModelObj ?? '0' !!};

        for (var sel_box_name in selboxinfo) {
            var selboxvaluelist = selboxinfo[selboxname];
            var dropdown = $("#" + sel_box_name);//find related dropdown in form
            let entitykeyvalue = (UpdateEntObj!=0) ? UpdateEntObj[selboxname] : -1;

            if(entitykeyvalue==-1)
            {
                //if there is no modelObj check old value   
                entitykeyvalue= "{{old(sel_box_name) ?? -1}}";//??? sel_box_name is undefined ERORR
                                                                                                 //sel_box_name is client side variable.
                                                                                                 
                                                                                                 //OK so I want to have list of all old value and search on 
                                                                                                  //them
            }

            dropdown.empty();
            $.each(selboxvaluelist, function (key, value) {
                dropitem=new Option(value,key);
                dropitem.selected=(key==entitykeyvalue);
                dropdown.append(dropitem);
            });
        }
    });








neilstee's avatar

@saluei you should wrapsel_box_name in a single quote like 'sel_box_name':

{{old('sel_box_name') ?? -1}}

that's why it's throwing undefined because it assumes its a variable

saluei's avatar

sel_box_name is variable , but its JavaScript variable at client side

it is in javascript loop and is the name of select box in each loop,

if I have list of all old value my problem solved, thank's

edoc's avatar
edoc
Best Answer
Level 24

how i could get the list of all old value in form ?

try this @saluei

<script>

var list_of_all_old_value = {{  json_encode(session()->getOldInput())  }};

</script>

Please or to participate in this conversation.