I am trying to render a view inside a controller and load it using ajax.
It works fine in my local system but in the server it throws an error:
Error: The view for MyController::myView() was not found.
Here is my code
Controller :
public function creativeForms(){
$this->layout = 'ajax';
$createdForm = '';
if(!empty($this->request->data['id'])){
$request = array();
$requestFields = array();
if(isset($this->request->data['req_id']) && !empty($this->request->data['req_id'])) {
App::import('Model', 'RequestField');
$RequestField = new RequestField();
$request = $RequestField->find('list',array('fields'=>array('RequestField.form_field_id','RequestField.value'),'conditions'=>array('RequestField.request_id'=>$this->request->data['req_id'])));
$requestFields = $RequestField->find('list',array('fields'=>array('RequestField.form_field_id','RequestField.id'),'conditions'=>array('RequestField.request_id'=>$this->request->data['req_id'])));
}
$form = $this->FormField->Form->findByid($this->request->data['id']);
$createdForm = $this->createForm($form,$request,$requestFields);
}
echo $createdForm;
exit;
}
public function createForm($form , $data= NULL,$dataMap=array()){
$createForm ='';
foreach($form['FormField'] as $FormField){
$this->set('FormField',$FormField);
$this->set('data',$data);
$this->set('dataMap',$dataMap);
switch ($FormField['field_type']){
case 'textbox':
$createForm .= $this->render('../elements/fields/textbox');
break;
case 'textarea':
$createForm .= $this->render('../elements/fields/textarea');
break;
case 'radio':
$createForm .= $this->render('../elements/fields/radio');
break;
case 'checkbox':
$createForm .= $this->render('../elements/fields/checkbox');
break;
case 'file':
$createForm .= $this->render('../elements/fields/upload');
break;
case 'datefield':
$createForm .= $this->render('../elements/fields/datefield');
break;
case 'select':
$createForm .= $this->render('../elements/fields/select');
break;
}
}
return $createForm;
}
datefield.ctp
$field = $this->form->hidden('RequestField.'.$FormField['id'].'.type',
array('value'=>'datefield'));
$required = '';
if($FormField['field_required']){
$required = ' required';
}
if(isset($data[$FormField['id']])){
$field .= $this->form->hidden('RequestField.'.$FormField['id'].'.id',
array('value'=>$dataMap[$FormField['id']]));
$field .= $this->Form->input('RequestField.'.$FormField['id'].'.value',
array('value'=>$data[$FormField['id']],'type'=>'text',
'required'=>$FormField['field_required'],
'label'=>$FormField['field_label'],'class'=>'form-control default date-picker',
'div'=>array('class'=>'form-group'.$required)));
} else {
$field .= $this->Form->input('RequestField.'.$FormField['id'].'.value',
array('type'=>'text','required'=>$FormField['field_required'],
'label'=>$FormField['field_label'],'class'=>'form-control default-date-picker',
'div'=>array('class'=>'form-group'.$required)));
}
echo $field;
I have tried solving it with things like:
$view = new View($this, false);
$view->set(compact('foo', 'bar')); // set variables
$view->viewPath = 'elements'; // render an element
$html = $view->render('message');
Within your code you are referencing folder:
app/View/elements
when it should be
app/View/Elements
The first line works on Windows (as it uses a case-insensitive file and directory naming convention), but won't work on Unix-like OSes.
Try changing your code to:
$this->viewPath = 'Elements';
$this->render('fields/upload');
$this->render('fields/checkbox');
//and so on
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.