Saving Form Data

jcastillotx - 4 months ago

So I have a form that has an array for child id. I need to save a single row for each child id based off the form data being saved. I have a formhandler and trying to get it to save everything before resetting form. Look for some tips.

piterden - 4 months ago

Are you absolutely sure about the storing method of your relation? I would better store inside child parent_id, than the array of child ids inside a parent. Try to use onBuild() method in the FormBuilder to handle form. What you mean resetting?

ryanthompson - 4 months ago

Hmm.. can you post some code / images to help convey what you're trying to do? I should be able to point you in the right direction.

jcastillotx - 4 months ago

So I have a sponsors module, streams are sponsors and children. I originally had the form as one sponsor one child but the client wants the sponsor to be able to sponsor multiple children with one form. In other fashion I would have my function that checks if the child variable was a single child or multiple children. So I have a sessions that takes all the children that the sponsor wants and sends that to the form handler. What I would like to do is save to the sponsors stream the single child with the sponsor information.

piterden - 4 months ago

Please, show migrations config

jcastillotx - 4 months ago

What are you looking for with migrations? All migrations will show are the different pieces.

Sponsors - Sponsor Name, Email, Phone, Address, Child Relationship by Child_ID
Child - Name, Age, Location, School

jcastillotx - 4 months ago

Is there a way to manual save the stream within the form handler instead of doing $builder->saveForm(); we do $builder->resetForm();

ryanthompson - 4 months ago

You can do something like $builder->getFormEntry()->save(); which would pull the entry out of the form - and save it manually.

If you're handling the form logic yourself just make sure you don't run post or it'll double save basically.

jcastillotx - 4 months ago

Ok it savea a row but doesn't save the data. Is there something I am missing? This is in formhandler.

@package Modules\SponsorshipsModule\Sponsor\Form
 */
class SponsorFormHandler {
    
    public function handle( FormBuilder $builder, MessageBag $message) {
        
        // See if it can save the form
        if ( ! $builder->canSave() ) {
            return;
        }
        
        // Make Sure Passwords Match
        if ( $builder->getFormValue( 'password' ) != $builder->getFormValue( 'cfpassword' ) ) {
            
            $message->error( 'Passwords do not match!' );
            
            return;
            
        }
        
        // Is it coming from the front form
        if ( isset( $_POST['front'] ) || $builder->getFormValue( 'sponsor_payment' ) == 'Credit' ) {
            
            $url      = 'https://fts.cardconnect.com:6443/cardconnect/rest';
            $user     = 'testing';
            $password = 'testing123';
            
            
            // Set up the API
            $client = new CardConnectRestClient( $url, $user, $password );
            
            // Send a request
            $request = array(
                'merchid'   => '496160873888',
                'accttyppe' => $builder->getFormValue( 'sponsor_card' ),
                'account'   => isset( $_POST['sponsor_cc'] ) ? $_POST['sponsor_cc'] : null,
                'expiry'    => $builder->getFormValue( 'sponsor_exp_m' ) . $builder->getFormValue( 'sponsor_exp_y' ),
                'cvv2'      => $builder->getFormValue( 'sponsor_ccv' ),
                'amount'    => isset( $_POST['childfee'] ) ? $_POST['childfee'] : null,
                'currency'  => "USD",
                'orderid'   => 'DN' . sprintf( '%05d', rand( 0, 99999 ) ),
                'name'      => $builder->getFormValue( 'sponsor_first' ) . ' ' . $builder->getFormValue( 'sponsor_last' ),
                'street'    => $builder->getFormValue( 'sponsor_address' ),
                'city'      => $builder->getFormValue( 'sponsor_city' ),
                'region'    => $builder->getFormValue( 'sponsor_state' ),
                'country'   => "US",
                'postal'    => $builder->getFormValue( 'sponsor_zip' ),
                'tokenize'  => "Y",
            );
            
            // Grab the response
            $response = $client->authorizeTransaction( $request );
            
            // Was it a approval
            if ( $response['resptext'] != "Approval" ) {
                
                $builder->setSave( false );
                $builder->addFormError( 'sponsor_card', $response['resptext'] );
                $message->error( $response['resptext'] );
                
                return;
            }
            
            
            // Add to the form
            $builder->setFormValue( 'sponsor_cardtoken', $response['token'] );
            $builder->setFormValue( 'sponsor_retref', $response['retref'] );
            $builder->setFormValue( 'sponsor_response', $response['resptext'] );
            $builder->setFormValue( 'fee', isset( $_POST['childfee'] ) ? $_POST['childfee'] : null );
            $builder->setFormValue( 'sponsor_payment', isset( $_POST['childfee'] ) ? $_POST['childfee'] : null );
            
            // Empty the Credit Card Info
            $builder->setFormValue( 'sponsor_cc', '0000000000' );
            
            
            if ( isset( $_POST['multiple'] ) ) {
                
                $children = isset( $_POST['children'] ) ? $_POST['children'] : null;
                
                foreach ( $children as $key => $value ) {
                    
                    $builder->setFormValue( 'sponsor_child', $key );
                    
                }
                
                // Save the Form
                $builder->getFormEntry()->save();
                
            } else {
                
                // Add to the form
                $builder->setFormValue( 'sponsor_cardtoken', $response['token'] );
                $builder->setFormValue( 'sponsor_retref', $response['retref'] );
                $builder->setFormValue( 'sponsor_response', $response['resptext'] );
                $builder->setFormValue( 'sponsor_child', $_POST['child'] );
                
                // Empty the Credit Card Info
                $builder->setFormValue( 'sponsor_cc', '0000000000' );
                
                // Setup Email
                $request = array(
                    'email'  => $builder->getFormValue( 'sponsor_email' ),
                    'phone'  => $builder->getFormValue( 'sponsor_phone' ),
                    'name'   => $builder->getFormValue( 'sponsor_first' ) . ' ' . $builder->getFormValue( 'sponsor_last' ),
                    'street' => $builder->getFormValue( 'sponsor_address' ),
                    'city'   => $builder->getFormValue( 'sponsor_city' ),
                    'region' => $builder->getFormValue( 'sponsor_state' ),
                    'amount' => $builder->getFormValue( 'childfee' ) . '.00',
                    'postal' => $builder->getFormValue( 'sponsor_zip' ),
                    'child'  => isset( $_POST['childname'] ),
                    'date'   => date( 'M d, Y' )
                );
                
                // Send an Email
                Mail::to( $builder->getFormValue( 'sponsor_email' ) )->send( new HasSponsored( $request ) );
                
                // Save the Form
                $builder->saveForm();
                
            }
            
        }
        
        $builder->resetForm();
        
    }
    
}