Php 尝试使用每个验证程序Yii2时发生数组到字符串转换错误

Php 尝试使用每个验证程序Yii2时发生数组到字符串转换错误,php,validation,yii,yii2,each,Php,Validation,Yii,Yii2,Each,我一直在尝试开发一个基于数据库中的问题生成的动态表单,但是我在表单验证方面遇到了很多问题。我到处找,但没有一个有效的。最有希望的解决方案是使用“each”验证器,但当我尝试使用它时,它给了我和“Array-to-string-conversion”错误,我无法修复。如果有人知道我的代码有什么问题,我将不胜感激。 这是代码,有点混乱,因为我测试了很多东西。 模型 看法 预审程序表 在cuidado的回应中,没有其他人支持editar las Respestas。 普雷蒙塔 答复: 答复: 控

我一直在尝试开发一个基于数据库中的问题生成的动态表单,但是我在表单验证方面遇到了很多问题。我到处找,但没有一个有效的。最有希望的解决方案是使用“each”验证器,但当我尝试使用它时,它给了我和“Array-to-string-conversion”错误,我无法修复。如果有人知道我的代码有什么问题,我将不胜感激。 这是代码,有点混乱,因为我测试了很多东西。 模型

看法


预审程序表
在cuidado的回应中,没有其他人支持editar las Respestas。
普雷蒙塔
答复:
答复:


控制器

    public function actionResponderFormulario($slug) {
    
        $evento = $this->findModel("", $slug);
        $inscripcion = Inscripcion::find()->where(["idEvento" => $evento->idEvento, "idUsuario" => Yii::$app->user->identity->idUsuario])
            ->andWhere(["<>", "estado", 1])
            ->andWhere(["<>", "estado", 2])
            ->one();
    
        if ($inscripcion != null) {
            $preguntas = Pregunta::find()->where(["idEvento" => $evento->idEvento])->all();
    
            $respuestaYaHechas = [];
            foreach ($preguntas as $pregunta){
                $respuesta = RespuestaSearch::find()->where(["idpregunta" => $pregunta->id, "idinscripcion" => $inscripcion->idInscripcion])->one();
                if($respuesta == null){
                    array_push($respuestaYaHechas, false);
                }else{
                    array_push($respuestaYaHechas, $respuesta);
                }
            }
            $model = new RespuestaTest();
            return $this->render('responderFormulario',
                            ["preguntas" => $preguntas,
                                "evento" => $evento,
                                "idInscripcion" => $inscripcion->idInscripcion,
                                "respuestaYaHechas" => $respuestaYaHechas,
                                "model" => $model]);
        } else {
            return $this->goHome();
        }
    }
公共功能actionResponderFormulario($slug){
$evento=$this->findModel(“,$slug”);
$inscripcion=inscripcion::find()->其中([“idEvento”=>$evento->idEvento,“idUsuario”=>Yii::$app->user->identity->idUsuario])
->andWhere([“”,“estado”,1])
->andWhere([“”,“estado”,2])
->一个();
如果($inscripcion!=null){
$preguntas=Pregunta::find()->where([“idEvento”=>$evento->idEvento])->all();
$respuestaYaHechas=[];
foreach($preguntas as$pregunta){
$respuesta=RespuestaSearch::find()->其中([“idpregunta”=>$pregunta->id,“idinscripcion”=>$inscripcion->idinscripcion])->一个();
如果($respuesta==null){
数组推送($respuestaYaHechas,false);
}否则{
阵列推送($respuestaYaHechas,$respuesta);
}
}
$model=new RespuestaTest();
返回$this->render('responderFormulario',
[“preguntas”=>$preguntas,
“evento”=>$evento,
“idiscripcion”=>$inscripcion->idiscripcion,
“respuestaYaHechas”=>respuestaYaHechas美元,
“模型”=>$model]);
}否则{
返回$this->goHome();
}
}

谢谢

我解决了,这真是件容易的事

我需要将输入中的名称声明为数组:

<?= $form->field($model, 'respuestaCorta[$i]')->textInput(['maxlength' => true])->label(false) ?>

输入规则名称=您的属性==>

[['name'], 'eachAlphaNumeric'],
和用于验证每个属性的自定义函数

 public function eachAlphaNumeric($attribute)
    {
        $alphaNumeric = true;
        $arr = explode(',', $this->$attribute);
        foreach ($arr as $key) {

            if (preg_match("/^[a-zA-Z0-9]*$/", $key) != 1 || in_array($key, ['NA', 'NULL', 'nill'])) {
                $alphaNumeric = false;
                $this->addError($attribute, 'invalid_value');
                break;
            }
        }

        return $alphaNumeric;
    }

事实上,这并没有解决问题:/,很抱歉,您使用的是哪个版本的yii2?v2.0.35似乎每个验证器都有问题。
[['name'], 'eachAlphaNumeric'],
 public function eachAlphaNumeric($attribute)
    {
        $alphaNumeric = true;
        $arr = explode(',', $this->$attribute);
        foreach ($arr as $key) {

            if (preg_match("/^[a-zA-Z0-9]*$/", $key) != 1 || in_array($key, ['NA', 'NULL', 'nill'])) {
                $alphaNumeric = false;
                $this->addError($attribute, 'invalid_value');
                break;
            }
        }

        return $alphaNumeric;
    }