我正在尝试将电子邮件值验证为唯一值。这样做。
[['HRMS_candidateEmail'],'unique'],
但在link
之后,它无效,插入声明use yii\validators\UniqueValidator;
模型
<?php
namespace app\models;
use Yii;
use yii\validators\UniqueValidator;
class Candidate extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'HRMS_candidate';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['HRMS_candidateEmail'],'unique'],
[['HRMS_candidateFirstName', 'HRMS_candidateLastName', 'HRMS_candidateEmail', 'HRMS_candidateMobileNo', 'HRMS_candidateLocalAddress', 'HRMS_candidatePermanantAddress', 'HRMS_candidateGender', 'HRMS_candidateDateOfBirth',
'HRMS_candidateMaritalStatus', 'HRMS_candidaterRefType', 'HRMS_candidateStatus', 'HRMS_candidateCurrentSalary', 'HRMS_candidateExpectedSalary', 'HRMS_candidateNoticePeriodBuyOut', 'HRMS_candidateCountry', 'HRMS_candidateCity',
'HRMS_candidatePrimarySkills', 'HRMS_candidateWillingToRelocate','HRMS_candidateExpYears','HRMS_candidateExpMonths','HRMS_candidateHrId','HRMS_candidateNoticePeriod','HRMS_candidateResumeSource',
'HRMS_candidateTrack','HRMS_candidateDepartment','HRMS_candidateNoOfChildren'], 'required'],
[['HRMS_candidateGender', 'HRMS_candidateBloodGroup', 'HRMS_candidateMaritalStatus', 'HRMS_candidaterRefType', 'HRMS_candidateStatus'], 'string'],
[['HRMS_candidateDateOfBirth', 'HRMS_candidateCreatedAt', 'HRMS_candidateUpdatedAt'], 'safe'],
[['HRMS_candidateTokenNo', 'HRMS_candidateNoticePeriodBuyOut', 'HRMS_candidateExpYears','HRMS_candidateExpMonths', 'HRMS_candidateNoOfChildren','HRMS_candidateMobileNo','HRMS_candidateAlternateNo','HRMS_candidateAdharNumber','HRMS_candidateNoticePeriod'], 'integer'],
[['HRMS_candidateFirstName', 'HRMS_candidateMiddleName', 'HRMS_candidateLastName', 'HRMS_candidateCurrentSalary', 'HRMS_candidateExpectedSalary'], 'string', 'max' => 50],
[['HRMS_candidateEmail', 'HRMS_candidateAlternateEmail'], 'string', 'max' => 150],
[['HRMS_candidateMobileNo', 'HRMS_candidateAlternateNo', 'HRMS_candidatePanNumber', 'HRMS_candidateAdharNumber'], 'string', 'max' => 25],
[['HRMS_candidateLocalAddress', 'HRMS_candidatePermanantAddress'], 'string', 'max' => 500],
[['HRMS_candidateNoticePeriod'], 'string', 'max' => 10],
[['HRMS_candidatePrimarySkills'], 'string', 'max' => 300],
[['HRMS_candidateEmail','HRMS_candidateAlternateEmail'], 'email'],
[['HRMS_candidateFirstName','HRMS_candidateMiddleName','HRMS_candidateLastName'], 'match', 'pattern' => '/^[a-zA-Z]+$/', 'message' => 'Please enter alphabets only.'],
[['HRMS_candidatePanNumber'], 'match', 'pattern' => '/^[a-zA-Z0-9]+$/', 'message' => 'Please enter alphanumric charachters only.'],
[['HRMS_candidatePassportNo','HRMS_candidateCountry','HRMS_candidateCity'], 'match', 'pattern' => '/^[a-zA-Z0-9-_@ ]+$/', 'message' => 'Please enter alphanumric charachters only.'],
[['HRMS_candidateLocalAddress','HRMS_candidatePermanantAddress'], 'match', 'pattern' => '/^[a-zA-Z0-9-_@.,: ]+$/', 'message' => 'Please enter valid address.'],
[['HRMS_candidatePrimarySkills'], 'match', 'pattern' => '/^[a-zA-Z0-9-_@.#+&*, ]+$/', 'message' => 'Please enter valid skills.'],
[['HRMS_candidateAlternateEmail'], 'compare', 'compareAttribute' => 'HRMS_candidateEmail','operator'=>'!='],
[['HRMS_candidateAlternateNo'], 'compare', 'compareAttribute' => 'HRMS_candidateMobileNo','operator'=>'!='],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'HRMS_candidateID' => 'ID',
'HRMS_candidateReferralID' => 'Referral ID',
'HRMS_candidateFirstName' => 'First Name',
'HRMS_candidateMiddleName' => 'Middle Name',
'HRMS_candidateLastName' => 'Last Name',
'HRMS_candidateEmail' => 'E-mail Id',
'HRMS_candidateAlternateEmail' => 'Alternate E-mail Id',
'HRMS_candidateMobileNo' => 'Mobile No',
'HRMS_candidateAlternateNo' => 'Alternate No',
'HRMS_candidateLocalAddress' => 'Current Address',
'HRMS_candidatePermanantAddress' => 'Permanant Address',
'HRMS_candidatePanNumber' => 'Pan Number',
'HRMS_candidateAdharNumber' => 'Adhar Number',
'HRMS_candidateGender' => 'Gender',
'HRMS_candidateDateOfBirth' => 'Date Of Birth',
'HRMS_candidateBloodGroup' => 'Blood Group',
'HRMS_candidateMaritalStatus' => 'Marital Status',
'HRMS_candidateNoOfChildren' => 'No Of Children',
'HRMS_candidaterRefType' => 'Reference Type',
'HRMS_candidateStatus' => 'Status',
'HRMS_candidateTokenNo' => 'Token No',
'HRMS_candidateRolesResponsibility' => 'Roles Responsibility',
'HRMS_candidateTokenExpiryDate' => 'Token Expiry Date',
'HRMS_candidateTokenGenerationDate' => 'Token Generation Date',
'HRMS_candidateCurrentSalary' => 'Current Salary',
'HRMS_candidateExpectedSalary' => 'Expected Salary',
'HRMS_candidateNoticePeriod' => 'Notice Period',
'HRMS_candidateNoticePeriodBuyOut' => 'Notice Period Buy Out',
'HRMS_candidatePassportNo' => 'Passport No',
'HRMS_candidateCountry' => 'Current Country',
'HRMS_candidateCity' => 'Current City',
'HRMS_candidatePrimarySkills' => 'Primary Skills',
'HRMS_candidateExpYears' => 'Exp Years',
'HRMS_candidateExpMonths'=> 'Exp Months',
'HRMS_candidateWillingToRelocate' => 'Willing To Relocate',
'HRMS_candidateDriveId' => 'Drive ID',
'HRMS_candidateHrId' => 'HR Name',
'HRMS_candidateCreatedAt' => 'Created At',
'HRMS_candidateUpdatedAt' => 'Updated At',
'HRMS_candidateResumeSource'=> 'Resume Source',
'HRMS_candidateTrack'=>'Track',
'HRMS_candidateDepartment'=>'Department',
'HRMS_candidateVacancyId'=>'Vacancy'
];
}
/**
* @return \yii\db\ActiveQuery
*/
/**
* @return \yii\db\ActiveQuery
*/
public function getHRMSCandidateEducations()
{
return $this->hasMany(HRMSCandidateEducation::className(), ['HRMS_candidateEducationCandidateID' => 'HRMS_candidateID']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getHRMSCandidateNotes()
{
return $this->hasMany(HRMSCandidateNote::className(), ['HRMS_candidateNoteCandidateId' => 'HRMS_candidateID']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getHRMSCandidatePreviousEmployments()
{
return $this->hasMany(HRMSCandidatePreviousEmployment::className(), ['HRMS_candidatePreviousEmploymentcandidateID' => 'HRMS_candidateID']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getHRMSFeedbacks()
{
return $this->hasMany(HRMSFeedback::className(), ['HRMS_feedbackCandidateID' => 'HRMS_candidateID']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getHRMSInterviewSchedules()
{
return $this->hasMany(HRMSInterviewSchedule::className(), ['HRMS_interviewScheduleCandidateID' => 'HRMS_candidateID']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getHRMSVacancyCandidates()
{
return $this->hasMany(HRMSVacancyCandidate::className(), ['HRMS_vacancyCandidateCandiateID' => 'HRMS_candidateID']);
}
public static function getFull_Name()
{
return $this->HRMS_candidateFirstName . ' ' . $this->HRMS_candidateLastName;
}
}
查看
<?= $form->field($model, 'HRMS_candidateEmail')->textInput(['maxlength' => true,'class'=>'form-control input-sm','placeholder'=>'Enter email of candidate']) ?>
答案 0 :(得分:2)
我假设你的观点为register.php
。所以,
register.php(查看)
1)删除use yii\bootstrap\ActiveForm;
(如果存在)
2)添加use yii\widgets\ActiveForm;
3)在该字段中添加'enableAjaxValidation' => true
(您要验证的位置。如下所示)
<?= $form->field($model, 'HRMS_candidateEmail',['enableAjaxValidation' => true])->textInput(['maxlength' => true,'class'=>'form-control input-sm','placeholder'=>'Enter email of candidate']) ?>
<强>控制器强>
在use yii\web\Response;use yii\widgets\ActiveForm;
use yii\web\Response;
use yii\widgets\ActiveForm;
.
.
.
public function actionRegister() {
$model = new Candidate();
//Add This For Ajax Email Exist Validation
if(Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())){
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
else if ($model->load(Yii::$app->request->post())) {
.
// Your code
.
.
}
}