how to give class name using php - php

I am trying to add class like this "class' => 'form-control' in this but it is not working.
$data = array(
'name' => 'SupplierAddress',
'value' => set_value('SupplierAddress'),
'id'=>'SupplierAddress',
'class' => 'form-control',
'required' => 'true'
);
echo form_input($data);

Related

Form Validation in CakePHP 2

In my view I have
<?= $this->Form->create('Asc201516', array(
'url' => array(
'controller' => 'asc201516s',
'action' => 'submit_asc201516'
),
'class' => 'form-inline',
'onsubmit' => 'return check_minteacher()'
)); ?>
<div class="form-group col-md-3">
<?= $this->Form->input('bi_school_na', array(
'type' => 'text',
'onkeypress' => 'return isNumberKey(event)',
'label' => 'NA',
'placeholder' => 'NA',
'class' => 'form-control'
)); ?>
</div>
<?php
$options = array(
'label' => 'Submit',
'class' => 'btn btn-primary');
echo $this->Form->end($options);
?>
In my Controller, I have
$this->Asc201516->set($this->request->data['Asc201516']);
if ($this->Asc201516->validates()) {
echo 'it validated logic';
exit();
} else {
$this->redirect(
array(
'controller' => 'asc201516s',
'action' => 'add', $semisid
)
);
}
In my Model, I have
public $validate = array(
'bi_school_na' => array(
'Numeric' => array(
'rule' => 'Numeric',
'required' => true,
'message' => 'numbers only',
'allowEmpty' => false
)
)
);
When I submit the form, logically it should not get submitted and print out the error message but the form gets submitted instead and validates the model inside controller which breaks the operation in controller.
You have to check validation in your controller like
$this->Asc201516->set($this->request->data);
if($this->Asc201516->validates()){
$this->Asc201516->save($this->request->data);
}else{
$this->set("semisid",$semisid);
$this->render("Asc201516s/add");
}
You will have your ID there in variable $semisid, or you can set data in $this->request->data = $this->Asc201516->findById($semisid);

how to create an array element in form - zend framework 2

