How can loop through different text fields which have same class name - ruby

MY HTML
<div class='participants'>
<div class="participant_period" data-view="scorecards/participant_edit" data-model_id="348292" data-model_name="scorecard_participants" data-view-data.model="{"type":"model","key":"model:scorecard_participants:348292","model":"scorecard_participants","id":"348292"}" data-model-name="scorecard_participants" data-model-id="348292"><div class="content-item content-full content-results content-box-score">
<span class="profile-image"><img src="//d3f4b3d8pglafo.cloudfront.net/system/teams/images/348292/thumb/profile" /></span>
<span class="profile-name">Web-driver Baseball Team1</span>
<div class='row'>
<div class='col col-control'>
<div class='final-header'>Score</div>
<div class='period-score'>
<input name=final_score type="text" size="5" placeholder="-" value="" />
</div>
</div>
</div>
</div>
</div>
<div class="participant_period" data-view="scorecards/participant_edit" data-model_id="347821" data-model_name="scorecard_participants" data-view-data.model="{"type":"model","key":"model:scorecard_participants:347821","model":"scorecard_participants","id":"347821"}" data-model-name="scorecard_participants" data-model-id="347821"><div class="content-item content-full content-results content-box-score">
<span class="profile-image"><img src="//d3f4b3d8pglafo.cloudfront.net/system/teams/images/347821/thumb/102px-RedSoxPrimary_HangingSocks.svg.png" /></span>
<span class="profile-name">Boston Red Sox</span>
<div class='row'>
<div class='col col-control'>
<div class='final-header'>Score</div>
<div class='period-score'>
<input name=final_score type="text" size="5" placeholder="-" value="" />
</div>
</div>
</div>
</div>
</div>
I want to fill up the tex fields " input name=final_score " with random data with loop
can anyone suggest me how can i do it
my Watir code is like below
def edit_score_fields
field = #browser.text_field(:name => 'final_score')
data = rand(1...2)
s = #browser.text_fields(:class => "period-score").size
0.upto(s) { |i| #browser.text_field(:name => 'final_score', :index => '#{i}'.to_i).set(data) }
end

Try something like:
#browser.inputs(:name => 'final_score').each do |field|
field.send_keys data
end

Related

Laravel form submit unsuccessful returns blank page instead of redirect home

I have a laravel form to create a new product entry in a database on submit, am supposed to be redirected back but I get a completely blank page without any errors. There is no new entry in my database when I check. The form is made up of various text fields, an image URL, and a multi-selected Image URL
please this is my blade template
<form method="POST" action="{{ route('products.store') }}" enctype="multipart/form-data">
<h4 class="card-title">Create Product</h4><br><br>
#csrf
<div class="row">
<div class="col">
<div class="row mbr-1">
<label for="example-text-input" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-6">
<input name="name" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input"
class="col-sm-2 col-form-label">Category</label>
<div class="col-sm-6">
<input name="name" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Price</label>
<div class="col-sm-6">
<input name="catchy_title" class="form-control" type="number"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Status</label>
<div class="col-sm-6">
<input name="status" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Product
Description</label>
<div class="col-sm-8">
<textarea id="elm1" name="long_description" placeholder="Please enter a vivid description of the product"></textarea>
</div>
</div>
<!-- end row -->
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Tags</label>
<div class="col-sm-6">
<input name="tags" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Estimated
Delivery
Time</label>
<div class="col-sm-6">
<input name="estimated_delivery_time" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Available
Quantity</label>
<div class="col-sm-6">
<input name="available_quantity" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Colors</label>
<div class="col-sm-6">
<input name="colors" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Supplier's
Name</label>
<div class="col-sm-6">
<input name="supplier_name" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Supplier's
Contact</label>
<div class="col-sm-6">
<input name="supplier_contact" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Video Url /
Link</label>
<div class="col-sm-6">
<input name="video_description" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Primarry
Image</label>
<div class="col-sm-6">
<input name="primary_image" accept="image/*" class="form-control"
type="file" id="image">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label"> Primary Image
Preview</label>
<div class="col-sm-6">
<img id="showImage" class="" width="300px"
src="{{ !empty($Product->image) ? url('upload/products/' . $product->image) : url('upload/no_image.jpg') }}"
alt="Hero image">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Add Other
Images</label>
<div class="col-sm-6">
<input name="multi_image[]" accept="image/*" class="form-control"
type="file" id="image" multiple="">
</div>
</div>
<!-- end row -->
</div>
</div>
<input type="submit" class="btn btn-info waves-effect waves-light"
value="Create Product">
</form>
and this is my store function
public function store(Request $request)
{
if ($request->file('image')) {
$image = $request->file('image');
$name_gen = hexdec(uniqid()).'.'.$image->getClientOriginalExtension(); // 3434343443.jpg
Image::make($image)->resize(523,605)->save('upload/home_about/'.$name_gen);
$save_url = 'upload/products/'.$name_gen;
$this->validate($request, [
'name' => $request->name,
'category' => $request->category,
'price' => $request->price,
'description' => $request->description,
'status' => $request -> status,
'tags' => $request -> tags,
'estimated_delivery_time' => $request->estimated_delivery_time,
'available_quantity' => $request->available_quantity,
'colors' => $request->colors,
'supplier_name' => $request->supplier_name,
'supplier_phone' => $request->supplier_phone,
'video_description' => $request->video_description,
'primary_image' => $save_url,
'other_images' => $save_url,
]);
$notification = array(
'message' => 'Product created successfully',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
Please what am I doing wrong?
thank you for taking some time to review
you are validating the request but not saving it to database.
after the validation and before the return you should do like this to save in database.
$item=new YOUR_MODEL_NAME:
$item->name => $request->name,
$item->category => $request->category,
$item->price => $request->price,
$item->description => $request->description,
$item->status => $request -> status,
$item->tags => $request -> tags,
$item->estimated_delivery_time => $request->estimated_delivery_time,
$item->available_quantity => $request->available_quantity,
$item->colors => $request->colors,
$item->supplier_name => $request->supplier_name,
$item->supplier_phone => $request->supplier_phone,
$item->video_description => $request->video_description,
$item->primary_image => $save_url,
$item->other_images => $save_url,
$item->save();
$this->validate($request, [
'name' => $request->name,
'category' => $request->category,
'price' => $request->price,
'description' => $request->description,
'status' => $request -> status,
'tags' => $request -> tags,
'estimated_delivery_time' => $request->estimated_delivery_time,
'available_quantity' => $request->available_quantity,
'colors' => $request->colors,
'supplier_name' => $request->supplier_name,
'supplier_phone' => $request->supplier_phone,
'video_description' => $request->video_description,
'primary_image' => $save_url,
'other_images' => $save_url,
]);
In the snippet above you're performing the validation, but I don't see a request to persist the data to your database in your store() function.
It's recommended to utilize the functionality provided by the framework when checking for request input presence. These functions usually provide quality-of-life aspects such as checking for multiple input presence and conditional callbacks as well.
https://laravel.com/docs/9.x/requests#determining-if-input-is-present
if ($request->has('name')) {
//
}
You should also be calling the validate() method on $request, not $this.
First, I'd like to recommend that you utilize a Form Request
https://laravel.com/docs/9.x/validation#form-request-validation
Second, use the validated data and mass assignment when storing the new model. If the validated data structure is not 1:1 with the table schema, you could always assign the values manually but this will result in a bloated controller. One possible solution is to utilize a DTO (Data Transfer Object) and provide the validated() array to the DTO when using mass assignment.
// Without DTO
Model::create($modelStoreRequest->validated());
// With DTO
$DTO = new MyDTO($modelStore$request->validated())->toArray();
Model::create($DTO);
Note that if the incoming request fields do not pass the given validation rules Laravel will automatically redirect the user back to their previous location. In addition, all of the validation errors and request input will automatically be flashed to the session.
Try to be specific, pass route where you want to redirect like below:
return redirect()->route('route-name-here')->with($notification);

how to select and show data comment by id post in codeigniter

master peace of codeigniter. im new user for this framework.
i have problem to show comment data by id_diskusi in the single post of diskusi. i try to show data using 'where' in the lybrary code but its still not show the data. i hope the people at there can help me & solved this problem.
i use library to get data from database
public function setuju(){
$data=$this->CI->db->query("SELECT komentar.id AS id,
komentar.diskusi_id AS id_diskusi,
komentar.pilih AS pilih,
komentar.nama AS nama,
komentar.email AS email,
komentar.pesan AS pesan,
komentar.tanggal AS tanggal,
diskusi.diskusi_id as nomor_diskusi
FROM diskusi, komentar
WHERE diskusi.diskusi_status='publish' AND komentar.pilih='1' AND komentar.diskusi_id=diskusi.diskusi_id ORDER BY komentar.id DESC
");
return $data->result_array();
}
i use this code to filter data who data on komentar will be show by id of diskusi.
komentar.diskusi_id=diskusi.diskusi_id
but still not work
and its my controller
$data['setuju']=$this->diskusi->setuju();
code in the view
<div id="netral" class="tab-pane">
<?php
foreach ($netral AS $value) {
echo "<div class='box box-body no-border'>
<div class='row'>
<div class='col-md-2'>
<div class='box-profile'>"; ?>
<img alt='user image' style='margin-top:10px' class='profile-user-img img-responsive img-circle' src='<?php echo img_user_url('user.png'); ?>'>
<?php echo"
<h3><center>$value[nama]</center</h3>
</div>
</div>
<div class='col-md-10'>
<div class='callout callout-danger lead'><span class='pull-right'>".format_tanggal($value['tanggal'])."</span><br>
<p>$value[pesan]</p>
</div>
</div>
</div>
</div>";
}?>
</div><!-- /.tab-pane -->
<div class="box box-danger">
<div class="box-header with-border">
<div class="user-block">
<h3 class="no-margin">Tinggalkan Komentar</h3>
</div><!-- /.user-block -->
<div class="box-tools">
<button data-widget="collapse" class="btn btn-danger btn-sm"><i class="fa fa-minus"></i></button>
</div><!-- /.box-tools -->
</div>
<div class="box-body">
<form method='POST' id='komentar' action='<?php echo baseURL('form_visitors/komentar'); ?>' autocomplete='off' method="post" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="inputName">Name</label>
<div class="col-sm-10">
<input type="hidden" name="diskusi_id" value="<?php echo "$diskusi[id]";?>">
<input type="text" placeholder="Nama" data-original-title="Masukkan Nama" required='required' name="nama" id="inputName" class="form-control">
<input type='hidden' class='form-control' name='url' value='<?php echo current_url() ?>' />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="inputEmail">Email</label>
<div class="col-sm-10">
<input type="email" name="email" data-original-title="Masukkan Email" placeholder="Email" required='required' id="inputEmail" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"></label>
<div class="col-sm-10">
<div class="checkbox">
<label>
<input type="radio" name="pilih" value="1"> <i class="fa fa-thumbs-o-up margin-r-5 text-green"> <b>Setuju</b></i>
</label>
<label>
<input type="radio" name="pilih" value="2"> <i class="fa fa-square margin-r-5 text-yellow"> <b>Netral</b></i>
</label>
<label>
<input type="radio" name="pilih" value="3"> <i class="fa fa-thumbs-o-down margin-r-5 text-red"> <b>Tidak Setuju</b></i>
</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="inputExperience">Komentar</label>
<div class="col-sm-10">
<textarea placeholder="Komentar" name="pesan" id="inputExperience" required='required' class="form-control"></textarea>
</div>
</div>
<div class='form-group'>
<label class="col-sm-2 control-label" for="inputExperience"></label>
<div class="col-sm-10">
<div id='recaptcha1'></div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button class="btn btn-danger" type="submit" id="submit">Submit</button>
<div class="cssload" style="display: none; width: 100px">
<div class="cssload-tube-tunnel"></div>
</div>
</div>
</div>
</form>
</div>
</div>
how can i show comment data according with single post of diskusi who have comment. Thank you very much in advance! sorry for my english.
I can see you are fetching the data from two different tables but you have not used 'JOIN'.
you can try the following query string :
public function setuju(){
$data=$this->CI->db->query("SELECT komentar.id AS id,
komentar.diskusi_id AS id_diskusi,
komentar.pilih AS pilih,
komentar.nama AS nama,
komentar.email AS email,
komentar.pesan AS pesan,
komentar.tanggal AS tanggal,
diskusi.diskusi_id as nomor_diskusi
FROM komentar, diskusi
JOIN diskusi
ON diskusi.diskusi_id=komentar.diskusi_id
WHERE diskusi.diskusi_status='publish' AND komentar.pilih='1' ORDER BY komentar.id DESC
");
return $data->result_array();
}

How to fill out login form with mechanize in Ruby?

Below the form that I wish to fill with Mechanize; already tried the model commonly used (example there: Using Ruby and Mechanize to fill in a remote login form mystery); but didn't succeed.
<form method="post" action="/sso/login.php" id="form-login-page">
<div id="form-login-container-page" style="color:red;text-align:center;width:100%;margin:10px 0"></div>
<input type="hidden" name="minimalist" value="1">
<input type="hidden" name="SSO_Context" value="/pdf/telecharger2.php?pdfpf=&pdfg=%2Fpdf%2Ftelecharger.php%3Fdir%3DJOURNAL%26file%3D20140603.pdf">
<div class="clear"> </div>
<label>Email<span>*</span></label>
<div class="insc-saisie">
<input class="insc-saisie-champ" type="text" id="login-page" name="login" value="">
</div>
<div class="clear"> </div>
<label>Mot de passe<span>*</span></label>
<div class="insc-saisie">
<input class="insc-saisie-champ" type="password" id="password-page" name="password" value="">
</div>
<div class="clear"> </div>
<label><input type="checkbox" unchecked=""></label>
<div class="insc-saisie">Se souvenir</div>
<div class="clear"> </div>
<label> </label>
<div class="insc-saisie">
Mot de passe oubliƩ ?
</div>
<div class="clear"> </div>
<label> </label>
<div class="insc-saisie">
<input class="b-connexion" type="image" src="/img/trans.gif">
</div>
<div class="clear"> </div>
<div class="clear"> </div>
<label><span>*</span></label>
<div class="insc-saisie">Saisie obligatoire</div>
<div class="clear"> </div>
</form>
Here my attempt
form = agent.page.parser.css('form')[1]
agent.page.forms[1]["login"] = "my_login"
agent.page.forms[1]["password"] = "my_password"
agent.page.forms[1].submit
The following code should work:
page = agent.get("your_page_url")
form = page.form_with(:id => 'form-login-page')
form.login = "my_login"
form.password = "my_password"
form.submit

Silverstripe - Form with custom template shows wrong validation errors

I have the following custom form
$list = DataObject::get('AreaOfExpertise');
foreach ($list as $l) {
$source[$l->ID] = $l->Name;
}
$fields = new FieldSet(
new TextField('Customer'),
new TextField('ProjectName'),
new DateField('WinDate'),
new TextareaField('Details'),
new OptionsetField('AreaOfExpertiseID', 'Area Of Expertise', $source),
new TextField('ProjectValueLength'),
new TextField('DeliverTimeframe'),
new TextField('ProjectTeam'),
new TextField('Contact')
);
$actions = new FieldSet(
new FormAction('doSubmit', 'Submit')
);
$requiredFields = array(
'Customer',
'ProjectName',
'WinDate',
'Details',
'AreaOfExpertiseID',
'ProjectValueLength',
'DeliverTimeframe',
'ProjectTeam',
'Contact',
);
$validator = new RequiredFields($requiredFields);
$form = new Form($this, 'SubmissionForm', $fields, $actions, $validator);
$form->setTemplate('Forms/NewWinForm');
return $form;
and my template for this form is
<form $FormAttributes>
<% if Message %>
<p id="{$FormName}_error" class="message $MessageType">$Message</p>
<% else %>
<p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
<% end_if %>
<fieldset id="formContainer" class="clearfix NewWinForm">
<div id="Customer" class="row field text">
<label class="left" for="{$FormName}_Customer">Customer</label>
<div class="middleColumn">$dataFieldByName(Customer)</div>
</div>
<div id="ProjectName" class="row field text">
<label class="left" for="{$FormName}_ProjectName">Project name</label>
<div class="middleColumn">$dataFieldByName(ProjectName)</div>
</div>
<div id="WinDate" class="row field text">
<label class="left" for="{$FormName}_WinDate">Win date</label>
<div class="middleColumn">$dataFieldByName(WinDate)</div>
</div>
<div id="AreaOfExpertiseID" class="row field text">
<label class="left" for="{$FormName}_AreaOfExpertiseID">Area of expertise</label>
<div class="middleColumn">$dataFieldByName(AreaOfExpertiseID)</div>
</div>
<div id="Details" class="row field textarea">
<label class="left" for="{$FormName}_Details">Project details</label>
<div class="middleColumn">$dataFieldByName(Details)</div>
</div>
<div id="ProjectValueLength" class="row field text">
<label class="left" for="{$FormName}_ProjectValueLength">Project value/length</label>
<div class="middleColumn">$dataFieldByName(ProjectValueLength)</div>
</div>
<div id="DeliverTimeframe" class="row field text">
<label class="left" for="{$FormName}_DeliverTimeframe">Delivery timeframe</label>
<div class="middleColumn">$dataFieldByName(DeliverTimeframe)</div>
</div>
<div id="ProjectTeam" class="row field text">
<label class="left" for="{$FormName}_ProjectTeam">Project team</label>
<div class="middleColumn">$dataFieldByName(ProjectTeam)</div>
</div>
<div id="Contact" class="row field text">
<label class="left" for="{$FormName}_Contact">Contact</label>
<div class="middleColumn">$dataFieldByName(Contact)</div>
</div>
</fieldset>
<div class="Actions">
<% if Actions %>
<% control Actions %>$Field<% end_control %>
<% end_if %>
</div>
</form>
When I submit the form, instead of "Please fill out "Customer", it is required." message I get "Please fill out "this", it is required."
This is happening for all fields apart from Details field!
Can someone please tell me what I am doing wrong? It's killing me...
Ok, after some digging in sapphire/javascript/Validator.js, I found that the first class of the wrapper div must be field so I replaced this
<div id="Customer" class="row field text"> with <div id="Customer" class="field text row"> and everything works just fine.
This should be done by creating a form class and implementing the "forTemplate()" function. See SilverStripe Forms.
Also, instead of creating the "AreaOfExpertise" source array the way you have, there's a convenience method, toDropDownMap(), that does this for you.
$source = $list->toDropDownMap('ID', 'Name');
Hope this helps.

jquery ajax, array and json

I am trying to log some input values into an array via jquery and then use those to run a method server side and get the data returned as JSON.
The HTML looks like this,
<div class="segment">
<div class="label">
<label>Choose region: </label>
</div>
<div class="column w190">
<div class="segment">
<div class="input">
<input type="checkbox" class="radio" value="Y" name="area[Nationwide]" id="inp_Nationwide">
</div>
<div class="label ">
<label for="inp_Nationwide">Nationwide</label>
</div>
<div class="s"> </div>
</div>
</div>
<div class="column w190">
<div class="segment">
<div class="input">
<input type="checkbox" class="radio" value="Y" name="area[Lancashire]" id="inp_Lancashire">
</div>
<div class="label ">
<label for="inp_Lancashire">Lancashire</label>
</div>
<div class="s"> </div>
</div>
</div>
<div class="column w190">
<div class="segment">
<div class="input">
<input type="checkbox" class="radio" value="Y" name="area[West_Yorkshire]" id="inp_West_Yorkshire">
</div>
<div class="label ">
<label for="inp_West_Yorkshire">West Yorkshire</label>
</div>
<div class="s"> </div>
</div>
<div class="s"> </div>
</div>
I have this javascript the detect whether the items are checked are not
if($('input.radio:checked')){
}
What I dont know is how to get the values of the input into an array so I can then send the information through AJAX to my controller. Can anyone help me?
You can use jQuery's each() function.
$('input.radio:checked').each(function() {
//put each of the selected check boxes into an array
});
You need to serialize your input with - serialize()
var data = $('#MYFORM').serialize();

Resources