Title: [Plugin: WP MVC] Validation on create?
Last modified: August 20, 2016

---

# [Plugin: WP MVC] Validation on create?

 *  [makiavelo](https://wordpress.org/support/users/makiavelo/)
 * (@makiavelo)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/plugin-wp-mvc-validation-on-create/)
 * Hi, I noticed that validations aren’t triggered when an entity is created, and
   it triggers normally when i’m updating an entity. Is this a bug?
 * [http://wordpress.org/extend/plugins/wp-mvc/](http://wordpress.org/extend/plugins/wp-mvc/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [makiavelo](https://wordpress.org/support/users/makiavelo/)
 * (@makiavelo)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/plugin-wp-mvc-validation-on-create/#post-2790906)
 * 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();}
 *  Thread Starter [makiavelo](https://wordpress.org/support/users/makiavelo/)
 * (@makiavelo)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/plugin-wp-mvc-validation-on-create/#post-2790907)
 * sorry forgot to add the ‘code’ tag… use your ide 😛
 *  [Onemark.cz](https://wordpress.org/support/users/onemarkcz/)
 * (@onemarkcz)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/plugin-wp-mvc-validation-on-create/#post-2791121)
 * Thank you, for your upgrade. I’ve just started with WPMVC. Your code modification
   helps me a lot:-)

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘[Plugin: WP MVC] Validation on create?’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/wp-mvc_ffffff.svg)
 * [WP MVC](https://wordpress.org/plugins/wp-mvc/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-mvc/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-mvc/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-mvc/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-mvc/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-mvc/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Onemark.cz](https://wordpress.org/support/users/onemarkcz/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/plugin-wp-mvc-validation-on-create/#post-2791121)
 * Status: not resolved