views:

39

answers:

1

Using CakePHP 1.3.

I've created a form with a primary model that 'hasMany' secondary models.

However, the fields for the secondary models are created by Javascript ("add one" buttons). While they adhere to the CakePHP naming conventions, I'm wondering:

How do I hook up any kind of server-side, CakePHP validation to these fields, and how can I re-show them, along with their error messages, should they fail validation (since they're created on the fly)?

I'm assuming that my controller or view will actually have to determine if there was data posted for the given models, attempt to validate them, and if necessary I will have to "manually" create and show the HTML for the previously-dynamically-created fields. Is that correct?

A: 

If you alter the html to add the field , then it gets submitted to server side validation on the server.
You can put a div tag to display the errors like so:

<div>
    <?php echo $this->error('each_field'); ?>
</div>

If you have these out there, then they only get displayed when there is actually an error, then it gets populated there, and then you can shift the resulting error next to the specific field that failed validation.

chustar
Should that look like `$this->error('Model.0.fieldname')`? [where 0 is changed for each]
anonymous coward