Welcome Guest, Not a member yet? Register   Sign In
DataMapper, Validation, Crud issues
#1

[eluser]freshfutures[/eluser]
Hi,

I've done a quick search on this topic and found nothing so I thought I'd post my problem here and see if anyone knows a solution.

I'm using Codeigniter 2 and DataMapper 1.81 and I've got two problems:

1) I'm trying to display form validation errors when a form has been submitted. The validation is done in the model, however I'm not getting proper error strings like 'Name' is a required field, instead I get just 'required'.

2) I'm stumped as to how I can repopulate the form fields a user has filled in when the page is reloaded after validation fails.

My code is attached below:

Controller
Code:
if (!empty($_POST)) {
      $section = new Section_model();
      $section->name = $this->input->post('name');
      $section->description = $this->input->post('description');
      $section->validate();        

      if($section->valid) {
      // save object and return to listings
           $section->save();
           $this->template->set_message("Created the section '$section->name'", 'success');
           redirect('admin/sections/listings');
      } else {
      // display validation error messages
           $this->template->set('errors', $section->error->all);
           $this->template->render();
      }    
} else {
   $this->template->render();
}

Model
Code:
class Section_model extends DataMapper {

    var $table = 'sections';
    var $validation = array(
        'name' => array(
            'label' => 'Section Name',
            'rules' => array('required')
        ),
        'description' => array(
            'label' => 'Section Description',
            'rules' => array('required', 'min_length' => 3)
        )
    );
    
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }

    function post_model_init($from_cache = FALSE)
    {
    }
}

$section->error->all
returns an array:

Code:
array(2) { ["name"]=> string(15) "required" ["description"]=> string(15) "required" }

I'm completely stumped so any help would be greatly appreciated!
#2

[eluser]WanWizard[/eluser]
Datamapper has issues accessing language files at the moment due to changes in core libraries in recent CI (reactor) versions.

This is being looked at, but there's no solution yet.
#3

[eluser]freshfutures[/eluser]
Ah that's good to know - I checked the BitBucket repo and couldn't find any issues to do with it - is there anywhere this has been reported so I can check the progress?
#4

[eluser]WanWizard[/eluser]
It is related to issue #59.

There isn't any progress at the moment.

Main issue is that the reactor team is very busy "sealing" all core classes, i.e. making class properties and/or methods protected or private.

This means that you can't access these properties anymore from outside the class, the only way to do something is via the creation of a MY_ extension.Which is a nightmare for all external libraries, as you can only have one extension. How are you, as a CodeIgniter user, going to deal with this?

Suppose you use both HMVC, Datamapper, and some other libraries. As of now (CI 2.0.2+) you're stuck having to merge the functionality of the MY_Lang, MY_Loader and possibly more that each of these solutions provide to work around this issue. And when you have merge conflicts (which you will), you're on your own.

Imho it was very wrong to start doing this without thinking about CI's architecture. Towards the future, it will make it nearly impossible to provide 3rd party libraries if there is no proper way to integrate into CI's core.
#5

[eluser]freshfutures[/eluser]
[quote author="WanWizard" date="1310485062"]It is related to issue #59.

There isn't any progress at the moment.

Main issue is that the reactor team is very busy "sealing" all core classes, i.e. making class properties and/or methods protected or private.

This means that you can't access these properties anymore from outside the class, the only way to do something is via the creation of a MY_ extension.Which is a nightmare for all external libraries, as you can only have one extension. How are you, as a CodeIgniter user, going to deal with this?

Suppose you use both HMVC, Datamapper, and some other libraries. As of now (CI 2.0.2+) you're stuck having to merge the functionality of the MY_Lang, MY_Loader and possibly more that each of these solutions provide to work around this issue. And when you have merge conflicts (which you will), you're on your own.

Imho it was very wrong to start doing this without thinking about CI's architecture. Towards the future, it will make it nearly impossible to provide 3rd party libraries if there is no proper way to integrate into CI's core.[/quote]

