Laravel4:带有图像选择器的表单 [英] Laravel4: Form with image-picker

查看:100
本文介绍了Laravel4:带有图像选择器的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Bootstrap + Laravel4的站点中. 在表单中,用户可以单击一些图像来选择它. 这是通过一个不错的JQuery插件完成的: http://rvera.github.io/image-picker/

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/

现在它是用html编写的,并且可以正常工作:

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> 

我不需要在Laravel4中重写所有这些标记. 但是我需要一件事:我必须添加到Input :: old('inputName')来保留值,以防发生验证错误. 那很重要. 我尝试这样做: -将单个选项的值设置为:{{'Single room',Input :: old('1room')}} -将选择的值设置为{{Input :: old('rooms []')}} 但是他们都不在工作. 它没有错误,但是没有保留原始值.

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.

有人知道我该怎么办?

非常感谢!

在页面顶部打印出Input :: old()的值,实际上保留了房间的值:

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.

由于@Samuraisoulification,这是了解所选内容选项的方法:

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";
                 }
               }
              }
          ?>

推荐答案

这是一个非常精致的解决方案,但如果我正确的话应该可以使用.在选项标签中添加:

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";
           }
         }
?>

请注意,$ input和$ c不在比较部分,因此它们只会被评估一次,从而为您以后可能拥有的所有选项节省了少量时间(例如页面加载时间).这是一个不太好的解决方案,但是由于您允许多个值,因此我认为这是唯一的方法.旋转一下,让我知道是否有问题!另外,请务必将此代码放在每个开始标签中!

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.

最后,我认为由于选项与输入不同,因此value=="{{Input::old}}"不起作用,因为要选择它,必须说要选择它.另外,请务必评论此hack,以备将来参考.

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.

这篇关于Laravel4:带有图像选择器的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