makiavelo
Forum Replies Created
-
Forum: Plugins
In reply to: [WP MVC] [Plugin: WP MVC] Validation on create?sorry forgot to add the ‘code’ tag… use your ide ??
Forum: Plugins
In reply to: [WP MVC] [Plugin: WP MVC] Validation on create?Well I modified the core to achieve this…
Line 136 of mvc_model.php, there is an else block… changed it for this:
} else {
//Modify default behavior to validate on creation too…
//Original line in this block: $id = $this->create($data);
$model_data = $data[$this->name];
unset($model_data[$this->primary_key]);
$valid = $this->validate_data($model_data);
if ($valid !== true) {
$this->validation_error = $valid;
$this->validation_error_html = $this->validation_error->get_html();
$this->invalid_data = $model_data;
return false;
}else{
$id = $this->create($data);
}
}But that isn’t enough, because the script breaks when trying to set the object (actually when trying to fetch relationships without an id).
I made a quick fix…
Line 506 (make a verification of the id)
if (!empty($this->primary_key)) {
if(isset($object->{$this->primary_key})){
$object->__id = $object->{$this->primary_key};
}
}Lines 543 and 553 add this right after the ‘case’:
if(!isset($object->{$this->primary_key})) break;
Now the form behaves as espected when creating… the controller code would be this:
if (!empty($this->params[‘data’]) && !empty($this->params[‘data’][‘Store’])) {
$object = $this->params[‘data’][‘Store’];
if ($this->Store->save($this->params[‘data’])) {
$this->flash(‘notice’, ‘Successfully saved!’);
$this->refresh();
} else {
$this->flash(‘error’, $this->Store->validation_error_html);
}
$this->set_object();
}