Validating multiple fields with the same name

前端 未结 1 1376
借酒劲吻你
借酒劲吻你 2021-01-07 14:15

I have a view file that looks like this:

Form->create(\'ModelName\');
        echo $this->Form->input(\'ModelName.0.firs         


        
相关标签:
1条回答
  • 2021-01-07 15:16

    Model::set is used for assigning a single set/record of data to the current instance of the Model. So it may be possible that you are only validating the first set of data in your POST data. You would have to iterate through each record in the POST data, Model::set it to the Model data, and then call Model::validates.

    Instead of the above method or Model::validateMany, try using Model::saveAll without actually saving.

    http://api20.cakephp.org/class/model#method-ModelsaveAll

    validate: Set to false to disable validation, true to validate each record before saving, 'first' to validate all records before any are saved (default), or 'only' to only validate the records, but not save them.

    <?php
        if ($this->request->is('post')) {
            if ($this->ModelName->saveAll($this->request->data, array('validate' => 'only'))) {
                echo $this->Session->setFlash('Success');
            } else {
                echo $this->Session->setFlash('Failure');
            }
        }
    ?>
    
    0 讨论(0)
提交回复
热议问题