![]() |
DMZ 1.7.1 (DataMapper OverZealous Edition) - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: DMZ 1.7.1 (DataMapper OverZealous Edition) (/showthread.php?tid=28550) |
DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 05-05-2010 [eluser]lsemel[/eluser] Hi Phil, I was thinking about what the best way to integrate DataMapper model validation with regular CI form validation. I ended up writing an extension to the form validator in CI that lets you process forms involving DataMapper models in only a few lines of code. Assuming you have a DataMapper object Status that has its own validations, and your form's fields match the fields from Status, you can write this: Code: $status = new Status(); Here's the full code: Code: <?php Is there a better way to integrate the two distinct types of validation? DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 05-05-2010 [eluser]12vunion[/eluser] [quote author="NachoF" date="1273043831"]I just ran into a little trouble I need to use the sql query syntax like so. Code: $o=new Object(); I get Database error Quote:ERROR: column "id=2" does not existAm I missing something obvious?[/quote] Try: Code: $o=new Object(); DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 05-05-2010 [eluser]OverZealous[/eluser] @lsemel I don't know if you are aware, but DMZ already uses the CodeIgniter Form_Validation library. Any rules under Form_Validation are available to DMZ models. You shouldn't use Form_Validation the way you have, because DMZ has internal validation routines that are called on every save. It means you don't have to manually check the rules ever. The normal usage is this: Code: $object = new Object(); DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 05-05-2010 [eluser]lsemel[/eluser] Got it... I had a some cases where I wanted to keep the bulk of the validation rules within my models, but add others for other fields that exist on the form but don't correspond to one in a model, and didn't want to have to deal with those two sources of rules separately. And didn't want to manually copy all the fields from the form into my model with a bunch of $model->whatever = $this->input->post('whatever'), that should happen automagically. And wanted to display errors, wherever they came from, using the same code <?= form_error('field') ?> in my views, and not have the views worry about whether an error comes from the form validation class or DataMapper. So this solves for the problem of having two distinct sources of validation rules. DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 05-05-2010 [eluser]OverZealous[/eluser] [quote author="lsemel" date="1273112453"]I had a some cases where I wanted to keep the bulk of the validation rules within my models, but add others for other fields that exist on the form but don't correspond to one in a model...[/quote] Seems like you know this, but you can add validation for "virtual" fields, like the confirm_password example. However, they can get in the way when you don't need them. Quote:And didn't want to manually copy all the fields from the form into my model with a bunch of $model->whatever = $this->input->post('whatever') See the Array extension, using from_array! ;-) Quote:And wanted to display errors, wherever they came from, using the same code <?= form_error('field') ?> in my views, and not have the views worry about whether an error comes from the form validation class or DataMapper. You also can add your own errors to the object's error, using error_message. This might help, as well. (You can get a specific error message using object->error->{$field}.) DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 05-05-2010 [eluser]lsemel[/eluser] Thanks, I'll check that out. I was mainly looking for a consistent syntax for doing form validation sitewide. Some forms don't use datamapper objects, some do. Normally there are two distinct syntaxes for validating forms and displaying errors, depending on if you're working with a model or not. I want to be able to tell everyone on the site to always do things one way, and add the one line of code if they are validating against a model. DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 05-06-2010 [eluser]Alface[/eluser] For everyone know, I posted a new modification on codeigniter wiki http://codeigniter.com/wiki/HMVC_for_DMZ_Modular_Separation/ DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 05-08-2010 [eluser]Atas[/eluser] Hello, this orm is great. But i have a question. I want to set a One to many relationship, i have the following tables: Publication -id -name - other fields Image -id -publication_id -image_src Image has many linked records from publciation table. Must i use $has_one, $has_many or what in the models class? I don't want to create a dedicated table to do this. Thank in advance ! DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 05-08-2010 [eluser]OverZealous[/eluser] @Atas You can use In-Table Foreign Keys for Many-to-One or One-to-One relationships, without a dedicated join table. See In-Table Foreign Keys on the Database Tables page for more information. DMZ 1.7.1 (DataMapper OverZealous Edition) - El Forum - 05-08-2010 [eluser]Atas[/eluser] Thank you for your response! I have the following code. Code: /***controller***/ My models are: Code: /*** Model Publication ***/ I don't want to annoy you but i dont understand how build my models to do this. Thanks in advance. |