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

Flourish Flow

posted by imfael 8 years ago

I leave Codeigter beacause I love Flurish Features but I can't migrate my code from my previous Apps to Flourish

This is my Controller. I set a few rules after this I run the validation if it fails it loads the view and show the errors otherwise it insert the user into de database and redirect to other controller.

public function insert() {
	
	$this->form_validation->set_rules('firstname', 'Nombre', 'required|trim');
	$this->form_validation->set_rules('lastname', 'Apellidos', 'required|trim');
	$this->form_validation->set_rules('email', 'Correo Electrnico', 'required|trim|valid_email');
	$this->form_validation->set_rules('password', 'Contrasea', 'required|trim|sha1');
	
	if ( $this->form_validation->run() ) {
	
		$user->firstname   = $this->input->post('firstname');
		$user->lastname    = $this->input->post('lastname');
		$user->email       = $this->input->post('email');
		$user->password    = $this->input->post('password');
		$user->created     = now();
		$user->modified    = now();
		
		$this->users->insert($user);
		
		redirect('authentication/');
	
	} else {
	
		$data->view      = 'user/insert';
		
		$this->load->view('store/index', $data);
	
	}

}

View

#!text/html
<?php echo form_open('user/insert', array('class' => 'basic')); ?>
	<div class="inner-form">
		<?php if ( validation_errors() != '' ) : ?>
		<div class="msg msg-error"><?php echo validation_errors(); ?></div>
		<?php endif; ?>				
		<dl>
			<dt><?php echo form_label('Nombre', 'firstname'); ?></dt>
			<dd>
				<?php
					$input = array(
						'name'  => 'firstname',
						'class' => 'txt',
						'value' => set_value('firstname')
					);
					echo form_input($input);
				?>
			</dd>
			<dt><?php echo form_label('Apellidos', 'lastname'); ?></dt>
			<dd>
				<?php
					$input = array(
						'name'  => 'lastname',
						'class' => 'txt',
						'value' => set_value('lastname')
					);
					echo form_input($input);
				?>
			</dd>
			<dt><?php echo form_label('Correo Electrnico', 'email'); ?></dt>
			<dd>
				<?php
					$input = array(
						'name'  => 'email',
						'class' => 'txt',
						'value' => set_value('email')
					);
					echo form_input($input);
				?>
			</dd>
			<dt><?php echo form_label('Contrasea', 'password'); ?></dt>
			<dd>
				<?php
					$input = array(
						'name'  => 'password',
						'class' => 'txt pwd'
					);
					echo form_password($input);
				?>
			</dd>
		</dl>				
	</div>
<?php echo form_close(); ?>

I think that I should use isPost() method but I don't know how to merge fValidation and the exceptions to my previous code.

I don't know Code Igniter, but here is what I think the translation would be:

// This model will introspect the database and validate data against the schema
// Be sure to set fields that are required as NOT NULL in the database
class User extends fActiveRecord
{
    protected function configure()
    {
        // This makes the column be validated as an email address
        fORMColumn::configureEmailColumn($this, 'email');

        fORM::overrideColumnName($this, 'firstname', 'Nombre');
        fORM::overrideColumnName($this, 'lastname',  'Apellidos');
        fORM::overrideColumnName($this, 'email',     'Correo Electrnico');
        fORM::overrideColumnName($this, 'password',  'Contrasea');
    }

    // This hashes passwords with a salt and many rounds of hashing
    public function setPassword($password)
    {
        // Skip empty passwords
        if (!strlen($password)) {
            return $this;
        }
        return $this->set('password', fCryptography::hashPassword($password));
    }
}

class YourController
{
    public function insert($tmpl)
    {
        $user = new User();
        
        $tmpl->set(array(
            'user' => $user,
            'view' => 'store/index.php'
        ));

        if (fRequest::isPost()) {
            try {
                $user->populate();
                $user->store();

                fURL::redirect('authentication/');

            } catch (fValidationException $e) {
                fMessaging::create('error', $e->getMessage());
            }
        }

        $tmpl->place('view');
    }
}
#!text/html
<?
$user = $this->get('user');
?>
<form action="user/insert" method="post" class="basic">
    <div class="inner-form">
        <?
        fMessaging::show('error');
        ?>               
        <dl>
            <dt><label for="user-firstname"><?= fORM::getColumnName('User', 'firstname') ?></label></dt>
            <dd>
                <input id="user-firstname" type="text" name="firstname" value="<?= $user->encodeFirstname() ?>" />
            </dd>
            <dt><label for="user-lastname"><?= fORM::getColumnName('User', 'lastname') ?></label></dt>
            <dd>
                <input id="user-lastname" type="text" name="lastname" value="<?= $user->encodeLastname() ?>" />
            </dd>
            <dt><label for="user-email"><?= fORM::getColumnName('User', 'email') ?></label></dt>
            <dd>
                <input id="user-email" type="text" name="email" value="<?= $user->encodeEmail() ?>" />
            </dd>
            <dt><label for="user-password"><?= fORM::getColumnName('User', 'password') ?></label></dt>
            <dd>
                <input id="user-password" type="text" name="password" />
            </dd>
        </dl>                
    </div>
</form>

Currently there is no routing component to Flourish, however you can use Moor (beta) or Anchor (alpha) by Jeff Turcotte. At some point in the near future I think I will be adding a routing component to provide a more consistent experience for users.

posted by wbond 8 years ago

Thanks I already migrate my whole code with a minor changes. I love yout Library.

posted by imfael 8 years ago