I want to create the following element :
<input type="file" name="file[]">
I have tried the following code in myproject/module/Member/src/Member/Form/EditForm.php :
$this->add(array(
'name' => 'file',
'type' => 'file',
'attributes' => array(
'class' => 'form-control col-md-7 col-xs-12',
'id' => 'file',
),'options' => array(
'multiple'=>TRUE
),
));
and
$this->add(array(
'name' => 'file[]',
'type' => 'file',
'attributes' => array(
'class' => 'form-control col-md-7 col-xs-12',
'id' => 'file',
),'options' => array(
'multiple'=>TRUE
),
));
but it is not working.
For file upload Zend Framework 2 has a special FileInput class.
It is important to use this class because it also does other important things like validation before filtering. There are also special filters like the File\RenameUpload that renames the upload for you.
Considering that $this is your InputFilter instance the code could look like this:
$this->add(array(
'name' => 'file',
'required' => true,
'allow_empty' => false,
'filters' => array(
array(
'name' => 'File\RenameUpload',
'options' => array(
'target' => 'upload',
'randomize' => true,
'overwrite' => true
)
)
),
'validators' => array(
array(
'name' => 'FileSize',
'options' => array(
'max' => 10 * 1024 * 1024 // 10MB
)
)
),
// IMPORTANT: this will make sure you get the `FileInput` class
'type' => 'Zend\InputFilter\FileInput'
);
To attach file element to a form:
// File Input
$file = new Element\File('file');
$file->setLabel('My file upload')
->setAttribute('id', 'file');
$this->add($file);
Check the documentation for more information on file upload.
Or check the documentation here on how to make an upload form

Unable to include variables in a view that was defined in another view in codeigniter

I've created a view form_variables.php that contains all the form input variables defined in a single file. So that whenever i need to create an input field, i would simply include the form_variables file and then use the form input variables defined in the form_variables.php
Here's what it contains.
<?php
$email = array(
'name' => 'u_email',
'type' => 'text',
'maxlength' => '50',
'class' => 'form-control',
'value' => set_value('e_email'),
'placeholder' => "Enter your Email Address"
);
$pwd = array(
'name' => 'u_pwd',
'type' => 'password',
'maxlength' => '50',
'class' => 'form-control',
'id' => 'pwd',
'placeholder' => "Enter your Password"
); ?>
Now i have another view that contains the form.
<?php echo $this->load->view('includes/form_variables'); ?>
<div class="form-group">
<?php echo form_input($email); ?>
</div>
It still says that the variable $email is undefined. Although it loads the form_variables.php file. Please Help.
Instead of using a view for this purpose. try using a controller
Class form_variables extends CI_Controller
{
function get_email_field()
{
return array(
'name' => 'u_email',
'type' => 'text',
'maxlength' => '50',
'class' => 'form-control',
'value' => set_value('e_email'),
'placeholder' => "Enter your Email Address"
);
}
function get_password_field()
{
return array(
'name' => 'u_pwd',
'type' => 'password',
'maxlength' => '50',
'class' => 'form-control',
'id' => 'pwd',
'placeholder' => "Enter your Password"
);
}
}
Now to call this controller inside another controller
$this->load->library('../controllers/form_variables');
// use your function
$email_field = $this->form_variables->get_email_field();
$pass_field = $this->form_variables->get_password_field();
I hope this will work for you..
i have a better solution to this,this will solve your problem, as well as you can create a dynamic field also :
1st step:
create a common_helper.php function in /helpers.
and place the following code in it.
if (!function_exists('get_field')) {
function get_field($field, $data = array()) {
switch ($field) {
case "email":
return array(
'name' => 'u_email',
'type' => 'text',
'maxlength' => '50',
'class' => 'form-control',
'value' => set_value('e_email'),
'placeholder' => "Enter your Email Address",
);
break;
case "password":
return array(
'name' => 'u_pwd',
'type' => 'password',
'maxlength' => '50',
'class' => 'form-control',
'id' => 'pwd',
'placeholder' => "Enter your Password",
);
break;
case "custom":
if (count($data)) {
$placeholder = (isset($data['placeholder'])) ? $data['placeholder'] : 'Enter you text here';
$length = (isset($data['length'])) ? $data['length'] : '50';
$id = (isset($data['id'])) ? $data['id'] : '';
return array(
'name' => $data['fieldName'],
'type' => 'text',
'maxlength' => $length,
'id' => $id,
'class' => 'form-control',
'placeholder' => $placeholder,
);
}
break;
default:
return array(
'name' => 'textfiled',
'type' => 'text',
'maxlength' => '50',
'class' => 'form-control',
'placeholder' => "Enter your text",
);
}
}
}
2nd step:
autoload it in config/autoload.
when you need it just pass your defined field name to the function e.g.
get_field('password') ,in your case
<?php echo form_input(get_field('password')); ?>
and if you want to create a dynamic field just Passed the following:
$fieldOpt=array(
'fieldName' => 'username',
//optional
'placeholder' => "Enter your username here",
'id'=>'myidfield',
'length'=>'60',
);
<?php echo form_input(get_field('custom', $fieldOpt);?>
hope this will help you.

Cakephp Form is not getting submiited but giving Notice (8): Array to string Coversion, many other Warnings (2)

I am having trouble submitting/saving data to the CakePHP Model. I constructed the form as usual as I always do, but this time I am getting notices and warning an also data is not getting saved. Here is the form I have constructed and method in Controller:
educationaldetails.ctp
<?php
if (empty($Education)):
echo $this->Form->create('Candidate', array('class' => 'dynamic_field_form'));
echo $this->Form->input('CandidatesEducation.0.candidate_id', array(
'type' => 'hidden',
'value' => $userId
));
echo $this->Form->input('CandidatesEducation.0.grade_level', array(
'options' => array(
'Basic' => 'Basic',
'Masters' => 'Masters',
'Doctorate' => 'Doctorate',
'Certificate' => 'Certificate'
)
));
echo $this->Form->input('CandidatesEducation.0.course');
echo $this->Form->input('CandidatesEducation.0.specialization');
echo $this->Form->input('CandidatesEducation.0.university');
echo $this->Form->input('CandidatesEducation.0.year_started', array(
'type' => 'year'
));
echo $this->Form->input('CandidatesEducation.0.year_completed', array(
'type' => 'year'
));
echo $this->Form->input('CandidatesEducation.0.type', array(
'options' => array(
'Full' => 'Full',
'Part-Time' => 'Part-Time',
'Correspondence' => 'Correspondence'
)));
echo $this->Form->input('CandidatesEducation.0.created_on', array(
'type' => 'hidden',
'value' => date('Y-m-d H:i:s')
));
echo $this->Form->input('CandidatesEducation.0.created_ip', array(
'type' => 'hidden',
'value' => $clientIp
));
echo $this->Form->button('Submit', array('type' => 'submit', 'class' => 'submit_button'));
echo $this->Form->end();
endif;
CandidatesController.php
public function educationaldetails() {
$this->layout = 'front_common';
$this->loadModel('CandidatesEducation');
$Education = $this->CandidatesEducation->find('first', array(
'conditions' => array(
'candidate_id = ' => $this->Auth->user('id')
)
));
$this->set('Education', $Education);
// Checking if in case the Candidates Education is available then polpulate the form
if (!empty($Education)):
// If Form is empty then populating the form with respective data.
if (empty($this->request->data)):
$this->request->data = $Education;
endif;
endif;
if ($this->request->is(array('post', 'put')) && !empty($this->request->data)):
$this->Candidate->id = $this->Auth->user('id');
if ($this->Candidate->saveAssociated($this->request->data)):
$this->Session->setFlash('You educational details has been successfully updated', array(
'class' => 'success'
));
return $this->redirect(array(
'controller' => 'candidates',
'action' => 'jbseeker_myprofile',
$this->Auth->user('id')
));
else:
$this->Session->setFlash('You personal details has not been '
. 'updated successfully, please try again later!!', array(
'class' => 'failure'
));
endif;
endif;
}
Here is the screenshot of errors I am getting, Not able to figure out what is happening while other forms are working correctly. looks like something something wrong with view?
This error is coming out because you are not passing the proper arguments to the session->setFlash() method, you are doing like this:
$this->Session->setFlash('You personal details has not been updated successfully, please try again later!!', array(
'class' => 'failure'
));
In docs it is mentioned like:
$this->Session->setFlash('You personal details has not been updated successfully, please try again later!!',
'default', array(
'class' => 'failure'
));
Hi so if i understand :
array(
'CandidatesEducation' => array(
(int) 0 => array(
'candidate_id' => '5',
'grade_level' => 'Basic',
'course' => 'Masters of Computer Application',
'specialization' => '',
'university' => 'Mumbai University',
'year_started' => array(
'year' => '2011'
),
'year_completed' => array(
'year' => '2014'
),
'type' => 'Full',
'created_on' => '2014-11-27 15:44:36',
'created_ip' => '127.0.0.1'
)
)
),
Is your data , and you just need to save this in your candidates_educations table , so in this case i would do :
$this->Candidate->CandidatesEducation->save($this->request->data);
and your $data had to look :
array(
'CandidatesEducation' => array(
'candidate_id' => '5',
'grade_level' => 'Basic',
'course' => 'Masters of Computer Application',
'specialization' => '',
'university' => 'Mumbai University',
'year_started' => array(
'year' => '2011'
),
'year_completed' => array(
'year' => '2014'
),
'type' => 'Full',
'created_on' => '2014-11-27 15:44:36',
'created_ip' => '127.0.0.1'
)
);
the saveAssociated is used when you create your Model AND Model associated , not only your Associated model.
And : i m not sure for year_started and year completed rm of data, it depends of your table schema ; what's the type of year_completed and year_started ?

CodeIgniter Form information

I have a controller with a add() function and create() function. The add function posts to create.
The form is displayed using the form helper. In the add() function, I have an array to set the form input attributes that looks like the following:
$this->data['form'] = array(
'label_attributes' => array(
'class' => 'col-lg-2 control-label'
),
'media_name' => array(
'class' => 'form-control',
'id' => 'media_name',
'name' => 'media_name',
'value' => set_value('media_name')
),
'media_link' => array(
'class' => 'form-control',
'id' => 'media_link',
'name' => 'media_link',
'value' => set_value('media_link')
),
'media_width' => array(
'class' => 'form-control',
'id' => 'media_width',
'name' => 'media_width',
'size' => '4',
'maxlength' => '4',
'value' => ($this->form_validation->set_value('media_width')) ? $this->form_validation->set_value('media_width') : '640'
),
'media_height' => array(
'class' => 'form-control',
'id' => 'media_height',
'name' => 'media_height',
'value' => ($this->form_validation->set_value('media_height')) ? $this->form_validation->set_value('media_height') : '360'
),
'media_description' => array(
'class' => 'form-control',
'id' => 'media_desription',
'name' => 'media_desription',
'value' => $this->form_validation->set_value('media_desription')
)
);
When I post to the create() function, I lose access to the data['form'] values. Should all this information just be in the view or is it possible to put it in the model so I can load it whenever needed? When I tried to put it in the model, I had issues with the 'value' attributes even if I loaded the form_validation library in the model.
Because the controller class is renewed when a new request comes, so the problem with your code is that, $this->data is created when you visit the add function, while when you post to create function, the controller class is renewed again, there is no $this->data at all at this time.
If you want to pass data from one request to another request, you can pass the data via view or model.
Hope helps!

Categories