Welcome Guest, Not a member yet? Register   Sign In
[Deprecated] DMZ 1.5.3 (DataMapper OverZealous Edition)

[eluser]Mirage[/eluser]
Hi - I've got a mixed bag of problems.

1. The array extension does not seem to import non-table fields unless specifically required. I suppose this could be by design, but don't quite understand. Of course a workaround is to set them explicitly

2. The matches rules appears to ignore empty fields

Account Model
Code:
class Account extends DataMapper {

    var $validation = array(
        'password' => array(
            'rules' => array('trim', 'required', 'ucfirst', 'max_length' => 10, 'min_length' => 4),
            'label' => 'Password'
        ),
        
        // This field is not stored in the db
        'confirm_password' => array(
            'rules' => array('matches'=>'password'),
            'label' => 'Match Password'
        )
    );
}

Somewhere in the controller
Code:
function _validate_subform() {

    $user    = new Sae_User();
    $account = new Account();
            
    $user->from_array($_POST);  // firstname, lastname and email address
    $account->from_array($_POST); // password and confirm_password
            
    // confirm password not being imported, because non-existent ?
    $account->confirm_password = $this->input->post('confirm_password');

    // need to do custom validation because more than one object submitted from form
    // and objects not immediately saved (multi-page form)
    $user->validate();
    $account->validate();
    
    // both objects need to be valid to continue
    $valid = $user->valid && $account->valid;

    //merge errors for ouput          
    $errors= array_merge($user->error->all, $account->error->all);

    var_dump($errors);

In the above example, if I don't submit a password, I get a proper error.
If I do submit a different password and confirm_password, I get a proper error.
If I do submit a password but no confirm_password, Account validates (it shouldn't)

Thanks,
-m

[eluser]emorling[/eluser]
Thanks OverZealous, for your earlier reply.

I have a Advanced Multiple Relationships to the Same Model.

In this case the Book can have multiple Shelves.

I am using the render_form function as in your example.

I can add books to shelves, and vice versa using the form. Works nicely.

But deletion gives

Code:
An Error Was Encountered

Unable to relate book with shelf.

Is this a known issue with 'Multiple Relationships to the Same Model' and deletion using render_form? Or do I have another error in my setup, thx?

[eluser]Mirage[/eluser]
Some more trouble with manual form generation, manual validation and non-existing fields. Outputting error messages for unknown fields causes php notices:

Code:
<input type="text" name="cccvv" value="<?= $sae_transaction->cccvv ?>" class="small-input cr-s" /><?= $sae_transaction->error->cccvv ?>

$sae_transaction->cccvv will output nothing silently. But $sae_transaction->error->cccvv will throw a php notice for non-existing property.

I think error needs to pre-populate non-db fields from the validation rules the same as db-fields;

Also, curiously enough the cccvv is required as user input and needs to be validated but should never be saved in the database. I'm seeing more trouble ahead for automatic validation on save()?

[eluser]demogar[/eluser]
It would be great to use DataMapper Z with the native form_validation from CodeIgniter. I'm having some troubles to repopulate the form.

The only way I discovered to do it is passing the data submitted and doing an echo of them like this:

Code:
With DataMapper:
- In the controller something like this:
$this->load->view('template', $data)

- In the view something like this:
<?php echo form_input('username', htmlspecialchars($data->username)); ?>


But without DataMapper and using the native Form_Validation it would be simplier with only:
- <?php echo form_input('username', set_value('username')); ?>

Is there any other idea to implement this with the native form_validation? I think the form_validation from CI is really great but this library is great too. The only thing I don't like is how to implement those kind of actions because it would simplier without the DMZ.

I would like to know what you think about and how would you do this.

Thank you in advance.

[eluser]OverZealous[/eluser]
[quote author="Mirage" date="1253157098"]1. The array extension does not seem to import non-table fields unless specifically required.[/quote]

This is by design. It makes no sense to import everything the user sends into the object, because it might be intended for a different object, and might be a security risk. You can easily set the fields you expect, however.

Quote:2. The matches rules appears to ignore empty fields

You didn't set the field to required. (Note: there was a typo on that page that said 'related' not 'required' before just now.)

Quote:I think error needs to pre-populate non-db fields from the validation rules the same as db-fields;
This would be impossible. There are an infinite number of possible words that might not have an error.


The form and array extensions are designed mostly to work with database fields. Of course, the form library can handle printing non-database fields. You just need to process them manually.

[eluser]OverZealous[/eluser]
[quote author="emorling" date="1253157245"]
But deletion gives
Code:
Unable to relate book with shelf.
[/quote]

Why does my relationship work from one object, but fail from the other?

You only see that error if you are requesting a relationship that is incorrectly configured.

[eluser]OverZealous[/eluser]
[quote author="Demostenes Garcia" date="1253167819"]It would be great to use DataMapper Z with the native form_validation from CodeIgniter. I'm having some troubles to repopulate the form.[/quote]

First, DMZ does use the native Form_Validation library for validating. However, you have to do it in a completely different manner. Instead of passing individual fields to the View, pass the entire object. Then print out the object directly. The usual method looks like this:
Code:
$user = new User();
$user->get_by_id($userid);

if($this->input->post('name')) {
    // process form
    $user->name = $this->input->post('name');
    $user->whatever = ...
    ...
    if($user->save()) {
        // redirect
    }
}

$this->load->view('form', array('user' => $user));

Then, in your view, just output $user->name or whatever. Everything the user submitted is automatically processed returned. This also allows you to easily provide error feedback on individual fields.

There are plenty of other examples throughout the extensive documentation, but you can save yourself a ridiculous amount of time by using the included extensions for form handling. You can use them together or separate.

(Also, you aren't using the CI input methods correctly, because they automatically handle escaping the values for you. There is no reason to call htmlspecialchars(). See the form_prep documentation for info.)

[eluser]OverZealous[/eluser]
@ZeusChicago

First off, I see some (possibly unrelated) problems:

1) There's no reason to manually query the fa_user_id. Instead, use the supported where_related method:
Code:
$subModel->where_related('fa_user', 'id', $this->db_session->userdata('id'));

This is less error prone, and less risky.

2) join_related is deprecated. Although this won't cause a problem for now, it has been replaced with include_related

3) Your SubscriptionType model is incorrect. You always need to describe both sides of the relationship. It must have a $has_many that includes subscription. This is in the troubleshooting guide as things to look for. I have to assume your fa_user model is incorrect, as well.

Beyond those problems, I can only assume you have a configuration error in CodeIgniter or DMZ, you are using a class to extend the DataMapper class that is causing conflicts, or something is happening in code outside the code you posted.

Please try the previously recommended hint of seeing what the database is returning, try stripping down your query to fundamental parts (say, just querying for all subscriptions with the given fa_user id), or some other basic debugging.

You also may want to look over the Change Log to see what features might have changed in the past.

[eluser]Mirage[/eluser]
Phil,

Thanks for the feedback.

New question - why does DMZ do a query every time a dmz object is created?

Thanks,
m

[eluser]OverZealous[/eluser]
[quote author="Mirage" date="1253171236"]New question - why does DMZ do a query every time a dmz object is created?[/quote]

I assume you are referring to these queries:
Code:
SELECT * FROM $table LIMIT 1

If so, those are used by CI to determine the names of the columns on the table. DMZ needs the result of these queries to work. (It's a core feature.)

They are only called once per model class, per session. They aren't used every time a model is created (every copy of a model shares a copy field data, validation info, and more).

Now, there are two things about these:

1) Almost without question, they have no real performance impact. I've tested those queries on my server, and the DB returns them in like 0.001 seconds.

2) If they really bother you, when you head off to production, enabled the Production Cache. But please heed the big red warnings on that page. I doubt it will provide any performance increase for less than 100 page views/minute, so testing is worth it.




Theme © iAndrew 2016 - Forum software by © MyBB