Reputation: 61
I need to validate the form fields in wordpress WP MVC generated plugins.
I followed the tutorial of wpmvc but got no response.
Below code is used for validation:
/models/geozone.php
var $validate = array(
'geozone_name' => array(
'required' => true,
'pattern' => '/^[A-Z]/',
'message' => 'Please enter a capitalized name in the Geozone Name field!'
),
'state' => array(
'rule' => 'numeric',
'required' => true,
'message' => 'you cannot leave this field empty!')
);
The record is added as usual.
Any idea to refine the code?
Upvotes: 0
Views: 208
Reputation: 61
I solved this myself.
This code works fine for 'edit' action. For validation of the fields using the above code we explicitly need to overwrite the 'create_or_save' function in the AdminController like this:
if (empty($object['id'])) {
-> if ($this->model->create($this->params['data'])) {
$id = $this->model->insert_id;
$url = MvcRouter::admin_url(array('controller' => $this->name, 'action' => $this->_redirect_action, 'id' => $id));
$this->flash('notice', 'Successfully created!');
}else{
$this->flash('error', $this->model->validation_error_html);
}
$this->redirect($url);
By adding the if condition shown above with the '->' arrow, it will validate the add form fields also.
Thanks.
Upvotes: 1