Php 显示yii2表单验证中的错误

Php 显示yii2表单验证中的错误,php,validation,yii2,Php,Validation,Yii2,我想在yii2中进行自定义验证,但这不起作用 我想验证的是,只要卡车状态不是17或18,即如果是12,则应返回此错误,但始终显示错误 public function validateRegno() { $truck = TblTrucks::find()->where(["reg_no"=>$this->reg_no])->all(); if ($truck) { if(!$truck->truck_status ==18 ||

我想在yii2中进行自定义验证,但这不起作用

我想验证的是,只要卡车状态不是17或18,即如果是12,则应返回此错误,但始终显示错误

  public function validateRegno()
{
    $truck = TblTrucks::find()->where(["reg_no"=>$this->reg_no])->all();

    if ($truck) {
        if(!$truck->truck_status ==18 || !$truck->truck_status ==17){
            $this->addError('reg_no', 'The truck is not yet cleared by customer service');
        }


    }
}
这些是标准规则

public function rules()
{
    return [
        [['reg_no', 'truck_category', 'added_by', 'truck_status', 'driver_name'], 'required'],
        [['truck_category', 'added_by', 'truck_status', 'is_normal'], 'integer'],
        [['added_on'], 'safe'],
        [['reg_no'], 'string', 'max' => 50],

        ['reg_no', 'validateRegno','on' => 'create'],

}
这是我的表格

  <?php $form = ActiveForm::begin(['id' => $model->formName(),
        'enableAjaxValidation' => true,
        //'enableClientValidation' => true,
        'validationUrl' => ['truck/validate'],

    ]); ?>

您的自定义验证方法如下所示

    public function validateRegno()
{
    $truck = TblTrucks::find()->where(["reg_no"=>$this->reg_no])->all();

    if ($truck) {
        if(!$truck->truck_status ==18 || !$truck->truck_status ==17){
            $this->addError('reg_no', 'The truck is not yet cleared by customer service');
        }


    }
}
我希望在您的模型中,
$this->reg\u no
是主键字段,我指的是自动递增字段。因此,在创建新记录时,它将是空的

编辑:

根据您的评论,
$this->reg\u no
不是模型中的主键。使用
all()
检索记录时,它将返回记录数组。请参考此链接-因此您必须迭代记录以检查每个卡车的状态


否则,
$this->reg\u no
在表中是唯一的,您必须使用
one()
方法检索记录。它将从表中返回一条记录。请参考
one()
-

的文档,您的自定义验证方法如下所示

    public function validateRegno()
{
    $truck = TblTrucks::find()->where(["reg_no"=>$this->reg_no])->all();

    if ($truck) {
        if(!$truck->truck_status ==18 || !$truck->truck_status ==17){
            $this->addError('reg_no', 'The truck is not yet cleared by customer service');
        }


    }
}
我希望在您的模型中,
$this->reg\u no
是主键字段,我指的是自动递增字段。因此,在创建新记录时,它将是空的

编辑:

根据您的评论,
$this->reg\u no
不是模型中的主键。使用
all()
检索记录时,它将返回记录数组。请参考此链接-因此您必须迭代记录以检查每个卡车的状态



否则,
$this->reg\u no
在表中是唯一的,您必须使用
one()
方法检索记录。它将从表中返回一条记录。请参阅表单上的
one()
-

使用
'enableAjaxValidation'=>true
,并添加
if(Yii::$app->request->isAjax&&$model->load(Yii:$app->request->post()){Yii:$app->response->format=\Yii\web\response::format\u JSON;return ActiveForm::validate($model)}
in action part但它无法执行soare you get error?未返回任何错误发生的情况是or(| |)条件不起作用,当使用eg 17的单个条件进行测试时,它起作用,但当使用这两个条件进行测试时,它不起作用将条件更正为
if(!in_数组($truck->truck|u status,[17,18]))
在表单上使用
'enableAjaxValidation'=>true
并添加
if(Yii::$app->request->isAjax&&$model->load(Yii::$app->request->post()){Yii::$app->response->format=\Yii\web\response::format_JSON;return ActiveForm::validate($model)}
in action part但它无法执行soare you get error?未返回任何错误发生的情况是or(| |)条件不起作用,当使用eg 17的单个条件进行测试时,它起作用,但当使用这两个条件进行测试时,它不起作用将条件更正为
if(!in_数组($truck->truck|u status,[17,18]))
它与我在问题和it中发布的内容有何不同fails@GEOFFREYMWANGI请检查validateRegno方法,我删除了查询和if条件。在你的情况下,我觉得没有必要。发生的是,在注册期间,ama检查是否有一辆卡车具有类似的注册号,因此我们不能假设为空,注册号不是自动递增的查询对于检查卡车是否注册和exists@GEOFFREYMWANGI好啊让我根据您的要求更改我的答案。它与我在问题和答案中发布的内容有何不同fails@GEOFFREYMWANGI请检查validateRegno方法,我删除了查询和if条件。在你的情况下,我觉得没有必要。发生的是,在注册期间,ama检查是否有一辆卡车具有类似的注册号,因此我们不能假设为空,注册号不是自动递增的查询对于检查卡车是否注册和exists@GEOFFREYMWANGI好啊让我根据你的要求修改我的答案