I have rule in my model like this
public function rules()
{
return [
[['tbl_data_induk_mahasiswa_id'], 'required'],
[['tbl_data_induk_mahasiswa_id'], 'integer'],
[['nama'], 'file','extensions'=>'png,jpg','maxSize' => 1024000,'tooBig' => 'Size maksimum adalah 1 MB'],
[['nama'], 'string', 'max' => 300],
[['tbl_data_induk_mahasiswa_id'], 'unique'],
[['tbl_data_induk_mahasiswa_id'], 'exist', 'skipOnError' => true, 'targetClass' => TblDataIndukMahasiswa::className(), 'targetAttribute' => ['tbl_data_induk_mahasiswa_id' => 'id']],
];
}
i have form like this
<?php
use yii\helpers\Html;
//use yii\widgets\ActiveForm;
echo Html::beginForm(
['mahasiswa-foto-biodata/update'],
'post',
['enctype' => 'multipart/form-data'] //if you want to upload file with post
); ?>
<div class="form-group form-file-upload form-file-multiple">
<?= Html::activeFileInput(
$model,
'nama',
['class' => 'inputFileHidden', 'multiple' => '']
); ?>
<div class="input-group">
<?= Html::activeTextInput(
$model,
'nama',
[
'class' => 'form-control inputFileVisible',
'placeholder' => 'Single File',
]
); ?>
<span class="input-group-btn">
<button type="button" class="btn btn-fab btn-round btn-primary">
<i class="material-icons">attach_file</i>
</button>
</span>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?= Html::endForm(); ?>
I can not use activeForm widget because i must create html form that suit with my template, the form is work, the file succesfull uploaded, the problem is every tipe file is success uploaded, not just only png or jpg, but if i change the max rule for nama to [['nama'], 'string', 'max' => 2],then i upload a file that have name's length more than two the file can not be uploaded.
Any help?
Please check your form, you have 02 types of input with the same name ("nama"). One is "file", one is "text"
I think it is the reason that make your form does not works correctly!
Related
I want to apply validation rules only for the input fields that are not empty (not required) for the URL
For example,
If I submit a form and the input is empty, I got a validation error "Instagram Link must be valid URL.", However, I want it without requiring,
and if the input is not empty, I want to apply the rule "valid_url"
How can we fix it?
if (!$this->validate([
'instagram' => [
'rules' => 'valid_url',
'errors' => [
'valid_url' => 'Instagram Link must be valid url.',
],
],
])){
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
}
I tried with a permit_link rule, but if I submit it (with an input value like 'mylink' (which is not a valid_url)), it will accept it, but it should not.
Please check the following images and the code:
A form HTML
Result after clicking on edit button
<?= form_open('/settings/edit/1', ['id' => 'setting-form']); ?>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<?= form_label('Instagram'); ?>
<?= form_input(['name' => 'instagram', 'class' => 'form-control', 'id' => 'instagram', 'placeholder' => 'Enter instagram link', 'value' => old('instagram', $setting->instagram)]); ?>
</div>
</div>
</div>
<button type="submit" class="btn btn-dark">Edit</button>
<?= form_close(); ?>
public function edit($id)
{
if (!$this->validate([
'instagram' => [
'rules' => 'permit_empty|valid_url',
'errors' => [
'valid_url' => 'Instagram Link must be valid url.',
],
],
])) {
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
}
die('submitted');
}
It should display "Instagram Link must be valid url" and not "submitted",
first
Use separate variable $rules like this :
$rules = ['data' => 'require']
Then
check if $this->request->getVar("instagram"); is empty / is true or etc.. then set it on the $rules
finally
Do Something like this :
$rules = ['extra_data' => 'require'];
if(!empty($this->request->getVar("instagram"))
$rules["instagram"] = "valid_url";
if ($this->validate(rules){
//do something ...
}else {
//do something ...
}
I just noticed that the following examples:
"mylink", "mylink.com", "https://www.mylink.com"
will consider correct for the rule valid_url in Codeigniter (No errors),
While: "https:://www.mylink.com", "mylink#" will apply the validation and the error is applied.
I want to know that how to write the below given html code in cakePHP 3 syntax :-
<a href="#" class="sidebar-toggle" data-toggle="push-menu" role="button">
<span class="sr-only">Toggle navigation</span>
</a>
you can simply put the span tag inside the html link this way
<?= $this->Html->link(
'<span class="sr-only">Toggle navigation</span>',
'#', [
'class' => "sidebar-toggle",
'data-toggle' => "push-menu",
'role' => "button",
'escape' => false
]); ?>
or you can use the tag method to create the span tag
<?= $this->Html->link(
$this->Html->tag('span', 'Toggle navigation', ['class'=>"sr-only"]),
'#', [
'class' => "sidebar-toggle",
'data-toggle' => "push-menu",
'role' => "button",
'escape' => false
]); ?>
I am making a CRUD application with Codeignater 3.
I have an "Add Customers" form with First name, Last name, Email address, City and a submit button.
The model looks like this:
class Customer extends CI_Model {
public function saveCustomer($data) {
$tbl = $this->db->dbprefix('customers');
$this->db->insert($tbl, $data);
}
}
The controller:
if ($this->form_validation->run()) {
$data = $this->input->post();
$this->load->model('Customer');
if ($this->Customer->saveCustomer($data)) {
$this->session->set_flashdata('response','Customer successfully added');
} else {
$this->session->set_flashdata('response','Failed to save customer');
}
return redirect('home');
}
The View file:
<?php echo form_open('home/save'); ?>
<div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
<?php echo form_input('first_name', '', [
'type' => 'text',
'id' => 'first_name',
'class' => 'form-control',
'value' => '',
'placeholder' => 'First name',
]);
?>
<?php echo form_error('first_name'); ?>
</div>
<div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
<?php echo form_input('last_name', '', [
'type' => 'text',
'id' => 'last_name',
'class' => 'form-control',
'value' => '',
'placeholder' => 'Last name',
]);
?>
<?php echo form_error('last_name'); ?>
</div>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<?php echo form_input('email', '', [
'type' => 'text',
'id' => 'email',
'class' => 'form-control',
'value' => '',
'placeholder' => 'Email address',
]);
?>
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<?php echo form_input('phone', '', [
'type' => 'text',
'id' => 'phone',
'class' => 'form-control',
'value' => '',
'placeholder' => 'Phone number',
]);
?>
</div>
<div class="form-group">
<?php echo form_input('city', '', [
'type' => 'text',
'id' => 'city',
'class' => 'form-control',
'value' => '',
'placeholder' => 'City',
]);
?>
</div>
<div class="form-group">
<?php echo form_input('address', '', [
'type' => 'text',
'id' => 'address',
'class' => 'form-control',
'value' => '',
'placeholder' => 'Address',
]);
?>
</div>
<div class="form-group">
<?php echo form_submit('submit', 'Save', 'class = "btn btn-primary btn-block"'); ?>
</div>
<?php echo form_close(); ?>
The problem:
When I submit the form I get this error: Unknown column 'submit' in 'field list'
Why is that?
Change your $data as follows:
$data = array('column_name1' => $this->input->post('first_name'),
'column_name2' => $this->input->post('last_name'),
'column_name3' => $this->input->post('email'),
'column_name4' => $this->input->post('phone'),
'column_name5' => $this->input->post('city'),
'column_name6' => $this->input->post('address'));
NOTE: There is no need of return in controller, only redirect('home') is needed.
Small change in controller
if ($this->form_validation->run()) {
$data = $this->input->post();
//unset your submit value which are come from submit btn
if(isset($data['submit'])){unset($data['submit'])}
if(isset($data->submit)){unset($data->submit)}
$this->load->model('Customer');
if ($this->Customer->saveCustomer($data)) {
$this->session->set_flashdata('response','Customer successfully added');
} else {
$this->session->set_flashdata('response','Failed to save customer');
}
return redirect('home');
}
<div class="form-group">
<?php echo form_textarea(array('name' => 'txtdescription', 'id' => 'desc', 'class' => "ckeditor", 'value' => set_value('txtdescription', htmlspecialchars_decode($desc)))); ?>
</div>
This is my view code when we get data from db
Add 3 argument for set_value.
Try this:
<div class="form-group">
<?php echo form_textarea(array('name' => 'txtdescription', 'id' => 'desc', 'class' => "ckeditor", 'value' => set_value('txtdescription', $desc,false))); ?>
</div>
how does one load a div using its id to a view?
this is my view
<div id="register" class="animate form">
<section class="login_content">
<form <?php echo form_open('User/register'); ?>
<h1>Create Account</h1>
<div class="inputlogin">
<div class="form-group">
how will i pass it to
if ($this->form_validation->run() === false) {
// validation not ok, send validation errors to the view
$index['title']="Welcome to Hoovie";
$index['mainContent']="login";
$index['results'] = $this->users_model->get_category();
$this->load->view('includes/redirect', $index, $data);
First, form_open tag makes <form> so you can't echo it inside of form it would look like <form <form>>
here is example of login form inside of view:
echo form_open('path/to/controller');
echo form_input(array(
'name' => 'username',
'class' => 'form-control input-lg',
'placeholder' => 'username'
));
echo form_password(array(
'name' => 'password',
'class' => 'form-control input-lg',
'placeholder' => 'password'
));
echo form_submit(array(
'name' => 'LoginSubmit',
'class' => 'btn btn-lg btn-info',
'value' => 'Login'
));
echo form_close();}
After that inside of your controller you have to activate form_validation library if it is not auto-loaded and then you have to set rules for your inputs
example :
$dugme = $this->input->post('LoginSubmit');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if(isset($dugme) && $this->form_validation->run()){
//if everything ok, do something
}
else {
//if validation did not pass load view
$this->load_view('login'); }
and after that inside of view you have to echo errors
//I am using bootstrap so alert-danger will show this div with red background
<?php echo validation_errors('<div class="alert alert-danger">','</div>');?>
you can check more about form validation here :
Codeigniter 3: https://www.codeigniter.com/userguide3/libraries/form_validation.html
Codeigniter 2: https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html