I am testing avatar photo upload functionality in a Symfony 3.4 app. The Symfony documentation assumes that this will be via a form. The page in question does not use form input however so I am having difficulty working out how to write the test.
Here is the html:
<div class="col-md-4 docs-buttons">
<label class="btn btn-info btn-block btn-upload" for="inputImage" title="Upload image file" style="margin-bottom:15px;">
<input class="sr-only" id="inputImage" name="file" type="file" accept="image/*">
<span class="docs-tooltip" data-toggle="tooltip" title="Import image with Blob URLs">
<span class="fa fa-plus"></span> Upload New Photo
</span>
</label>
<div class="btn-group" style="margin-right:10px;">
<button class="btn btn-info" data-method="zoom" data-option="-0.1" type="button" title="Zoom Out">
<span class="docs-tooltip" data-toggle="tooltip" title="$().cropper("zoom", -0.1)">
<span class="fa fa-search-minus"></span>
</span>
</button>
<button class="btn btn-info" data-method="zoom" data-option="0.1" type="button" title="Zoom In">
<span class="docs-tooltip" data-toggle="tooltip" title="$().cropper("zoom", 0.1)">
<span class="fa fa-search-plus"></span>
</span>
</button>
</div>
<div class="btn-group">
<button class="btn btn-info" data-method="rotate" data-option="-22.5" type="button" title="Rotate Left">
<span class="docs-tooltip" data-toggle="tooltip" title="$().cropper("rotate", -22.5)">
<span class="fa fa-rotate-left"></span>
</span>
</button>
<button class="btn btn-info" data-method="rotate" data-option="22.5" type="button" title="Rotate Right">
<span class="docs-tooltip" data-toggle="tooltip" title="$().cropper("rotate", 22.5)">
<span class="fa fa-rotate-right"></span>
</span>
</button>
</div>
</div>
<input type="hidden" id="userId" value="6001777">
</div>
<div class="modal-footer">
<div class="row">
<div class="col-xs-6 text-left">
<a class="btn btn-default" data-dismiss="modal">Cancel</a>
</div>
<div class="col-xs-6">
<button id="save" class="btn btn-info">Save</button>
</div>
</div>
This is a phpunit test written for a similar case, but where the input is a form. Is there any way to refactor this to test the page above?
public function testUpload()
{
# Open the page
...
# Select the file from the filesystem
$image = new UploadedFile(
# Path to the file to send
dirname(__FILE__).'/../uploads/photo.jpg',
# Name of the sent file
'photo.jpg',
# MIME type
'image/jpeg',
# Size of the file
11832
);
# Select the form (adapt it for your needs)
$form = $crawler->filter('input[type=submit]...')->form();
# Put the file in the upload field
$form['... name of your field ....']->upload($image);
# Send it
$crawler = $this->client->submit($form);
# Check that the file has been successfully sent
# (in my case the filename is displayed in a <a> link so I check
# that it appears on the page)
$this->assertEquals(
1,
$crawler->filter('a:contains("filename.jpg")')->count()
);
}
Related
How grab dynamic start_date & end_date form url to href after i click print?
enter image description here
When i click print, start_date & end_date always null
enter image description here
Here my Views:
<form action="/laporanstok/viewlaporanstok" method="get">
<div class="container align-items-center">
<div class="row">
<div class="col-6">
<div class="form-group">
<label class="font-weight-bold">Tanggal Mulai:</label>
<input type="date" id="start_date" name="start_date" class="form-control"
required>
</div>
</div>
<div class="col-6">
<div class="form-group">
<label class="font-weight-bold">Tanggal Akhir:</label>
<input type="date" id="end_date" name="end_date" class="form-control"
required>
</div>
</div>
<div class="container text-center">
<button type="submit" class="btn btn-success"><i class="fas fa-search"></i>
Cari</button>
<a href="{{ route('cetak_stok',['start_date'=>"
????",'end_date'=>"????"],) }}" target="_blank" class="btn
btn-primary"><i class="fas fa-print"></i>
Print</a>
<a href="/laporanstok" class="btn btn-danger"><i class="fas fa-sync-alt"></i>
Reset</a>
</div>
</div>
</div>
</form>
My Models:
class M_Laporan_Stok extends Model
{
use HasFactory;
protected $table = 'tbl_barang';
protected $primaryKey = 'id_barang';
protected $fillable = [
'id_proyek',
'kode_barang',
'nama_barang',
'tipe_barang',
'stok',
'created_at',
'updated_at'
];
}
My Controller:
public function print(Request $request)
{
$data = [
'title' => 'Cetak Stok',
'judul' => 'Halaman Cetak Stok',
'start_date' => $request->start_date,
'end_date' => $request->end_date,
];
return view('/content/laporan_stok/v_cetak_laporan_stok', $data);
}
My Route:
Route::get('/laporanstok/cetak', [LaporanStokController::class, 'print'])->name('cetak_stok');
Change this part of your code
<div class="container text-center">
<button type="submit" class="btn btn-success"><i class="fas fa-search"></i>
Cari</button>
<a href="{{ route('cetak_stok',['start_date'=>"
????",'end_date'=>"????"],) }}" target="_blank" class="btn
btn-primary"><i class="fas fa-print"></i>
Print</a>
<a href="/laporanstok" class="btn btn-danger"><i class="fas fa-sync-alt"></i>
Reset</a>
</div>
This way you have 2 submit buttons. Submitting form gets those dates you need.
<div class="container text-center">
<button type="submit" name="submit" class="btn btn-success"><i class="fas fa-search"></i>
Cari</button>
<button type="submit" name="print" class="btn btn-primary"><i class="fas fa-print"></i>
Print</button>
<a href="/laporanstok" class="btn btn-danger"><i class="fas fa-sync-alt"></i>
Reset</a>
</div>
In controller check what button was clicked.
if (isset($_POST['submit'])) {
//submit button was clicked
} else if (isset($_POST['print'])) {
//print button was clicked
} else {
//no button pressed
}
I have a simple form in which a user can fill the form and can add multiple input fields as he/she wishes,
Here is HTML
<form id="paramsForms">
{{csrf_field()}}
<div class="modal-body">
<dvi class="container h-100">
<div class="d-flex justify-content-center">
<div class="card mt-5 col-md-12 animated bounceInDown myForm" id="multiple-container">
<div class="card-header">
<h4>Bidders Information</h4>
</div>
<div class="card-body" id="add_info">
<div id="dynamic_container">
<small id="bidder">Bidder 1</small>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text br-15"><i class="fa fa-tags"></i></span>
</div>
<input type="text" placeholder="Bidders Name" name="bidders_name[]" class="form-control"/>
</div>
<div class="input-group mt-3">
<div class="input-group-prepend">
<span class="input-group-text br-15"><i class="fa fa-tags"></i></span>
</div>
<input type="text" placeholder="atribute name" name="params_name[]" id="field1" class="form-control"/>
<input type="number" placeholder="atribute value" name="params_value[]" id="field2" class="form-control"/>
<a class="btn btn-secondary btn-sm moreinput_field" id="add_more_input">
<i class="fa fa-plus-circle"></i>
</a>
</div>
</div>
</div>
<div class="card-footer" id="card-footer">
<a class="btn btn-success btn-sm" id="add_more"><i class="fa fa-plus-circle"></i> Add</a>
<!-- <button class="btn btn-success btn-sm float-right submit_btn"><i class="fas fa-arrow-alt-circle-right"></i> Submit</button> -->
</div>
</div>
</div>
</dvi>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
Here is js to save the form
$("#paramsForms").on('submit', function(e){
e.preventDefault();
$.ajax({
type:"POST",
url: "/parameters",
data: $("#paramsForms").serialize(),
success: function(response){
console.log(response)
alert('data saved');
},
error: function(error){
console.log(error)
alert('Data not saved');
}
})
})
Here is controller store function
public function store(Request $request)
{
$parameters = new Parameter;
$parameters->params_name = $request->input('params_name');
$parameters->params_value = $request->input('params_name');
$parameters->bidders_name = $request->input('bidders_name');
// dd($parameters);
$parameters->save();
}
The dd($parameters) in-store function gives the following.
So when I remove dd($parameters) and click submit button I get the following error on console (network).
message: "Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\xampp\htdocs\roayalad-blog\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 869
What is wrong with my codes?
The issue is that you are sending params_name and params_name as an array but controller as handling as strings. Make this change in controller:
public function store(Request $request)
{
$parameters = new Parameter;
$record_count = count($request->input('params_name'));
for($i=0; $i<$record_count; $i++) {
$parameters->params_name = $request->input('params_name')[$i];
$parameters->params_value = $request->input('params_name')[$i];
$parameters->bidders_name = $request->input('bidders_name')[$i];
$parameters->save();
}
}
Above code will execute one query each record. You can also do bulk insertion. This is the link.
Try just
$request->params_name[$i]
Instead of
$request->input(“params_name”)[$i]
I have a set of form fields in PHP. I also added jQuery functionality to clone some fields and add them to the form. However, after I submit the form only the original fields are submitted and not the ones added though cloning.
PHP
<form id="myForm" method="post" action"...">
<div class="row contRow">
<div class="col-md-5">
<input type="text" id="contactInputName['.$i.']" name="name['.$i.']" value="'.$output['0'].'" class="form-control" placeholder="Name">
</div>
<div class="col-md-6">
<input type="text" id="contactInputEmail['.$i.']" name="email['.$i.']" value="'.trim($output['1']).'" class="emlVal form-control">
</div>
<div class="col-md-1 text-right">
<span class="btn btn-default rmContact"><i class="fas fa-times fa-fw fa-lg text-danger"></i></span>
</div>
</div>
<div class="row">
<div class="col-md-12">
<span id="saveContacts" class="btn btn-success"><i class="fas fa-check fa-fw fa-lg"></i> Save Contacts</span>
<span id="addRow" class="btn btn-default"><i class="fas fa-plus-circle fa-fw fa-lg"></i> Add More</span>
</div>
</div>
</form>
JS
$(document).on("click", "#addRow", function() {
var lastRow = $("#addContactsForm").find(".contRow").last();
lastRow.clone().insertAfter(lastRow);
lastRow.find("input").val("");
});
$(document).on("click", "#saveContacts", function() {
$(this).closest("form").submit();
});
What am I missing?
I would remove id="contactInputName['.$i.']" and id="contactInputEmail['.$i.']" because I don't see the need to set these attributes.
If you set name attributes with empty keys, incremented keys will be generated for you. E.g.
name="name[]"
and
name="email[]"
This should eliminate the issue with copied indexes while cloning.
i have following div in project. after clicking on "request for demo" button , it will redirect to next window to registration form (link is mentioned in javascript function). i want to save relevant div id in database after clicking on button and save register.
following is my html and javascript
<div class="row">
<div class="col-md-4 col-sm-6 pos-vertical">
<span><b>Supermarkets, Fruits & Vegetables</b></span>
<hr>
<p>Uninterrupted fast billing, inventory control, CRM & Loyalty programs makes your shoppers happier</p>
<button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
<!--<button type="button" onclick="openWin()" class="btn btn-default" id="demo-btn" data-toggle="modal" data-target="#demoModal">Request for Demo</button>-->
<button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Supermarkets, Fruits and Vegetables" >Request for Demo</button>
</div>
<div class="col-md-4 col-sm-6 pos-vertical">
<span><b>Grocery & Departmental Stores</b></span>
<hr>
<p>POS for grocery, departmental store, hypermarket and convenience store with GSmartPOS</p>
<button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
<button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Grocery and Departmental Stores" >Request for Demo</button>
</div>
<div class="col-md-4 col-sm-6 pos-vertical">
<span><b>Pharmacy, Medical Shop POS</b></span>
<hr>
<p>Prefilled drug index, update stock position and bill from Day One. Manage batches and expiry with ease</p>
<button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
<button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Pharmacy Medical Shop POS" >Request for Demo</button>
</div>
<br>
<div class="col-md-4 col-sm-6 pos-vertical">
<span><b>Apparel & Footwear</b></span>
<hr>
<p>POS Features like matrix-inventory, non-moving stock analysis, can help keep inventory latest and fashionable</p>
<button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
<button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Apparel and Footwear" >Request for Demo</button>
</div>
<div class="col-md-4 col-sm-6 pos-vertical">
<span><b>Electrical & Electronics POS</b></span>
<hr>
<p>Perfect solution to manage serialized inventory for mobile, computer electrical and electronics shops</p>
<button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
<button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Electrical and Electronics POS" >Request for Demo</button>
</div>
<div class="col-md-4 col-sm-6 pos-vertical">
<span><b>Fashion Jewellery Shop</b></span>
<hr>
<p>An integrated, modular & scalable POS for furniture, glass & crockeries, opticals, music, toys & baby shop retail</p>
<button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
<button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Fashion Jewellery Shop" >Request for Demo</button>
</div>
</div>
following function is called on each div button,
<script>
function openWin() {
window.open("register.php");
}
</script>
after clicking on each div button following form will be open in new window,
<div class="register">
<div class="container">
<div class="row main">
<div class="main-register main-center">
<form class="" method="post" id="contact-form">
<div class="form-group">
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon" id="register-icon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" id="name" placeholder="Enter your Name"/>
</div>
</div>
</div>
<div class="form-group">
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon" id="register-icon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" id="email" placeholder="Enter your Email"/>
</div>
</div>
</div>
<div class="form-group">
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon" id="register-icon"><i class="fa fa-phone fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" id="contact_no" placeholder="Enter your contact number"/>
</div>
</div>
</div>
<input type="hidden" id="requestType">
<div class="form-group ">
<button class="btn btn-primary btn-lg btn-block register-button submit">Register</a>
</div>
</form>
<span class="success" style="display:none">Thank You for Register with us.</span>
</div>
</div>
</div>
</div>
form will be save using ajax and php
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" >
$(function() {
$(".modalBtn").click(function(){
var formName = $(this).attr("id");
$('#requestType').val(formName);
$('.success').hide();
$('.error').hide();
$('#contact-form').show();
$("#demoModal").modal();
});
$(".submit").click(function() {
var name = $("#name").val();
var requestType = $('#requestType').val();
var contact_no = $("#contact_no").val();
var email = $("#email").val();
//alert(requestType);
var dataString = 'name='+ name + '&contact_no=' + contact_no + '&email=' + email+ '&requestType=' + requestType;
if(name=='' || contact_no=='' || email=='')
{
$('.success').fadeOut(200).hide();
$('.error').show();
}
else
{
$.ajax({
type: "POST",
url: "register.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
$('#contact-form').hide();
}
});
}
return false;
});
});
</script>
Php code to insert data is,
<?php
require('connection.php');
if($_POST) {
$id = $_POST['regid'];
$name = $_POST['name'];
$contact_no = $_POST['contact_no'];
$email = $_POST['email'];
$requestType = $_POST['requestType'];
if ($id == '') {
$sql = "INSERT INTO registration(name,contact_no,email,requestType) VALUES ".
"('".$name."', '".$contact_no."', '".$email."','".$requestType."')";
}
$query_result = mysql_query( $sql );
if(!$query_result ) {
echo 'Could not enter data: ' . mysql_error();
} else {
//header("Location: lead-view.php?s=Y");
$successMsg = 'Record instered successfully';
}
}
?>
i want save those div id as in url after submitting form of relevant dive click button. url will be save in "requestType" field in database. please help.
You can make use of sessionStorage for passing the requestType value.
https://www.nczonline.net/blog/2009/07/21/introduction-to-sessionstorage/
While calling openWin() pass the div value to it. For example:
<button type="button" onclick="openWin('abc')" class="modalBtn btn btn-default" id="Supermarkets, Fruits and Vegetables" >Request for Demo</button>
Then save this value in sessionStorage as follows:
function openWin(div_value) {
sessionStorage.setItem("registerType", div_value);
window.open("register.php");
}
You can retrieve this variable in register.php:
var registerType = sessionStorage.getItem("registerType");
Add this value to the hidden input control in register form and save it to database.
<input type="hidden" id="requestType">
How can I pass with form what is selected from dropdown field? So that url goes to ?s=sdf&tag=bookor ?s=sdf&tag=newspaper in this example
http://codepen.io/filaret/pen/yazXKQ
I couldn't create my design goal with <select><option>... inside input element so I used Bootstrap dropdown with .input-group-btn.
<form action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div class="input-group">
<input type="text">
// SELECT OPTIONS
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><i class="fa fa-book"></i></a></button>
<ul class="dropdown-menu">
<li><i class="fa fa-book"></i></li>
<li><i class="fa fa-newspaper-o"></i></li>
</ul>
</div>
// SUBMIT BUTTON
<div class="input-group-btn">
<button type="submit" class="btn btn-default" data-toggle="dropdown">Submit</button>
</div>
</div>
</form>
I managed to make it work with this
<input id="search-hidden" type="hidden" value="">
$selectWrapper.find(".dropdown-menu li").click(function() {
var $selectedIcon = $(this).find('.fa');
$selectWrapper.find("#search-hidden").val( $selectedIcon.data('value') );
});