Page 1 of 1

Admin multi edit feature validation

Posted: Tue May 04, 2010 4:01 pm
by chaleswa
The multiple edit feature is really a nice feature to have in the admin panel. But I don't know how to perform server side validation with multiple edit feature. For example if I edit two users and remove the mandatory field e.g email field, it updates with blank email.
Is there a way to do a validation without loosing the multi edit feature or is it better to remove and update for edit/updates.

Note: The new design in the admin panel looks nice and more compact than the previous one. Thanks for the quick update.

Re: Admin multi edit feature validation

Posted: Tue May 04, 2010 8:48 pm
by dbashyal
Not exactly sure what you mean.

Do you want to save some users without email address? If so, whats the use of creating one as they need to enter email address when they login.

I am thinking to add username field as well, so users can login with either username or email. Also, someone suggested to include change of password for users from admin.

I am currently making changes on CMS to work on CodeIgniter 2.0. Its almost finished, just need to convert existing plugins to helper as Codeigniter doesn't support plugins anymore.

Please let me know if you think of any modification/upgrade that will be benficial for all.

Re: Admin multi edit feature validation

Posted: Tue May 04, 2010 8:58 pm
by chaleswa
Sorry. I was indeed referring to validation in the user section of the admin panel.
The email is a must field for the user. So accidently, If the admin removes the email and updates, it gets updated. But it is a required field and should not get updated without a proper email address (like wise others pages etc as well).

I tried to add validation but since it is written to update multiple users at the same time, I feel difficult in doing so.

Also when creating/editing a page if a menu option is not chosen then php error appears on top after submission.

Re: Admin multi edit feature validation

Posted: Thu May 06, 2010 9:12 pm
by dbashyal

Code: Select all

if(!empty($email) && !empty($groups_id) && !empty($id))
Because of above code it doesn't save without email but i haven't placed the validation for email yet. I'll do my best to look into these on this weekend.

Re: Admin multi edit feature validation

Posted: Sun May 09, 2010 9:58 am
by dbashyal
For a quick solution you can replace edit function with this one:

Code: Select all

	function _edit()
	{
		$this->load->helper('security');
		$this->load->library('form_validation');
		$data = '';
		$id_array = array();
		
		if(!isset($_POST['users'])) {
			if(isset($_POST['select'])) {
				$id_array = $_POST['select'];
			} else {
				//$data['error_message']['select'] = "You must select atleast one user to edit";
				$msg = array('error' => '<p>You must select atleast one user to edit.</p>');
				set_global_messages($msg, 'error');

				unset($_POST);
				$this->index();
				exit();
		
			}
		}
		
		!is_array($id_array) ? $id_array = array() : '';

		//START: for the first page load, get data from database
		foreach($id_array as $id) {
		
			$id = preg_replace('/[^0-9]+/','',$id);
			
			$this->db->where('users_id',$id);
			$query = $this->db->get('users');
			
			foreach ($query->result() as $row)
			{
				$_POST['users'][$row->users_id]['id'] = $row->users_id;
				$_POST['users'][$row->users_id]['active'] = $row->active;
				$_POST['users'][$row->users_id]['email'] = $row->email;
				$_POST['users'][$row->users_id]['firstname'] = $row->firstname;
				$_POST['users'][$row->users_id]['lastname'] = $row->lastname;
				$_POST['users'][$row->users_id]['groups_id'] = $row->groups_id;
			}
		}
		//END: for the first page load, get data from database
			
		//START: clean data and update in database
		if($this->input->post('edit') == 'Update' && isset($_POST['users']) && is_array($_POST['users'])) {
			foreach($_POST['users'] as $v) {
				//cleaning
				$id = (int)preg_replace('/[^0-9]+/','',$v['id']); //only intergers
				$active = (int)preg_replace('/[^0-9]+/','',$v['active']);
				$email = xss_clean($v['email']);
				$firstname = xss_clean($v['firstname']);
				$lastname = xss_clean($v['lastname']);
				$groups_id = (int)preg_replace('/[^0-9]+/','',$v['groups_id']);
				
				//clean the data to autofill in form
				$_POST['users'][$id]['id'] = $id;
				$_POST['users'][$id]['active'] = $active;
				$_POST['users'][$id]['email'] = $email;
				$_POST['users'][$id]['firstname'] = $firstname;
				$_POST['users'][$id]['lastname'] = $lastname;
				$_POST['users'][$id]['groups_id'] = $groups_id;
				
				//update database if set
				if(!empty($email) && !empty($groups_id) && !empty($id)) {
			
					$_POST['email'] = $email;
					$_POST['groups_id'] = $groups_id;
					$_POST['firstname'] = $firstname;
					$_POST['lastname'] = $lastname;
					
					$val = array(
							array('field' => 'email','label' => 'Email','rules' => 'trim|required|xss_clean|valid_email'),
							array('field' => 'groups_id','label' => 'Group','rules' => 'trim|required|xss_clean'),
							array('field' => 'firstname','label' => 'First Name','rules' => 'trim|required|xss_clean'),
							array('field' => 'lastname','label' => 'Last Name','rules' => 'trim|required|xss_clean')
						);
					
					$this->form_validation->set_rules($val);
					
					if ($this->form_validation->run() == FALSE)
					{
						if(!validation_errors() == '' && $this->input->post('edit') == 'Update') {
							$msg = array('error' => validation_errors());
							set_global_messages($msg, 'error');
						}
					}
					else {
						$this->db->where('users_id', $id);
						$sql_update = array(
										'active'    => $active,
										'email'     => $email,
										'firstname' => $firstname,
										'lastname'  => $lastname,
										'groups_id' => $groups_id
										);
						$this->db->update('users', $sql_update);
						
						$msg = array('success' => '<p>Updated successfully.</p>');
						set_global_messages($msg, 'success');
					}
				} else {
						$msg = array('error' => '<p>Required fields can not be empty!</p>');
						set_global_messages($msg, 'error');
				}
			}
		}
		//END: validate data and update in database
				
			$assets = array();
			
			//load all required css
			//if media type not defined, screen is default.
			//$assets['css'] = array('admin','swiff','box','upload');
			$assets['css'] = array(
								'all' => array('admin','users','box')
							);
			//load all required js
			$assets['js'] = array();
			
			$this->assets->load($assets);
			
		//$data['users'] = $this->usersmodel->get_users();
		
		//---
		$html_string = $this->load->view('users_edit_view', $data, true);//Get view data in place of sending to browser.
		
		$this->process->view($html_string);
	}

Re: Admin multi edit feature validation

Posted: Tue May 11, 2010 4:14 pm
by chaleswa
Thank you very much for the quick solution.