lithium\data\Model::validates PHP Method

validates() public method

Note that these are application-level validation rules, and do not interact with any rules or constraints defined in your data source. If such constraints fail, an exception will be thrown by the database layer. The validates() method only checks against the rules defined in application code. This method uses the Validator class to perform data validation. An array representation of the entity object to be tested is passed to the check() method, along with the model's validation rules. Any rules defined in the Validator class can be used to validate fields. See the Validator class to add custom rules, or override built-in rules.
See also: lithium\data\Model::$validates
See also: lithium\util\Validator::check()
See also: lithium\data\Entity::errors()
public validates ( object $entity, array $options = [] ) : boolean
$entity object Model entity to validate. Typically either a `Record` or `Document` object. In the following example: ``` $post = Posts::create($data); $success = $post->validates(); ``` The `$entity` parameter is equal to the `$post` object instance.
$options array Available options: - `'rules'` _array_: If specified, this array will _replace_ the default validation rules defined in `$validates`. - `'events'` _mixed_: A string or array defining one or more validation _events_. Events are different contexts in which data events can occur, and correspond to the optional `'on'` key in validation rules. For example, by default, `'events'` is set to either `'create'` or `'update'`, depending on whether `$entity` already exists. Then, individual rules can specify `'on' => 'create'` or `'on' => 'update'` to only be applied at certain times. Using this parameter, you can set up custom events in your rules as well, such as `'on' => 'login'`. Note that when defining validation rules, the `'on'` key can also be an array of multiple events. - `'required`' _mixed_: Represents whether the value is required to be present in `$values`. If `'required'` is set to `true`, the validation rule will be checked. if `'required'` is set to 'false' , the validation rule will be skipped if the corresponding key is not present. If don't set `'required'` or set this to `null`, the validation rule will be skipped if the corresponding key is not present in update and will be checked in insert. Defaults is set to `null`. - `'whitelist'` _array_: If specified, only fields in this array will be validated and others will be skipped.
return boolean Returns `true` if all validation rules on all fields succeed, otherwise `false`. After validation, the messages for any validation failures are assigned to the entity, and accessible through the `errors()` method of the entity object.
    public function validates($entity, array $options = array())
    {
        $defaults = array('rules' => $this->validates, 'events' => $entity->exists() ? 'update' : 'create', 'model' => get_called_class(), 'required' => null, 'whitelist' => null);
        $options += $defaults;
        if ($options['required'] === null) {
            $options['required'] = !$entity->exists();
        }
        $self = static::_object();
        $validator = $self->_classes['validator'];
        $entity->errors(false);
        $params = compact('entity', 'options');
        $filter = function ($parent, $params) use($validator) {
            $entity = $params['entity'];
            $options = $params['options'];
            $rules = $options['rules'];
            unset($options['rules']);
            if ($whitelist = $options['whitelist']) {
                $whitelist = array_combine($whitelist, $whitelist);
                $rules = array_intersect_key($rules, $whitelist);
            }
            if ($errors = $validator::check($entity->data(), $rules, $options)) {
                $entity->errors($errors);
            }
            return empty($errors);
        };
        return static::_filter(__FUNCTION__, $params, $filter);
    }

Usage Example

示例#1
0
	/**
	 * Check related model for validation errors
	 *
	 * @param \lihtuim\data\Entity $entity
	 * @return boolean
	 */
	public function validates($entity, array $options = array()) {
		$success = parent::validates($entity, $options);
		$model = $entity->model();
		foreach ($model::relations() as $related => $relationship) {
			if (!empty($entity->{$related}) && is_object($entity->{$related})) {
				switch ($relationship->type()) {
					case 'hasOne' :
					case 'belongsTo' :
						$success = $entity->{$related}->validates() && $success;
						break;
					case 'hasMany' :
						foreach ($entity->{$related} as $relatedEntity) {
							$success = $relatedEntity->validates() && $success;
						}
						break;
				}
			}
		}
		return $success;
	}