Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

default form values

edited March 2014 in Framework

How can I set default values for a form just before inserting/updating record; for example calculated columns.

I would like also some columns remain null in order to use database default (like current timestamp).

I looked at "long form usage" in http://api.elefantcms.com/visor/lib/Form but I don't know if it is the right way.

Thanks for help.

Comments

  • Before the echo $form->handle() is called, you can assign default values like this:

    $form->data = array (
        'field_name' => 'default value'
    );
    

    Fields that just get inserted into the DB, but don't need to be edited directly (e.g., current time), can be left out of $form->data and just added later, like this:

    echo $form->handle (function ($form) {
        $obj = new myapp\SomeModel (array (
            'field_name' => $_POST['field_name'],
            'created_on' => time ()
        ));
        $obj->put ();
    });
    
  • I was doing as in the second example without success using time() function.

    I solved it using: date('Y-m-d H: i: s')

    Many thanks.

Sign In or Register to comment.