public function actionCreate() { $data = $_POST; //will be empty if CSRF authentication fails if (!empty($data)) { $data['name'] = trim($data['name']); $data['company'] = trim($data['company']); $data['position'] = trim($data['position']); $data['email'] = trim($data['email']); $data['contact_numbers'] = trim($data['contact_numbers']); $data['address'] = trim($data['address']); $data['notes'] = trim($data['notes']); //FORM VALIDATION HERE $errors = array(); //name is required if (strlen($data['name']) == 0) { array_push($errors, 'NAME_ERROR: Name is required'); } //email is required if (strlen($data['email']) == 0) { array_push($errors, 'EMAIL_ERROR: Email is required'); //check if email already exists } else { if (ProjectContactPersons::model()->exists('email = :email AND project_id = :project_id', array(":email" => $data['email'], ":project_id" => $data['project_id']))) { array_push($errors, 'EMAIL_ERROR: Email already taken'); // must be a valid email - text regexp } else { if (preg_match($this->emailRegExp, $data['email']) != 1) { array_push($errors, 'EMAIL_ERROR: Email is not valid'); } } } //data is good if (count($errors) == 0) { $contact_person = new ProjectContactPersons(); $contact_person->project_id = $data['project_id']; $contact_person->name = $data['name']; $contact_person->company = $data['company']; $contact_person->position = $data['position']; $contact_person->contact_numbers = $data['contact_numbers']; $contact_person->email = $data['email']; $contact_person->address = substr($data['address'], 0, 255); $contact_person->notes = substr($data['notes'], 0, 255); $contact_person->date_created = date("Y-m-d H:i:s"); $contact_person->date_updated = '0000-00-00 00:00:00'; $contact_person->created_by = Yii::app()->user->name; $contact_person->save(); echo CJSON::encode(array('type' => 'success', 'data' => '')); } else { echo CJSON::encode(array('type' => 'error', 'data' => implode(',', $errors))); } } else { echo CJSON::encode(array('type' => 'error', 'data' => 'CSRF_ERROR: CSRF Token did not match')); } }