⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Separate validation rules for add and edit



michaeljean@ncf.ca
  • profile picture
  • Member

Posted 11 May 2015 - 17:31 PM

Hello,

 

I have said it before and I will say it again...I love this software!

 

I am trying to set different validation rules for the state add and edit but it does not seem to be working. Has anyone else been successful doing this?  Maybe I am not doing it correctly. I do see the different states appearing in the log. I am using code igniter 2.2 and grocery crud 1.4.1.

 

Here is my code of the controller function:

        $crud = new grocery_CRUD();
        $crud->columns( 'fm_session_id','fm_session_start_time', 'client', 'coach', 'mood', 'weather', 'awareness_level', 'client_interaction_rating', 'session_delivery_rating', 'comments', 'activities' );
        $data[ 'title' ] = 'sessions';

        $crud->set_table( 'fm_sessions' );

        $crud->change_field_type( 'created_time', 'invisible' );
        $crud->change_field_type( 'created_by', 'invisible' );
        $crud->change_field_type( 'modified_time', 'invisible' );
        $crud->change_field_type( 'modified_by', 'invisible' ); 

        $crud->set_relation( 'mood', 'mood_types', 'name' );
        $crud->set_relation( 'weather', 'weather_types', 'name' );
        $crud->set_relation( 'awareness_level', 'awareness_level_types', 'name' );
        $crud->set_relation( 'client', 'clients', 'client_full_name' );
        $crud->set_relation( 'coach', 'customers', 'customer_full_name' );
	$crud->set_relation_n_n( 'activities', 'fm_session_activities', 'activities', 'fm_session', 'activity', 'activity_name' );

        $crud->required_fields( 'client', 'coach', 'fm_session_start_time', 'activities' );

        $crud->display_as( 'fm_session_id', 'Session ID' );
        $crud->display_as( 'fm_session_start_time', 'Session Start' );
        $crud->display_as( 'awareness_level', 'Awareness Level' );
        $crud->display_as( 'client_interaction_rating', 'Client Interaction Rating' );
        $crud->display_as( 'session_delivery_rating', 'Session Delivery Rating' );

        $crud->set_subject( 'Session' );
        
        // Set the coach and client select boxes appropriately
        // Customer coaches can only see and select session elements associated with the clients assigned to them
        // Customer admins can see all info for coaches who are members of the same customer organization
        $crud->callback_add_field( 'coach', array( $this, 'get_session_coach_select' ) );
        $crud->callback_add_field( 'client', array( $this, 'get_session_client_select' ) );
        $crud->unset_edit_fields( 'activities' );
        $crud->unset_add_fields( 'comments' );
        
        // set the select element then disable and then enable it again on submit
	// This needs to remain on for the activity selection to work!
        $crud->callback_add_field( 'activities', array( $this, 'get_session_activities' ) );
        $crud->callback_edit_field( 'session_delivery_rating', array( $this, 'edit_field_callback_session_delivery_rating' ) );
        $crud->callback_edit_field( 'client_interaction_rating', array( $this, 'edit_field_callback_client_interaction_rating' ) );
        $crud->callback_before_insert( array( $this, 'insert_hidden_fields' ) );
        $crud->callback_before_update( array( $this, 'update_hidden_fields' ) );
        $crud->unset_read();
        
        // Validation rules
        $crud->set_rules( 'client', 'Client', 'required|callback_session_client' );
	$crud->set_rules( 'coach', 'Coach', 'required' );
	// This next callback will set the session id which will then be used for the session time validation rule
	$crud->set_rules( 'fm_session_id', 'Session ID', 'callback_assign_session_id' );
        	
        $crud->add_action( 'Evaluation', base_url('assets/grocery_crud/themes/flexigrid/css/images/lightbulb.png'), 'sessionactivities', '' );
        $crud->add_action( 'Download', base_url('assets/images/doc.png'), 'sessions/download', '' );

        // Try to separate the session start time validation rules for add and edit
        $state = $crud->getState();
        $state_info = $crud->getStateInfo(); 
        log_message( 'info', 'sessions.index: the current state is ' . $state );
        if( $state == 'add' )
        {
            $crud->set_rules( 'fm_session_start_time', 'Session Start', 'required|trim|xss_clean|callback_check_date_time_format|callback_check_session_gt_now|callback_check_session_no_conflict' );
        }
        elseif( $state == 'edit' )
        {
            $crud->set_rules( 'fm_session_start_time', 'Session Start', 'required|trim|xss_clean|callback_check_date_time_format' );
        }
                     
        $output = $crud->render();

        $this->load->view( 'templates/header', $data );
	$this->load->view( 'sessions_content' );
        $this->load->view( 'contents', $output );
        $this->load->view( 'templates/footer' );
        log_message( 'info', 'Done sessions.index' );