I am trying to add a form on the front end so my users can use crud functionality and allowing me to add responsiveness to the form.
I have been pointed in the direction of an article and examples over at ee-recipes and this have given me a good starting point. But I need to add some validation to the process. Firstname and lastname are required as well as checks to so if the player being added is already in the system.
I have added a form and action and I am able to save the data into the database.
public function player_form() {
// Build an array to hold the form's hidden fields
$hidden_fields = array(
"ACT" => $this->EE->functions->fetch_action_id( 'Clubmanager', 'create_club_player' )
);
$action_id = ee()->functions->fetch_action_id('Clubmanager', 'create_club_player');
// Build an array with the form data
$form_data = array(
"action" => "{site_url}?ACT=$action_id",
"id" => $this->EE->TMPL->form_id,
"class" => $this->EE->TMPL->form_class,
"hidden_fields" => $hidden_fields
);
// Fetch contents of the tag pair, ie, the form contents
$tagdata = $this->EE->TMPL->tagdata;
$form = $this->EE->functions->form_declaration($form_data) .
$tagdata . "</form>";
return $form;
}
public function create_club_player() {
$firstname = $this->EE->input->post('firstname');
$lastname = $this->EE->input->post('lastname');
$knownas = $this->EE->input->post('knownas');
//$club = $this->EE->input->post('club');
$position = $this->EE->input->post('position');
$dob = $this->EE->input->post('dob');
$url_title = strtolower(str_replace("'", "",$firstname)).'-'. strtolower(str_replace("'", "",$lastname));
$data = array(
'firstname' => $firstname,
'lastname' => $lastname,
'knownas' => $knownas,
'url_title' => $url_title,
'position' => $position,
'dob' => $dob
);
$this->EE->db->insert('tm_players', $data);
$ret = $this->EE->functions->fetch_site_index();
$data = array(
'title' => 'Add Player',
'content' => "Player added successfully",
'link' => array($ret, "Back to site")
);
$this->EE->output->show_message($data);
//print_r( $_POST );
//exit;
}
I know that I need to use form validation for the name etc and have what I need already as part of the CP module but not sure how to put it all together especially when reloading the page if there are errors. I have seen a post like this one here. page reload after postback answer by @Adrian Macneil that references ee()->core->generate_page(). Are there any simple examples for me to try and follow
BACKGROUND Some more background information for you, should answer some questions. My site was originally built on CI and added bits here and there. I had a vision of integrating the site with EE but unfortunately it wasn't straight forward or so I was told.
I paid to get my football stats into EE and with that I lost a of functionality and didn't have the knowledge or money to fix that. I ran with EE for two years but it just couldn't cope the stats and how it pulled it all together. I got lucky and teammanager module was then available and I was able to change this slightly and just plugin my database tables from my CI site.
With EE there are many ways to build websites and I am sure there are better ways to use EE for me but I do not have the expertise and just have to try to get there myself somehow.
I have control panel access for updating and adding all information including a separate one for Club Admins to add player information. As most of my users use mobiles to visit the site and I want to try and push members to use the club admin feature I thought best way would be to have responsive design rather than mess with the CP CSS or try and build a mobile app.
Unless I am wrong the CP process for forms are different from building a form for front and this is the reason why I am asking this question. My form will also require a validation routine to check whether this player exists and also allow an override for different players with the same name. I have this code already but as I said the CP form and front end form are processed differently. Hopefully this clears some questions up.
And thanks for all comments.