本文整理汇总了PHP中CActiveRecord::validate方法的典型用法代码示例。如果您正苦于以下问题:PHP CActiveRecord::validate方法的具体用法?PHP CActiveRecord::validate怎么用?PHP CActiveRecord::validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CActiveRecord
的用法示例。
在下文中一共展示了CActiveRecord::validate方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: afterValidate
/**
* Extended after validate function from CFormModel that validates associated
* active record as well
* @param array $attributes
* @param boolean $clearErrors
* @return boolean
*/
public function afterValidate($attributes = null, $clearErrors = true)
{
if ($this->model != null) {
if ($this->model->validate()) {
return true;
} else {
$errors = $this->model->getErrors();
$this->addErrors($errors);
return false;
}
}
return true;
}
示例2: validate
public function validate($attributes = NULL, $clearErrors = true)
{
$valid = parent::validate();
if (!$this->dpid) {
return false;
}
$site = Site::model()->find('lid<>:siteId and type_id=:typeId and dpid=:companyId and serial=:serial and delete_flag=0', array(':serial' => $this->serial, ':siteId' => $this->lid ? $this->lid : '', ':typeId' => $this->type_id, ':companyId' => $this->dpid));
if ($site) {
$this->addError('serial', '座位号已经存在');
return false;
}
return !$this->hasErrors();
}
示例3: update
/**
*### .update()
*
* main function called to update column in database
*
*/
public function update()
{
//get params from request
$this->primaryKey = yii::app()->request->getParam('pk');
$this->attribute = yii::app()->request->getParam('name');
$this->value = yii::app()->request->getParam('value');
//checking params
if (empty($this->attribute)) {
throw new CException(Yii::t('TbEditableSaver.editable', 'Property "attribute" should be defined.'));
}
if (empty($this->primaryKey)) {
throw new CException(Yii::t('TbEditableSaver.editable', 'Property "primaryKey" should be defined.'));
}
//loading model
$this->model = CActiveRecord::model($this->modelClass)->findByPk($this->primaryKey);
if (!$this->model) {
throw new CException(Yii::t('TbEditableSaver.editable', 'Model {class} not found by primary key "{pk}"', array('{class}' => get_class($this->model), '{pk}' => is_array($this->primaryKey) ? CJSON::encode($this->primaryKey) : $this->primaryKey)));
}
//set scenario
$this->model->setScenario($this->scenario);
//commented to be able to work with virtual attributes
//see https://github.com/vitalets/yii-bootstrap-editable/issues/15
/*
//is attribute exists
if (!$this->model->hasAttribute($this->attribute)) {
throw new CException(Yii::t('EditableSaver.editable', 'Model {class} does not have attribute "{attr}"', array(
'{class}'=>get_class($this->model), '{attr}'=>$this->attribute)));
}
*/
//is attribute safe
if (!$this->model->isAttributeSafe($this->attribute)) {
throw new CException(Yii::t('editable', 'Model {class} rules do not allow to update attribute "{attr}"', array('{class}' => get_class($this->model), '{attr}' => $this->attribute)));
}
//setting new value
$this->setAttribute($this->attribute, $this->value);
//validate attribute
$this->model->validate(array($this->attribute));
$this->checkErrors();
//trigger beforeUpdate event
$this->beforeUpdate();
$this->checkErrors();
//saving (no validation, only changed attributes)
if ($this->model->save(false, $this->changedAttributes)) {
//trigger afterUpdate event
$this->afterUpdate();
} else {
$this->error(Yii::t('TbEditableSaver.editable', 'Error while saving record!'));
}
}
示例4: update
/**
* main function called to update column in database
*
*/
public function update()
{
//set params from request
$this->primaryKey = yii::app()->request->getParam('pk');
$this->attribute = yii::app()->request->getParam('name');
$value = Yii::app()->request->getParam('value');
//checking params
if (empty($this->attribute)) {
throw new CException(Yii::t('zii', 'Property "attribute" should be defined.'));
}
if (empty($this->primaryKey)) {
throw new CException(Yii::t('zii', 'Property "primaryKey" should be defined.'));
}
//loading model
$this->model = CActiveRecord::model($this->modelClass)->findByPk($this->primaryKey);
if (!$this->model) {
throw new CException(Yii::t('editable', 'Model {class} not found by primary key "{pk}"', array('{class}' => get_class($this->model), '{pk}' => $this->primaryKey)));
}
$this->model->setScenario($this->scenario);
//is attribute exists
if (!$this->model->hasAttribute($this->attribute)) {
throw new CException(Yii::t('editable', 'Model {class} does not have attribute "{attr}"', array('{class}' => get_class($this->model), '{attr}' => $this->attribute)));
}
//is attribute safe
if (!$this->model->isAttributeSafe($this->attribute)) {
throw new CException(Yii::t('zii', 'Model {class} rules do not allow to update attribute "{attr}"', array('{class}' => get_class($this->model), '{attr}' => $this->attribute)));
}
//setting new value
$this->setAttribute($this->attribute, $value);
//validate
$this->model->validate(array($this->attribute));
if ($this->model->hasErrors()) {
$this->error($this->model->getError($this->attribute));
}
//save
if ($this->beforeUpdate()) {
//saving (only chnaged attributes)
if ($this->model->save(false, $this->changedAttributes)) {
$this->afterUpdate();
} else {
$this->error(Yii::t('zii', 'Error while saving record!'));
}
} else {
$firstError = reset($this->model->getErrors());
$this->error($firstError[0]);
}
}
示例5: update
/**
* main function called to update column in database
*
*/
public function update()
{
//get params from request
$this->primaryKey = yii::app()->request->getParam('pk');
$this->attribute = yii::app()->request->getParam('name');
$this->value = yii::app()->request->getParam('value');
$this->scenario = yii::app()->request->getParam('scenario');
//checking params
if (empty($this->attribute)) {
throw new CException(Yii::t('EditableSaver.editable', 'Property "attribute" should be defined.'));
}
$this->model = new $this->modelClass();
$isFormModel = $this->model instanceof CFormModel;
$isMongo = EditableField::isMongo($this->model);
if (empty($this->primaryKey) && !$isFormModel) {
throw new CException(Yii::t('EditableSaver.editable', 'Property "primaryKey" should be defined.'));
}
//loading model
if ($isMongo) {
$this->model = $this->model->findByPk(new MongoID($this->primaryKey));
} elseif (!$isFormModel) {
$this->model = $this->model->findByPk($this->primaryKey);
}
if (!$this->model) {
throw new CException(Yii::t('EditableSaver.editable', 'Model {class} not found by primary key "{pk}"', array('{class}' => get_class($this->model), '{pk}' => is_array($this->primaryKey) ? CJSON::encode($this->primaryKey) : $this->primaryKey)));
}
//keep parent model for mongo
$originalModel = $this->model;
//resolve model only for mongo! we should check attribute safety
if ($isMongo) {
$resolved = EditableField::resolveModels($this->model, $this->attribute);
$this->model = $resolved['model'];
//can be related model now
$this->attribute = $resolved['attribute'];
$staticModel = $resolved['staticModel'];
} else {
$staticModel = $this->model;
}
//set scenario for main model
if ($this->scenario) {
$originalModel->setScenario($this->scenario);
}
//is attribute safe
if (!$this->model->isAttributeSafe($this->attribute)) {
throw new CException(Yii::t('editable', 'Model {class} rules do not allow to update attribute "{attr}"', array('{class}' => get_class($this->model), '{attr}' => $this->attribute)));
}
//setting new value
$this->setAttribute($this->attribute, $this->value);
//validate attribute
$this->model->validate(array($this->attribute));
$this->checkErrors();
//trigger beforeUpdate event
$this->beforeUpdate();
$this->checkErrors();
//remove virtual attributes (which NOT in DB table)
if (!$isMongo) {
$this->changedAttributes = array_intersect($this->changedAttributes, $originalModel->attributeNames());
if (count($this->changedAttributes) == 0) {
//can not pass empty array in model->save() method!
$this->changedAttributes = null;
}
}
//saving (no validation, only changed attributes) note: for mongo save all!
if ($isMongo) {
$result = $originalModel->save(false, null);
} elseif (!$isFormModel) {
$result = $originalModel->save(false, $this->changedAttributes);
} else {
$result = true;
}
if ($result) {
$this->afterUpdate();
} else {
$this->error(Yii::t('EditableSaver.editable', 'Error while saving record!'));
}
}
示例6: validate
public function validate($attributes = null, $clearErrors = true)
{
// si el metodo de autenticacion es solo email, y, username es blanco
// se genera uno automaticamente:
if ($this->scenario == 'insert') {
$declared_authmodes = CrugeUtil::config()->availableAuthModes;
if (count($declared_authmodes == 1)) {
if ($declared_authmodes[0] == 'email' && $this->username == '') {
$um = new CrugeUserManager();
$this->username = $um->generateNewUserName($this->email);
} else {
if ($declared_authmodes[0] == 'username' && $this->email == '') {
$this->email = $this->username . '@noemail.local';
}
}
}
}
// realiza la validacion normal sobre los atributos de este modelo
$validateResult = parent::validate();
// ahora realiza la validacion sobre aquellos campos personalizados
// y copia todos los errores al objeto mayor ($this)
//
foreach ($this->getFields() as $f) {
if ($f->validateField() == false) {
$this->addErrors($f->getErrors());
$validateResult = false;
}
}
return $validateResult;
}
示例7: validate
public function validate($attributes = null, $clearErrors = true)
{
if (!$this->eavEnable) {
return parent::validate($attributes, $clearErrors);
}
if ($clearErrors) {
$this->clearErrors();
}
$separatedAttributes = $this->separateAttributes($attributes);
$modelAttributes = isset($separatedAttributes['attributes']) ? $separatedAttributes['attributes'] : null;
$eavAttributes = isset($separatedAttributes['eavAttributes']) ? $separatedAttributes['eavAttributes'] : null;
if ($this->beforeValidate()) {
foreach ($this->getValidators() as $validator) {
$validator->validate($this, $modelAttributes);
}
$this->validateEavAttribute($eavAttributes);
$this->afterValidate();
return !$this->hasErrors();
} else {
return false;
}
}
示例8: validate
public function validate($attributes = NULL, $clearErrors = true)
{
// realiza la validacion normal sobre los atributos de este modelo
//
$validateResult = parent::validate();
// ahora realiza la validacion sobre aquellos campos personalizados
// y copia todos los errores al objeto mayor ($this)
//
foreach ($this->getFields() as $f) {
if ($f->validateField() == false) {
$this->addErrors($f->getErrors());
$validateResult = false;
}
}
return $validateResult;
}
示例9: validate
/**
* Additional validations
*
* @author Valeriy Zavolodko <vals2004@gmail.com>
* @author Vadim Bulochnik <bulochnik@zfort.net>
*
* @return boolean
*/
public function validate($attributes = null, $clearErrors = true)
{
$result = parent::validate($attributes, $clearErrors);
if ($result) {
if (strtotime($this->startDate) > strtotime($this->endDate)) {
$this->addError('startDate', 'Start Date must be before End Date');
$this->addError('endDate', 'End Date must be after Start Date');
$result = false;
}
}
return $result;
}
示例10: validate
public function validate($attributes = null, $clearErrors = true)
{
if (parent::validate($attributes, $clearErrors)) {
return true;
} else {
$message = Yii::t('app', 'ERROR_VALIDATE');
if (Yii::app()->controller->edit_mode && Yii::app()->request->isAjaxRequest) {
echo CJSON::encode(array('message' => $message, 'valid' => false, 'errors' => $this->getErrors()));
Yii::app()->end();
} else {
Yii::app()->controller->setFlashMessage($message);
}
return false;
}
}
示例11: validate
public function validate()
{
$valid = parent::validate();
// now validate related tables
if (!isset($_POST['Listing']['p_categories'])) {
$this->addError('p_categories', 'You have to choose some category!');
$valid = false;
}
if (!isset($_POST['Listing']['p_locations'])) {
$this->addError('p_locations', 'You have to choose some location!');
$valid = false;
}
if (!$this->logo && !$this->_oldLogo) {
$this->addError('logo', 'Logo is required!');
$valid = false;
}
return $valid;
}