Laravel4: Form with image-picker

限于喜欢 提交于 2019-12-12 00:42:18

问题


In a site working with Bootstrap + Laravel4. In a form, there are some images that the user can click to select it. It is done with a nice JQuery plugin: http://rvera.github.io/image-picker/

Right now it is written in html and it is working:

            <label><span><i class="icon-check"></i></span> Click on a room to select it</label>
          <select name="rooms[]" multiple="multiple" class="image-picker show-html">
              <option value=""></option>
              <option name="1room" data-img-src="assets/img/rooms/singleRoom.jpg" value="Single room">Single room</option>
              <option name="2room" data-img-src="assets/img/rooms/2room.jpg" value="Double room">Double room</option>
          </select> 

I do not need to rewrite all these markups in Laravel4. But i need one thing: I have to add to Input::old('inputName') to retain the values in case of validation errors. That is very important. I have tried doing this: - set value of the single options to: {{ 'Single room', Input::old('1room') }} - set the value of the select to {{ Input::old('rooms[]') }} But none of them is working. It gives no errors, but the original value is not retained.

Someone knows how can i do it?

Thank you very much!

EDIT:

Printing out the value of Input::old() at the top of the page, the value for rooms are actually retained:

array (size=8)
  'rooms' => 
  array (size=2)
  0 => string 'Single room' (length=11)
  1 => string 'Double room' (length=11)
 'arrival' => string '13/11/2013' (length=10)
 'outgoing' => string '11/11/2013' (length=10)
 'name' => string 'name' (length=4)
 'email' => string 'email' (length=5)
 'phone' => string '9999999999' (length=10)
 'message' => string 'gjhgjghj' (length=8)
 'submit' => string '' (length=0)

The point is how to display them in the form. Precisely how to select some images based on the values contained in that array.

EDIT2:

This is how to understand which options of the select are selected, thanks to @Samuraisoulification:

          <?php  
              if (isset(Input::old()['rooms']))
              {

               for($i = 0 , $input = Input::old()['rooms'] , $c = count($input) ; $i < $c ; $i++)
               {
                 if($input[$i] == "Single room")
                 {
                      echo "Single room selected";
                 }

                 if($input[$i] == "Double room")
                 {
                      echo "Double room selected";
                 }
               }
              }
          ?>

回答1:


This is a very inelegant solution, but should work if I'm correct. In the option tags add:

    <?php  
        for($i = 0 , $input = Input::old()['rooms'] , $c = count($input) ; $i < $c ; $i++){
           if($input[$i] == "<select option's name>"){
                echo "selected";
           }
         }
?>

Note that the $input and $c are not in the comparison part so they will only be evaluated once, saving you a small amount of time(like page load time) on all the options you may have later. This is an inelegant solution, but since you allow multiple values, I think this is the only way to do it. Give a whirl and let me know if it has issues! Also be sure to put this code in each opening tag!

Also I would say condensing this all to one line would look better too.

Lastly, I think because options are different than inputs, the value=="{{Input::old}}" doesn't work, becasue in order for it to be selected you have to say selected on it. Also be sure to comment this hack for future reference.



来源:https://stackoverflow.com/questions/19904984/laravel4-form-with-image-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!