CodeIgniter Forums
Form Generation Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Form Generation Library (/showthread.php?tid=16439)



Form Generation Library - El Forum - 12-24-2009

[eluser]macigniter[/eluser]
[quote author="combatCoder" date="1261609025"]Any ideas why this is not validating?
Does anyone know if there is a issue with using the onchange to submit and not a button?[/quote]

Actually it is the missing submit() button that is causing the form not to validate. The form is checking the value of the submit button to check whether the form was submit or not. So this is a tricky one.

For now my best solution is adding the following code to the library:

Code:
// add this just below function set_value() in libraries/Form.php

/**
* Trigger
*
* Set a submit trigger if no submit button is
* included in the form. Whenever this trigger
* element is posted, the form is considered submit
*/    
function trigger($name='')
{
    if ( ! $name) show_error(FGL_ERR.'trigger: No element name specified');
    $this->_submit_name[] = $name;
    return $this;
}

Then you can simply do:

Code:
$type = array(
    'default'  =>    '--- Select a Report ---',
    'user'     =>    'Create an User Report',
    'errors'   =>    'Create an Errors Report',
    'office'   =>    'Create an Office Report',
    'eom'      =>    'Create an EOM Report'
);
    
$this->form->open('picker/index','picker')
->select('type', $type, 'Please Select the desired Report:', 'default', '', 'onchange="document.picker.submit()"')
->trigger('type') // specify the element which will be used as the form submit trigger
->validate();

if($this->form->valid)
{
    echo 'Thunderbirds are GO!';
} else {
    echo 'Houston we have a problem';
}

I think that this trigger() method is the best solution for now. I will add it to the next offical version. Thanks for pointing this out!


Form Generation Library - El Forum - 12-24-2009

[eluser]macigniter[/eluser]
off topic: MERRY CHRISTMAS EVERYONE :-D


Form Generation Library - El Forum - 12-25-2009

[eluser]Jakebert[/eluser]
Help!

I'm trying to to an update form, I finally got the values to populate from the DB:
Code:
function edit($sid)
    {    
            $data = $this->students->fetch($sid);
            
            $genders[] = array('male', 'Male');
            $genders[] = array('female', 'Female');
            
            $this->form->open('admin/students/edit/'.$sid)
            ->text('fname', 'First Name', 'trim|required|alpha|xss_clean', $data->fname)
            ->text('lname', 'Last Name', 'trim|required|alpha_numeric|xss_clean', $data->lname)
            ->text('age', 'Age', 'trim|numeric|required', $data->age)
            ->radiogroup('gender', $genders, 'Gender:', '', 'required')
            ->br()
            ->text('level', 'Level', 'trim|required|alpha_numeric|xss_clean', $data->level)
            ->submit()
            ->reset()
            ->onsuccess('redirect', 'admin/students/edit_success')
            ->model('student_model', 'edit');        
            
            $form['form'] = $this->form->get();
            $form['errors'] = $this->form->errors;
            
            $this->auth->view('students/edit', $form);
    }

My code is not throwing any errors, but here is the model:

Code:
function edit(&$form, $data)
        {
            $stuff['sid'] = $data['sid'];
            $stuff['fname'] = $data['fname'];
            $stuff['lname'] = $data['lname'];
            $stuff['age'] = $data['age'];
            $stuff['level'] = $data['level'];
        
            $this->db->update('students', $stuff, array('sid' => $sid));
            return $this->db->affected_rows();
            
        }

Why are my entries not updating?

Thanks!


P.S. I find it annoying that the onsuccess() method forces you to call another function instead of just going to a view. Maybe that could be something for future updates?


Form Generation Library - El Forum - 12-25-2009

[eluser]hugle[/eluser]
[quote author="Jakebert" date="1261746420"]Help!

