【发布时间】:2017-02-23 10:31:24
【问题描述】:
现在的问题是,我对每个字段都有验证规则 当我故意将表单保留为空值[说名称字段为空]时,它会给我一个明显的验证错误。
但是,填写的表格的其他部分与数据一起保存,但只有上传字段,[专业图片字段]设置为空。
HTML
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
'id'=>'recruitment-form',
'enableAjaxValidation'=>false,
'type'=>'vertical',
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<?php echo $form->textFieldRow($modelGeneral,'email',array('maxlength'=>50)); ?>
<br/><br/>
<?php echo $form->fileFieldRow($modelGeneral,'resume', array( 'allowEmpty'=>true)); ?>
PHP
public function actionCreate() {
$model = new Recruitment('test');
$modelGeneral = new RecruitmentGeneral;
if (isset($_POST['RecruitmentGeneral'])) {
//print_r($_POST['RecruitmentGeneral']);exit;
$modelGeneral->attributes = $_POST['RecruitmentGeneral'];
$modelGeneral->DOB = $_POST['RecruitmentGeneral']['DOB'];
$modelGeneral->middle_name = $_POST['RecruitmentGeneral']['middle_name'];
$modelGeneral->role = $_POST['RecruitmentGeneral']['role'];
$modelGeneral->consultancy = $_POST['RecruitmentGeneral']['consultancy'];
$modelGeneral->comments = $_POST['RecruitmentGeneral']['comments'];
$modelGeneral->candidate_choice = $_POST['RecruitmentGeneral']['candidate_choice'];
$modelGeneral->resume = CUploadedFile::getInstance($modelGeneral, 'resume');
if ($modelGeneral->validate()) {
$model->attributes = $modelGeneral->attributes;
$model->createDate = date('Y-m-d');
$model->candidate_choice = $modelGeneral->candidate_choice;
if ($model->save()) {
// chmod(Yii::getPathOfAlias('webroot').'/uploads/resums/', 0777);
if ($model->resume && $model->id) {
if ($model->resume->saveAs((Yii::getPathOfAlias('webroot') . '/uploads/resums/' . $model->id . "_" . $modelGeneral->resume->name))) {
$redirect=Yii::app()->createUrl('experince',['id'=>$model->id]);
}
}
$redirect=Yii::app()->createUrl('experince',['id'=>$model->id]);
} else {
$errors = $model->getErrors();
foreach ($errors as $attribute => $error) {
$modelGeneral->addError($attribute, $error[0]);
}
}
}
}
$this->render('create', array(
'modelGeneral' => $modelGeneral,
'Position' => Designation::designationLists(),
'Department' => Department::departmentLists(),
"ReferredBy" => Employee::allUsers(),
'consultancy' => ConsultancyDetails::consultancies(),
'roles' => EmployeeRole:: allRoles(),
'candidateChoice' => StaticCoreComponent::$candidate_choice,
));
}
【问题讨论】:
标签: php validation active-form yii1.x