我正在使用ajax提交包含数组,文本字段和文件的多部分表单.
我将每个VAR附加到主数据上
var attachments = document.getElementById('files');
var data= new FormData();
for (i=0; i< attachments.files.length; i++){
data.append('file', attachments.files[i]);
console.log(attachments.files[i]);
data.append ('headline', headline);
data.append ('article', article);
data.append ('arr', arr);
data.append ('tag', tag);
Run Code Online (Sandbox Code Playgroud)
然后我使用ajax函数将其发送到PHP文件以存储在sql DB中.
$.ajax({
type: "post",
url: 'php/submittionform.php',
cache: false,
processData: false,
contentType: false,
data: data,
success: function(request) {$('#box').html(request); }
})
Run Code Online (Sandbox Code Playgroud)
但在PHP方面,arr
变量(即数组)显示为字符串.
当我不使用ajax作为表单数据发送它但使用简单$.POST
选项时我会在PHP端将其作为数组获取,但是我也无法发送文件.
任何解决方案
对Laravel来说还是一个新手,我必须创建一个用于创建的表单和一个用于编辑的表单.在我的形式中,我有一些jquery ajax帖子.我想知道Laravel是否确实提供了一种简单的方法让我使用相同的表单进行编辑和创建,而无需在我的代码中添加大量逻辑.每次在表单加载时为字段赋值时,我都不想检查是否处于编辑或创建模式.关于如何以最少的编码实现这一目标的任何想法?
我已经尝试将我的traits文件夹添加到composer自定义自动加载,但这不起作用并返回错误.那么这可能是通过作曲家自动加载的特征吗?非常感谢任何答案.
我的特点:
trait User
{
public function is_email_unique($email)
{
return $this->User->get_one_by_email($email) ? FALSE : TRUE;
}
public function is_username_unique($username)
{
return $this->User->get_one_by_username($username) ? FALSE : TRUE;
}
}
Run Code Online (Sandbox Code Playgroud)
我的课:
class Auth extends MY_Controller
{
// Implement trait in class
use User;
public function __contstruct()
{
parent::__contstruct();
}
public function register()
{
if ($this->input->is_post()) {
// load validation library
$this->load->library('form_validation');
//set validation rules
$this->form_validation->set_rules('username', "Username", 'required|callback_username_check');
// There I use my trait method callback_is_email_unique
$this->form_validation->set_rules('email', "Email", 'required|valid_email|callback_is_email_unique');
$this->form_validation->set_rules('password', "Password", 'required|matches[confirm_password]|min_length[6]');
$this->form_validation->set_rules('confirm_password', …
Run Code Online (Sandbox Code Playgroud) 要预先填充表单字段,我们可以在create.blade.php中为表单字段添加"value":
{{ Form::text('title', 'Some default title') }}
Run Code Online (Sandbox Code Playgroud)
有没有办法在其他地方完成这项任务(可能在模型或控制器?).我想在创建和编辑视图中使用相同的表单字段代码.谢谢!
在Laravel应用程序中,我有一个表单,我需要用逗号作为小数分隔符来验证数字.目前,它只适用于一点,因为我的验证规则是:
$rules = [
'amount' => 'numeric|min:0',
];
Run Code Online (Sandbox Code Playgroud)
什么是最好的方法:
下面代码中的闭包使得该代码非常难以测试。我怎样才能继续急切地加载这些项目并保持完整的可测试性?
public function scopeWithCompanyPreferences(Builder $builder)
{
return $builder->with([
'companies' => function ($query) {
$query->with('companies');
$query->with('preference_settings');
$query->with('parent_company');
}
]);
}
Run Code Online (Sandbox Code Playgroud)
我见过使用 Mockery 来使用Mockery::on()
,但我认为这对于数组来说没有用。
我正在尝试使用Vue.js动画化列表的排序,但并非所有项目都具有动画效果。你知道为什么吗?以及如何使其工作?
new Vue({
el: '#app',
data: {
reverse: 1,
items: [
{ name: 'Foo' },
{ name: 'Bar' },
{ name: 'Baz' },
{ name: 'Qux' }
]
}
})
Run Code Online (Sandbox Code Playgroud)
.moving-item {
transition: all 1s ease;
-webkit-transition: all 1s ease;
}
ul {
list-style-type: none;
padding: 0;
position: relative;
}
li {
position: absolute;
border: 1px solid #42b983;
height: 20px;
width: 150px;
padding: 5px;
margin-bottom: 5px;
color: #42b983;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.0-alpha.2/vue.min.js"></script>
<div id="app">
<button on-click="reverse = Math.abs(reverse-1)">
<span v-if="reverse == …
Run Code Online (Sandbox Code Playgroud)php ×5
laravel ×4
javascript ×2
ajax ×1
arrays ×1
autoload ×1
composer-php ×1
css ×1
form-data ×1
forms ×1
mockery ×1
traits ×1
unit-testing ×1
validation ×1
vue.js ×1