I have been this for many days. My problem is i want to get the data "selected" in the birthdate when i click edit profile, my birthdate input is dropdown select which has year, month and day. My database field is birth_date which has a type date. During when saving the data, the date is saved by this format. for example 1990-01-20, and it will saved, my problem is when i click edit profile the select dropdown has no data, meaning it will not be "selected", how will i able to get the data to be selected? in symfony framework?
Here is my frontend/module/home actions
<?php
class editAction extends sfAction{
private function saveData($post, $file=''){
$firstName = ucfirst($post['first_name']);
$lastName = ucfirst($post['last_name']);
// $Slug = $post['slug'];
// $newSlug = explode(' ', $Slug);
// $Slug = implode("", $newSlug);
$cleanFirstName = preg_replace('/[^A-Za-z0-9\-]/', '', $firstName);
$cleanLastName = preg_replace('/[^A-Za-z0-9\-]/', '', $lastName);
$year = $post['year'];
$month = $post['month'];
$day = $post['day'];
$res = $year."-".$month."-".$day;
$user = Doctrine::getTable('User')->find($post['id']);
$user->setFirstName($cleanFirstName);
$user->setLastName($cleanLastName);
$user->setAbout($post['about']);
$user->setSex($post['sex']);
$user->setMobile($post['mobile']);
$user->setBirthDate($res);
$user->setSlug($post['slug']);
$user->save();
$imageName = $this->getUser()->getAttribute('homeImage');
if($imageName){
$temp = sfConfig::get('app_GalleryPhoto_dir_150');
$dir = sfConfig::get('app_GalleryPhoto_dir');
if(is_file($temp.$imageName)){
$file = $imageName;
$file['ext'] = getFileExtension($file['name']);
rename($file.$imageName,$dir.$file);
$user->setProfileImage($file);
$user->save();
}
}else{
if($file && !$file['error']){
$file = $this->saveImage($file, $user->getId());
$user->setProfileImage($file);
$user->save();
}
}
}
public function submit($request){
$user = $this->getUser()->getAttribute('home'.$this->id);
$imageName = $this->getUser()->getAttribute('homeImage');
if(!$user){
return $this->redirect('home/edit?id='.$this->id);
}
$this->saveData($user);
$this->getUser()->setAttribute('home'.$this->id, NULL);
return $this->redirect('home');
}
private function saveImage($file, $id=NULL){
if(!$id){
$dir = sfConfig::get('app_company_dir_150');
}else{
$dir = sfConfig::get('app_GalleryPhoto_dir');
}
$medium = sfConfig::get('app_GalleryPhoto_medium');
$small = sfConfig::get('app_GalleryPhoto_small');
$dir = sfConfig::get('app_GalleryPhoto_dir_150');
$dir_60 = sfConfig::get('app_GalleryPhoto_dir');
$file['ext'] = getFileExtension($file['name']);
if(!$id){
$fileName = uniqid(). '.'.$file['ext'];
}else{
$fileName = $id.'.'.$file['ext'];
}
$imagePath = $dir.$fileName;
//saving and cropping image
$image_medium = encode('home-'.$id).'.'.$fileName;
$image_small = encode('home-'.$id).'_thumb.'.$fileName;
$image_medium_path = $dir.$image_medium;
$image_small_path = $dir_60.$image_small;
rename($file['tmp_name'],$image_medium_path);
//cropping image medium
$image = new myImageManipulator($image_medium_path);
$image->resample2($medium, $medium);
$image->crop(0, 0, $medium, $medium);
$image->save($image_medium_path);
//cropping image small
$image = new myImageManipulator($image_medium_path);
$image->resample2($small, $small);
$image->crop(0, 0, $small, $small);
$image->save($image_small_path);
if(!$id){
$this->getUser()->setAttribute('homeImage',$image_medium);
}else{
return $image_medium;
}
}
private function deleteImageSixty(){
$dir60 = sfConfig::get('app_GalleryPhoto_dir');
$fileName = $this->getUser()->getAttribute('homeImage');
if($fileName){
if(is_file($dir60.$fileName)){
unlink($dir60.$fileName);
}
$this->getUser()->setAttribute('homeImage', '');
}
}
private function deleteImage($fileName){
$dir = sfConfig::get('app_GalleryPhoto_dir_150');
$fileName = $this->getUser()->getAttribute('homeImage');
if($fileName){
if(is_file($dir.$fileName)){
unlink($dir.$fileName);
}
$this->getUser()->setAttribute('homeImage', '');
}
}
private function saveImageTmp($request){
$id = $request->getParameter('id');
$formName = $this->form->getModelName();
$file = $this->request->getFiles($formName);
$file = $file['photo'];
if($file && $file['error'] == 0){
#$this->deleteImage();
#$this->deleteImageSixty();
$this->saveImage($file);
}
}
public function execute($request){
$this->id = $request->getParameter('id');
if(!$this->id){
$this->forward404();
}
$type = $request->getParameter('type');
if($type == "submit"){
return $this->submit($request);
}
$this->userLogged = Doctrine_Core::getTable('User')->find($this->id);
$this->form = new EditProfileForm($this->userLogged);
if($request->isMethod('post')){
$formName = $this->form->getModelName();
$files = $this->request->getFiles($formName);
$this->form->bind($request->getParameter($formName),$this->request->getFiles($formName));
$post = $request->getParameter($formName);
if($this->form->isValid()){
$this->saveImageTmp($request);
$this->getUser()->setAttribute('home'.$this->id, $post);
return $this->redirect('home/edit?id=' .$this->id. '&type=submit');
}else{
$this->saveImageTmp($request);
}
}
}
}
in the private function saveData()
this is how i saved the birthdate select dropdown code
$year = $post['year'];
$month = $post['month'];
$day = $post['day'];
$res = $year."-".$month."-".$day;
$user->setBirthDate($res);
this is my editSuccess code
<div class="content" class="wrap">
<div class="cols marginBottom30 clearFix">
<div class="content withIcon icon1" style="margin-left:488px; width:500px;">
<h2>Edit Profile</h2>
<form enctype="multipart/form-data" action="<?php echo url_for('home/edit?id=' .$userLogged->getId());?>" method="post">
<?php echo $form->renderHiddenFields(); ?>
<table>
<tr>
<td><?php echo $form['first_name']; ?></td>
</tr>
<tr>
<td>
<div class="error_list">
<?php echo $form['first_name']->renderError(); ?>
</div>
</td>
</tr>
<tr>
<td><?php echo $form['last_name']; ?></td>
</tr>
<tr>
<td>
<div class="error_list">
<?php echo $form['last_name']->renderError(); ?>
</div>
</td>
</tr>
<tr>
<td>
<?php if($userLogged->getProfileImage()): ?>
<a href="/uploads/images/<?php echo $userLogged->getProfileImage(); ?>" target="_blank"><img alt="" src="/uploads/images/<?php echo $userLogged->getprofileImage(); ?>" /></a>
<?php endif; ?>
</td>
</tr>
<tr>
<td><?php echo $form['photo'];?></td>
</tr>
<tr>
<td><?php echo $form['about']; ?></td>
</tr>
<tr>
<td>
<div class="error_list">
<?php echo $form['about']->renderError(); ?>
</div>
</td>
</tr>
<tr>
<td><?php echo $form['sex']; ?></td>
</tr>
<tr>
<td>
<div class="error_list">
<?php echo $form['sex']->renderError(); ?>
</div>
</td>
</tr>
<tr>
<td><?php echo $form['mobile']; ?></td>
</tr>
<tr>
<div class="error_list">
<?php echo $form['mobile']->renderError(); ?>
</div>
</tr>
<tr>
<td>Year: <?php echo $form['year']; ?>Month: <?php echo $form['month']; ?> Day: <?php echo $form['day']; ?></td>
</tr>
<tr>
<div class="error_list">
<?php echo $form['year']->renderError(); ?>
<?php echo $form['month']->renderError(); ?>
<?php echo $form['day']->renderError(); ?>
</div>
</tr>
<tr>
<td><?php echo $form['slug']; ?></td>
</tr>
<tr>
<div class="error_list">
<?php echo $form['slug']->renderError(); ?>
</div>
</tr>
<tr>
<td><input type="submit" value="Edit" /></td>
</tr>
</table>
</form>
<br />
<br />
<br />
<a href="<?php echo url_for('home'); ?>" title="HomePage">HomePage</a>
</div>
</div>
</div>
this is my EditProfileForm.class.php
<?php
/**
* Edit Profile form.
*/
class EditProfileForm extends BaseUserForm{
public function getModelName(){
return 'User';
}
public function configure(){
$options = UserTable::getGender();
$year = UserTable::getInstance()->getYear();
$month = UserTable::getInstance()->getMonths();
$day = UserTable::getInstance()->getDays();
$this->setWidgets(array(
'id' => new sfWidgetFormInputHidden(),
'first_name' => new sfWidgetFormInputText(array(),array('placeholder'=>'First Name: ')),
'last_name' => new sfWidgetFormInputText(array(),array('placeholder'=>'Last Name: ')),
'photo' => new sfWidgetFormInputFileEditable(array(
'file_src' => sfConfig::get('app_GalleryPhoto_dir_150'),
'is_image' => true,
'edit_mode' => false,
'with_delete' => false
)),
'about' => new sfWidgetFormTextarea(array(), array('placeholder'=>'About Me: ')),
'sex' => new sfWidgetFormSelectRadio(
array('choices' => $options)
),
'mobile' => new sfWidgetFormInputText(array(),array('placeholder'=>'Mobile Number: ')),
'year' => new sfWidgetFormSelect(
array('choices'=>$year)
),
'month' => new sfWidgetFormSelect(
array('choices'=>$month)
),
'day' => new sfWidgetFormSelect(
array('choices'=>$day)
),
'slug' => new sfWidgetFormInputText(array(),array('placeholder'=>'Slug: ')),
));
$this->setValidators(array(
'id' => new sfValidatorDoctrineChoice(
array('model' => $this->getModelName(),
'column' => 'id', 'required' => false)),
'first_name' => new sfValidatorString(
array('trim' => true,'required' => true, 'max_length' => 20),
array('invalid' => 'required', 'required'=>'Please enter first name', 'max_length' => 'Firstname is too long. 20 characters maximum.')
),
'last_name' => new sfValidatorString(
array('trim' => true,'required' => true, 'max_length' => 20),
array('invalid' => 'required', 'required'=>'Please enter last name', 'max_length' => 'Lastname is too long. 20 characters maximum.')
),
'photo' => new sfValidatorFile(array(
'required' => false,
'max_size' => 500000000000000,
'mime_types' => 'web_images',
'path' => sfConfig::get('app_GalleryPhoto_dir_150'),
)),
'about' => new sfValidatorString(
array('trim' => true,'required' => true)
),
'sex' => new sfValidatorString(
array('trim' => true,'required' => true),
array('required'=>'Please select gender.')
),
'mobile' => new sfValidatorString(
array('trim' => true,'required' => true, 'max_length' => 20),
array('invalid' => 'required', 'required'=>'Please enter mobile number', 'max_length' => 'mobile number is too long. 20 characters maximum.')
),
'year' => new sfValidatorString(
array('trim' => true),
array('required'=>'Please enter year.')
),
'month' => new sfValidatorString(
array('trim' => true),
array('required'=>'Please select month')
),
'day' => new sfValidatorString(
array('trim' => true),
array('required'=>'Please select day')
),
'slug' => new sfValidatorString(
array('trim' => true,'required' => true),
array('required'=>'Please enter Slug')
),
));
$this->widgetSchema->setNameFormat($this->getModelName().'[%s]');
$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback'=>array($this, 'validateForm'))));
}
public function validateForm($validator, $values){
//check slug if special characters has been inputed.
/*$values = $values['slug'];
if(preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/',$values)){
$error = new sfValidatorError($validator, 'Invalid Slug Characters.');
$error_data['slug'] = $error;
}else{
return true;
}
if(preg_replace('~[^\\pL\d]+~u', '', $values)){
$error = new sfValidatorError($validator, 'Invalid Slug, At Least No Spaces');
$error_data['slug'] = $error;
}*/
/*if($values['slug']){
$slug = Doctrine_Core::getTable('User')->findOneBySlug($values['slug']);
if($values['slug'] != $slug){
$error = new sfValidatorError($validator, 'Slug Already Exists.');
$error_data['slug'] = $error;
}
}*/
if($values['slug']){
if(!$values['slug']){
$error = new sfValidatorError($validator, 'Invalid slug.');
$error_data['slug'] = $error;
}else{
if(isset($values['id'])){
$user = UserTable::getInstance()->checkSlug($values['slug'],$values['id']);
}else{
$user = UserTable::getInstance()->findOneBySlug($values['slug']);
}
if($user->count()){
$error = new sfValidatorError($validator, 'Slug is already taken.');
$error_data['slug'] = $error;
}
}
}
//check strings if there are special characters.
if($values['slug']){
if(preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/',$values['slug'])){
$error = new sfValidatorError($validator, 'Invalid Slug Characters.');
$error_data['slug'] = $error;
}
}
//check two strings if it has spaces
if(preg_match('/\s/',$values['slug'])){
$error = new sfValidatorError($validator, 'Invalid Slug. At Least No Spaces');
$error_data['slug'] = $error;
}
if(isset($error_data)){
throw new sfValidatorErrorSchema($validator, $error_data);
}
return $values;
}
}
and this is my UserTableClass.php code
<?php
/**
* UserTable
*
* This class has been auto-generated by the Doctrine ORM Framework
*/
class UserTable extends Doctrine_Table
{
/**
* Returns an instance of this class.
*
* @return object UserTable
*/
public static function getInstance()
{
return Doctrine_Core::getTable('User');
}
public static function getGender(){
return array(
1 => 'Male',
2 => 'Female'
);
}
public static function getDays(){
return array(
'' => '----',
1 => '1',
2 => '2',
3 => '3',
4 => '4',
5 => '5',
6 => '6',
7 => '7',
8 => '8',
9 => '9',
10 => '10',
11 => '11',
12 => '12',
13 => '13',
14 => '14',
15 => '15',
16 => '16',
17 => '17',
18 => '18',
19 => '19',
20 => '20',
21 => '21',
22 => '22',
23 => '23',
24 => '24',
25 => '25',
26 => '26',
27 => '27',
28 => '28',
29 => '29',
30 => '30',
31 => '31'
);
}
public static function getMonths(){
return array(
'' => '----',
1 => 'January',
2 => 'February',
3 => 'March',
4 => 'April',
5 => 'May',
6 => 'June',
7 => 'July',
8 => 'August',
9 => 'September',
10 => 'October',
11 => 'November',
12 => 'December'
);
}
public static function getYear(){
return array(
'' => '----',
2014 => '2014',
2013 => '2013',
2012 => '2012',
2011 => '2011',
2010 => '2010',
2009 => '2009',
2008 => '2008',
2007 => '2007',
2006 => '2006',
2005 => '2005',
2004 => '2004',
2003 => '2003',
2002 => '2002',
2001 => '2001',
2000 => '2000',
1999 => '1999',
1998 => '1998',
1997 => '1997',
1996 => '1996',
1995 => '1995',
1994 => '1994',
1993 => '1993',
1992 => '1992',
1991 => '1991',
1990 => '1990',
1989 => '1989',
1988 => '1988',
1987 => '1987',
1986 => '1986',
1985 => '1985',
1984 => '1984',
1983 => '1983',
1982 => '1982',
1981 => '1981',
1980 => '1980'
);
}
public function checkSlug($slug, $id=NULL){
$q = Doctrine_Query::create()
->from('User u')
->where('u.slug = ?',$slug);
if($id){
$q->andWhere('u.id <> ?',$id);
}
$q->limit(1);
return $q->execute();
}
}
And in my UserTable.class.php file in there i put the code public function getDays() getMonths() getYear().
Now when i click edit profile all the data will be displayed, execpt for the birthdate it will not be selected. Can anyone please help me out on this?