简体   繁体   中英

CakePHP 1.3: How to display form validation error messages when form is not tied to models?

I have a view where I used FormHelper methods ( $this->Form->input , etc.) to create a form (post), but this form is not tied to any model. It's a dumb form.

For example, some fields are date fields. My controller will do some validation on these fields, but if there is a problem, how would I display the error message right below the field that had a validation error? With forms tied to models, CakePHP will automagically add a div to the relevant field to display the validation error message. Is there something similar for dumb forms?

Thank you for the assistance.

Use a model which isn't associated with a db table. Rest will be same as using a regular db backed model. Eg:

// Model
class Dummy extends Model {
   public $useTable = false;
   public $validate = array('somefield' => 'notEmpty');
}


// View
echo $this->Form->create('Dummy');
echo $this->Form->input('somefield');
......

// Controller
public some_action() {
    //if post request
    $this->Dummy->set($this->request->data);
    $this->Dummy->validates();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM