Laravel Form::具有默认值的文本样式


Laravel Form::text styling with default value

我尝试了以下代码:

{{ Form::text('username', 'Usuario', array('class' => 'form-control')) }}

但它失败了,它抛出了这个错误:

ErrorException
htmlentities() expects parameter 1 to be string, array given (View: /mydir/app/views/sessions/create.blade.php)
/mydir/vendor/laravel/framework/src/Illuminate/Support/helpers.php
     * Escape HTML entities in a string.
     *
     * @param  string  $value
     * @return string
     */
    function e($value)
    {
        return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
    }
}

文档api说:

/**
 * Create a text input field.
 *
 * @param  string  $name
 * @param  string  $value
 * @param  array   $options
 * @return string
 */
 public function text($name, $value = null, $options = array())
 {
     return $this->input('text', $name, $value, $options);
 }

这意味着:

// ...is correct
Form::text(   'username', 'Usuario', array('class' => 'form-control')   )

因此,这条线并不是罪魁祸首。你必须检查堆栈跟踪,看它在哪一行出现错误。


更新

我看到你在评论中谈论密码字段。使用密码字段的正确方法是:

public function password($name, $options = array())
{
    return $this->input('password', $name, '', $options);
}

你问:

好吧,我想就是这样。我的密码字段有一个默认文本。我不能吗?

答案是

不,你不能。但是你可以试试Form::input

/**
 * Create a form input field.
 *
 * @param  string  $type
 * @param  string  $name
 * @param  string  $value
 * @param  array   $options
 * @return string
 */
 public function input($type, $name, $value = null, $options = array()) {

这样做:(警告未经测试的代码

Form::input('password', 'passwordfield', 'defaultvalue', array('class' => 'myclass'))

我不知道你是否想填写默认值。也许你想使用placeholder。尝试:

{{ Form::text('username', null, array(
    'class' => 'form-control', 
    'placeholder' => 'Usuario',
  )) 
}}