I'm trying to to an update form, I finally got the values to populate from the DB:
Code:
function edit($sid)
    {    
            $data = $this->students->fetch($sid);
            
            $genders[] = array('male', 'Male');
            $genders[] = array('female', 'Female');
            
            $this->form->open('admin/students/edit/'.$sid)
            ->text('fname', 'First Name', 'trim|required|alpha|xss_clean', $data->fname)
            ->text('lname', 'Last Name', 'trim|required|alpha_numeric|xss_clean', $data->lname)
            ->text('age', 'Age', 'trim|numeric|required', $data->age)
            ->radiogroup('gender', $genders, 'Gender:', '', 'required')
            ->br()
            ->text('level', 'Level', 'trim|required|alpha_numeric|xss_clean', $data->level)
            ->submit()
            ->reset()
            ->onsuccess('redirect', 'admin/students/edit_success')
            ->model('student_model', 'edit');        
            
            $form['form'] = $this->form->get();
            $form['errors'] = $this->form->errors;
            
            $this->auth->view('students/edit', $form);
    }

My code is not throwing any errors, but here is the model:

Code:
function edit(&$form, $data)
        {
            $stuff['sid'] = $data['sid'];
            $stuff['fname'] = $data['fname'];
            $stuff['lname'] = $data['lname'];
            $stuff['age'] = $data['age'];
            $stuff['level'] = $data['level'];
        
            $this->db->update('students', $stuff, array('sid' => $sid));
            return $this->db->affected_rows();
            
        }

Why are my entries not updating?

Thanks!


P.S. I find it annoying that the onsuccess() method forces you to call another function instead of just going to a view. Maybe that could be something for future updates?[/quote]

Hello,

I think you should switch:
Code:
->model('student_model', 'edit');
  ->onsuccess('redirect', 'admin/students/edit_success')

in that way, first the model is called, then you are redirected to 'success' controller

Good luck Smile


Form Generation Library - El Forum - 12-25-2009

[eluser]hugle[/eluser]
[quote author="macigniter" date="1261672846"]off topic: MERRY CHRISTMAS EVERYONE :-D[/quote]

same with you Smile)))
all the good to you, and anyone else reading and not reading this post Smile))


Form Generation Library - El Forum - 12-25-2009

[eluser]Jakebert[/eluser]
Fixed. Had to add $sid as a hidden input to the form. merry christmas!


Form Generation Library - El Forum - 12-25-2009

[eluser]Jakebert[/eluser]
New question now (should I make a new thread for this, btw?):

Is there a more efficient way to do what I'm doing now which is:
Code:
$this->form->open('admin/students/add')
            // the rest of the form
            ->submit()
            ->model('student_model', 'add') //
            ->onsuccess('redirect', 'admin/students/add_success');

and then have an add_success() function that just redirects to a view AND in the model:
Code:
$sid = $data['sid'];
      $stuff['sid'] = $sid;
      $stuff['fname'] = $data['fname'];
      $stuff['lname'] = $data['lname'];
      $stuff['age'] = $data['age'];
      $stuff['level'] = $data['level'];
            
            $this->db->update('students', $stuff, array('sid' => $sid));
            return $this->db->affected_rows();

Is there no way to get the form to send the required data directly to the model without having to do all that transferring?


Form Generation Library - El Forum - 12-25-2009

[eluser]dinhtrung[/eluser]
@macigniter: Can't wait for the new release anymore... Is there any schedule for the new version already? I'd like to help, too.


Form Generation Library - El Forum - 12-25-2009

[eluser]macigniter[/eluser]
[quote author="Jakebert" date="1261750184"]Is there no way to get the form to send the required data directly to the model without having to do all that transferring?[/quote]

Sure. Just don't use the onsuccess() method. Instead do something like this:

Code:
$this->form
->open()
->...;

$this->form->validate();

if ($this->form->valid)
{
    // do stuff
}

$data['form'] = $this->form->get();



Form Generation Library - El Forum - 12-25-2009

[eluser]hugle[/eluser]
[quote author="dinhtrung" date="1261752249"]@macigniter: Can't wait for the new release anymore... Is there any schedule for the new version already? I'd like to help, too.[/quote]

soon, very soon Smile