小编Cra*_*eld的帖子

api调用后Wordpress使CF7无效

这是我的问题,我已经安装了Wordpress的联系表格7,并且在wpcf7_before_send_mail期间我对API进行了调用,如果API返回错误,则需要使表格无效,然后我需要使请求无效并返回从传回的错误API调用。

我在API失败时将标志设置为false,并且还存储了错误消息,但是尽管我诱发了失败,但我的表单仍在成功处理。

add_action("wpcf7_before_send_mail", "wpcf7_send_contact_builder");
function wpcf7_send_contact_builder($form) {
    $submission = WPCF7_Submission::get_instance();
    $wpcf7_data = $submission->get_posted_data();
    ... api call and set $success to true if ok and false if not ...
    if (!$success) {
        $form->status = 'validation_failed (statuscode:' . $xml->status->statuscode[0] . ').';
        $form->valid = false;
        $form->response = $xml->status->statusdesc[0];
        return $forml
    }
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用:

$form->invalidate('validation_failed (statuscode:' . $xml->status->statuscode[0] . ').', $xml->status->statusdesc[0]);
Run Code Online (Sandbox Code Playgroud)

但是无论哪种方式,我都无法阻止发送成功的电子邮件,并且表单验证成功。调试证明if语句中的!success成功,并且包含的​​代码已添加到变量中。我也尝试过,好像表单是一个数组($ form ['valid'] = false),但这也不起作用,表单提交成功。关于我在这里缺少什么的任何想法?我已经省略了API调用本身的代码以及确定正确的表单ID的代码,两者都可以正常工作,仅解析了我要使用的表单,并且API调用返回了预期的数据。

php forms wordpress invalidation contact-form-7

6
推荐指数
1
解决办法
836
查看次数

Eloquent 模型作为构建者回归

我正在使用 laravel 7 重新学习 Laravel,并遇到了无法查询数据库表中的记录的问题。因此,不是像这样的调用$test = Test::find_by_id_and_name(1, 'test 1');(并且还$test = Test::where('id', 1);返回 Illuninate\Database\Eloquent\Model 类,而是返回 Illuminate\Database\Eloquent\Builder 类。

我为名为 Tests 的表创建了一个迁移,并在其中添加了几行测试数据。App中的测试模型如下

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Test extends Model
{
    protected $guarded = [];
    use SoftDeletes;

}
Run Code Online (Sandbox Code Playgroud)

迁移是:

se Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
            $table->id();
            $table->string( 'name' );
            $table->string( 'url', 255 …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent

6
推荐指数
1
解决办法
4224
查看次数

LaravelWhereIn 保持数组使用的顺序

因此,我从数据库中获取一些项目以在下拉列表中使用。我希望下拉列表按输入顺序显示项目,例如我希望将项目排序为 2、1、3。当我渲染结果时,结果按 id 排序,而不是按我想要的顺序:

<?php

namespace App\View\Components;

use App\Models\JobState;
use Illuminate\View\Component;

class StatusDropdown extends Component
{
    public $states;

    public function __construct()
    {
        $this->states = JobState::whereIn( 'id', [4, 5, 10, 3, 11] )->get();
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.status-dropdown');
    }
}

Run Code Online (Sandbox Code Playgroud)

这是我的观点:

<select name="status" class="pl-10 block w-full shadow-sm sm:text-lg bg-gray-50 border-gray-300 focus:ring-primary-500 focus:border-primary-500 rounded-md">
    @foreach( $states as $state )
        <option value="{{ $state->id }}">{{ $state->value …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-query-builder

2
推荐指数
1
解决办法
1077
查看次数