简体   繁体   English

REST API的用户名和电子邮件检查

[英]Username and email check for REST API

I am new to php development. 我是php开发的新手。 I have created a REST API in which employees are created and the list of employees is generated. 我创建了一个REST APIemployees创建和产生的员工列表。 The tool for checking the API is postman . 用于检查API的工具是postman

Now I want to do is that when username and email are entered, it will check for both of them that if the username and email are already taken then it will not create rather than it will give an error message. 现在,我想做的是,当输入用户名和电子邮件时,它将检查是否都已使用了用户名和电子邮件,那么它们将不会创建,而是会给出错误消息。

As a newbie in php, i am unable to find the perfect example. 作为php的新手,我找不到完美的例子。 Below is the code 下面是代码

Employee Model 员工模式

class Employee extends \yii\db\ActiveRecord
{
const SCENARIO_CREATE = 'create';
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'employee';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['emp_name', 'emp_email', 'emp_sal'], 'required'],
        [['emp_name', 'emp_email', 'emp_sal'], 'string', 'max' => 100],
    ];
}


public function scenarios()
{
    $scenarios = parent::scenarios();
    $scenarios['create'] = ['emp_name','emp_email', 'emp_sal'];
    return $scenarios;

}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'emp_name' => 'Emp Name',
        'emp_email' => 'Emp Email',
        'emp_sal' => 'Emp Sal',
    ];
}

Employee Controller 员工控制人

 public function actionCreateEmployee()
{


   \Yii::$app->response->format= \yii\web\Response::FORMAT_JSON;


    $employee = new Employee();
    $employee-> scenario = Employee::SCENARIO_CREATE;

    $employee->attributes = \Yii::$app->request->post();

    if ($employee->validate())
    {
        $employee->save();
        return array('status'=> true, 'data' => 'Employee Created Sussessfully');
    }
    else
    {
        return array('status'=> false, 'data'=>$employee->getErrors());
    }
    //return array('status'=> true);
}

public function actionListEmployee()
{
    \Yii::$app->response->format= \yii\web\Response::FORMAT_JSON;

    $employee = Employee::find()->all();

    if(count($employee)>0)
    {
        return array('status' => true,'data'=> $employee);
    }
    else{
        return array('status'=>false, 'data'=> 'No Employee Record Found');
    }

}

The ID is auto incremented. ID是自动递增的。

Any help would be highly appreciated. 任何帮助将不胜感激。

Yii2 has validation for uniqueness. Yii2具有唯一性验证。 To implement this, you have to add this in the rules array for the fields that you want to check for uniqueness. 要实现此目的,您必须将其添加到要检查唯一性的字段的规则数组中。 In your case, this will be as follows: 您的情况如下:

public function rules()
{
    return [
        [['emp_name', 'emp_email', 'emp_sal'], 'required'],
        [['emp_name', 'emp_email', 'emp_sal'], 'string', 'max' => 100],
        [['emp_email'],'unique','message'=>'Employee email has already been taken. Use another email.'],
        [['emp_name'],'unique','message'=>'User name has already been taken.']
    ];
}

You can check this link if you want to know more about the unique validator of Yii2 http://www.yiiframework.com/doc-2.0/yii-validators-uniquevalidator.html 如果您想进一步了解Yii2的唯一验证器,可以查看此链接http://www.yiiframework.com/doc-2.0/yii-validators-uniquevalidator.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM