Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

$user->populate() // Is there a method to populate with data array?

posted by mrocha.me 9 years ago

Part of my html:

#!text/html
<div class="field email">
<label for="users[email]">E-mail:</label><span class="error">* <?php print $this->get('error[email]'); ?></span>
<input type="text" name="users[email]" value="<?php print $this->get('users[email]'); ?>"/>
</div>

Part of my code:


$user = fRequest::get('users');

if($user!=NULL){
    $error=Array(
        'email'=>'campo obrigatrio', // required field
        'password'=>'campo obrigatrio' // required field
    );
    $errors=count($error);
    foreach($user as $key=>$value){
        if($value!='' && array_key_exists($key, $error)){
            if($key=="email" && !eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$", $value)) {
                $error[$key]='e-mail invlido'; // invalid e-mail
            }
            else {
                $error[$key]="";
                $errors--;
            }
        }
    }
    if($errors<=0){
        $result = User::save($user);
    } else {
        $template->set('users', $user );
    }
} else {
    $error=Array();
}

By the flourish_demo_site & documentation I could understand that I can get the data from $_POST (HTTP), but I would like to get the data from a variable array and save them.

class User extends fActiveRecord

function save($data){
...
fORMDatabase::attach($mydb);
fORM::mapClassToTable('User', 'users');
$user = fRecordSet::build('User');

try {
$user = new User();
$user->setEmail($data["email"]) 

How can I populate without having to do one-by-one?

$data produces:

[users] {
     [ID] => "",
     [email] => "myemail@myhost.com",
     [password] => "123456"
}

This might not be the best way to do that, but I like to have a separated file to render the template, verify the data and produces error messages to the user, and them have all set to save them.

By the way, congratulations for such a nice library, and thank you.

Currently there is no method to do this.

This would probably be a good candidate for writing a plugin for. If you check out the fORMJSON class, you can see a simple example of writing a plugin. You could write a plugin that creates a hydrate() method that accepts an associative array and sets the values to the $values array that is passed to the plugin. The fORM page has documentation about what parameters are passed to plugins.

posted by wbond 9 years ago