I've noticed that when I was integrating the Ocular template library into my project.

Having worked in Zend exclusively for the last few months, it's been a bit difficult coming back to CI. The framework feels limited; especially when compared to the complete flexibility you get with Zend.

What would you suggest as a temporary work around for now?
#6

[eluser]WanWizard[/eluser]
I don't have one at the moment.

The only option, and advocated by CodeIgniter, is to provide MY_xxx versions of every core class that needs to be extended in the Datamapper distribution.

And leave the problem of sorting out what to do when you already have one (with potentionally the same methods, but with conflicting functionality) to the user...

For myself, the suggestion was very simple. I don't develop new applications in CodeIgniter anymore, I switched frameworks.
#7

[eluser]whoisinkus[/eluser]
[quote author="freshfutures" date="1310442640"]Hi,
1) I'm trying to display form validation errors when a form has been submitted. The validation is done in the model, however I'm not getting proper error strings like 'Name' is a required field, instead I get just 'required'.
[/quote]

I found that the datamapper_lang file lacked a 'required' entry, I added one like this on line 16:

Code:
$lang['required']    = 'The %s field is required.';

And that resolved the 'required' validation issue.

[quote author="freshfutures" date="1310442640"]Hi,
2) I’m stumped as to how I can repopulate the form fields a user has filled in when the page is reloaded after validation fails.
[/quote]

I've found that just using the form_value() function still works while using Datamapper validation. Are you getting different results?

The site I'm working on is using CI 1.7 and I realize you're using 2, so perhaps that has something to do w/ it, but thought I throw that out there and see if it got you anywhere.
#8

[eluser]freshfutures[/eluser]
[quote author="whoisinkus" date="1311479325"]
I found that the datamapper_lang file lacked a 'required' entry, I added one like this on line 16:

Code:
$lang['required']    = 'The %s field is required.';
[/quote]

Thanks for posting, I should have updated this thread as I managed to fix the issue by doing exactly the same thing you mentioned by copying the form_validation language strings into the datamapper_lang file Smile

Quote:I've found that just using the form_value() function still works while using Datamapper validation. Are you getting different results?

The site I'm working on is using CI 1.7 and I realize you're using 2, so perhaps that has something to do w/ it, but thought I throw that out there and see if it got you anywhere.

To confirm it works in Reactor as well, the problem existed between keyboard and chair Wink
#9

[eluser]Andy78[/eluser]
[quote author="WanWizard" date="1310485062"]It is related to issue #59.

There isn't any progress at the moment.

Main issue is that the reactor team is very busy "sealing" all core classes, i.e. making class properties and/or methods protected or private.

This means that you can't access these properties anymore from outside the class, the only way to do something is via the creation of a MY_ extension.Which is a nightmare for all external libraries, as you can only have one extension. How are you, as a CodeIgniter user, going to deal with this?

Suppose you use both HMVC, Datamapper, and some other libraries. As of now (CI 2.0.2+) you're stuck having to merge the functionality of the MY_Lang, MY_Loader and possibly more that each of these solutions provide to work around this issue. And when you have merge conflicts (which you will), you're on your own.

Imho it was very wrong to start doing this without thinking about CI's architecture. Towards the future, it will make it nearly impossible to provide 3rd party libraries if there is no proper way to integrate into CI's core.[/quote]


I'm bit confused with whats going on with codeigniter now. I'm using version 2.0.2 and the validation messages show for me without any problems. I'm not sure if I am using the reactor version or not. can you still download the non-reactor version of CI 2.0.2 that does not have the sealing changes to the classes?
#10

[eluser]WanWizard[/eluser]
Currently it depends a bit on how you use it, the sequence of libraries loaded, if you use MY_ libraries or not. It also depends on which version of Datamapper you use. 1.8.0 works fine, but generates lots of errors in the logs about missing language strings. Which can't be worked around at the moment due to the remarks above.

If it works for you, don't touch anything... Wink




Theme © iAndrew 2016 - Forum software by © MyBB