如何通过Yii2 Api中的错误消息?

尼扎尔·阿里·洪扎(Nizar Ali Hunzai)

这是附带的代码。如果缺少任何属性,如何抛出错误消息?我需要检查所有属性是否都来自表单。如果缺少任何属性,我想抛出一条带有正确错误代码的错误消息。

$get_email      = Null;
$get_password   = Null;
$get_username   = Null;

if (isset($_POST['username']) ) {
  $get_username = $_POST['username'];
}

if (isset($_POST['email']) && isset($_POST['password']) ) {
  $get_email = $_POST['email'];
  $get_password = $_POST['password'];
}


if ($get_email == Null || $get_username == Null || $get_password == Null ) {
 // through error code with message
} 

else {
  $model = new SignupForm();
  $model->email   = $get_email; 
  $model->username = $get_username;
  $model->password = $get_password;
}
尼扎尔·阿里·洪扎(Nizar Ali Hunzai)

这是我实现目标的代码。

$ model =新的SignupForm;

if (isset($_POST['username'] )) {
    $model->username = $_POST['username'];
}

if (isset($_POST['email'] )) {
    $model->email = $_POST['email'];
}

if (isset($_POST['password'] )) {
    $model->password = $_POST['password'];
}

if($model->validate()){
    $model->signup();
}

else{
    return $model->getErrors();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章