Joomla $this->get('Form') returns NULL - php

I have a view views/detailedforms/view.html.php
defined('_JEXEC') or die;
jimport('joomla.application.component.view');
class Detailed_formViewDetailedforms extends JViewLegacy {
protected $items;
protected $pagination;
protected $state;
protected $params;
protected $form;
/**
* Display the view
*/
public function display($tpl = null) {
$app = JFactory::getApplication();
$this->state = $this->get('State');
$this->form = $this->get('Form');
var_dump($this->form);
}
and model models/forms/detailedforms.xml
<?xml version="1.0" encoding="UTF-8"?>
<form>
<fieldset name="contact" label="Detail_FORM">
<field name="name"
type="text"
id="tailor-made-name"
size="30"
description="FORM_NAME_DESC"
label="FORM_NAME_LABEL"
filter="string"
required="true"
/>
<field name="email"
type="email"
id="email"
size="30"
description="EMAIL_DESC"
label="EMAIL_LABEL"
filter="string"
validate="tailoremail"
required="true"
/>
</fieldset>
$this->form returns null. Please help, how to fix the issue.
Due to this issue, fatal error occurs in the template file.

Related

Auto populate form inputs with Laravel and Live Wire

Goal: on focusout event I want to auto populate html input fields #City #State (full name) #State2 (abbreviation)
Questions
1) how do I get the input passed to the function?
2) What do I need to do to populate the fields?
in resouces\ views: locale.blade.php
<div>
<input type="text" name="City" id="City" value="{{ $city }}">
<input type="text" name="State" id="State" value="{{ $state }}">
<input type="text" name="State2" id="State2" value="{{ $state2 }}">
<input type="text" name="Zip" id="Zip" value="{{ $zip }}"
wire:focusout="setlocale">
</div>
in App\Http\ LiveWire Locale.php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\State;
use Illuminate\Support\Arr;
class Locale extends Component
{
public $zip = "";
public $city = "";
public $state = "";
public $state2 = "";
public function setlocale()
{
$locale_arr=[];
$g = $this->zip;
dd($g); ################## its only returning "" in my dd(); ###################
$z = State::get()->where('zip',$g);
$city = $z->city;
$state = $z->state_name;
$state2 = $z->state2_id;
//TODO somehow render these variables in input fields.
}
public function render()
{
return view('livewire.locale');
}
}
in App\Models\ ** State**
class State extends Model
{
use HasFactory;
}
Tinker
>>> use App\Models\State
>>> $a = State::get()->where('zip','10001');
=> Illuminate\Database\Eloquent\Collection {#70820
all: [
2569 => App\Models\State {#35131
id: 2570,
zip: "10001",
lat: 40.7506,
lng: -73.9972,
city: "New York",
state_id: "NY",
state_name: "New York",
county_name: null,
timezone: null,
},
],
}
With Livewire, you have to bind the inputs to the properties of your class using wire:model. Livewire will then handle setting the value of your inputs to what you have in the component.
<div>
<input type="text" name="City" id="City" wire:model="city" />
<input type="text" name="State" id="State" wire:model="state" />
<input type="text" name="State2" id="State2" wire:model="state2" />
<input type="text" name="Zip" id="Zip" wire:model="zip" wire:focusout="setlocale" />
</div>
Then, instead of using wire:focusout, I recommend that you use a lifecycle-hook in the PHP class instead. So remove wire:focusout="setlocale" from your HTML, and add the following to your PHP class,
public function updated($field, $value)
{
if ($field === 'zip') {
$this->setlocale();
}
}
public function setlocale()
{
$zip = State::where('zip', $this->zip)->first();
$this->city = $zip->city;
$this->state = $zip->state_name;
$this->state2 = $zip->state2_id;
}

Forms and objects in arrays

I am learning OOP in PHP and in an exercise, I don't know how to send form data to objects and enter the objects in an array. I was going through a lot of Youtube tutorials and forums but I couldn't find or understand much.
The exercise first asks for a class to manage the products of a supermarket whose attributes are the numeric key, the description, the price and the stock. It also asks me to define a constructor with parameters as methods.
<?php
class Product{
private $key;
private $description;
private $price;
private $stock;
public function __construct($key, $description, $price, $stock){
$this->key = $key;
$this->description = $description;
$this->price = $price;
$this->stock = $stock;
}
public function setKey($key){
$this->key = $key;
}
public function getKey(){
return $this->key;
}
public function setDescription($description){
$this->description = $description;
}
public function getDescription(){
return $this->description;
}
public function setPrice($price){
$this->price = $price;
}
public function getPrice(){
return $this->price;
}
public function setStock($stock){
$this->stock = $stock;
}
public function getStock(){
return $this->stock;
}
}
Then it asks me to use that class to declare an array of objects and control an inventory of up to 50 products using the POST method. In addition this program must have a menu with the next items: Add product, Remove product, List product, Sort product by key number and Exit.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<body>
<form method="post">
<label for="key">Enter product key</label></br>
<input type="number" name="key" id="key" required></br>
<label for="description">Enter product description</label></br>
<input type="text" name="description" id="description" required></br>
<label for="price">Enter product price</label></br>
<input type="text" name="price" id="price" required></br>
<label for="stock">Enter the stock of the product</label></br>
<input type="number" name="stock" id="stock" required></br>
<button type="submit" name="add" id="add">Add product</button>
<button type="submit" name="baja" id="baja">Remove product</button>
<button type="submit" name="list" id="list">List product</button>
<button type="submit" name="Sort" id="Sort">Sort product</button>
<button type="submit" name="exit" id="exit">Exit</button>
</form>
</body>
</html>
Here is the problem: I don't know how to insert the object in the array without deleting the previous ones and I don't know how to print all the entered objects.
<?php
if (strlen(session_id()) < 1) {
session_start();
}
include_once("product.php");
if(isset($_POST["add"])){
$_SESSION["quantity"] = $_SESSION["quantity"] +1;
$quantity = $_SESSION["quantity"];
if($quantity<=50){
$oproduct = new Product($_POST["key"], $_POST["description"], $_POST["price"],
$_POST["stock"]);
$oproduct->setKey($_POST["key"]);
$oproduct->setDescription($_POST["description"]);
$oproduct->setPrice($_POST["price"]);
$oproduct->setStock($_POST["stock"]);
$_SESSION["prod"]= $oproduct;
print_r($_SESSION["prod"]);
}
}
Dynamically append [] a new product to the array:
$_SESSION["prod"][] = $oproduct;
Or you could use key if it is unique:
$_SESSION["prod"][$_POST["key"]] = $oproduct;
Also, it looks like you have already added the info here:
$oproduct = new Product($_POST["key"], $_POST["description"], $_POST["price"], $_POST["stock"]);
So why are you doing this?
$oproduct->setKey($_POST["key"]);
$oproduct->setDescription($_POST["description"]);
$oproduct->setPrice($_POST["price"]);
$oproduct->setStock($_POST["stock"]);

Laravel send email on form submit

So I have a simple form and I want whenever a user submits the form it to email me, not sure what the problem is as whenever I submit the form I'm getting a 500 error and I'm not sure why.
Here is the form:
<div class="contact" id="contact">
<form action="{{url('/contact')}}" method="post">
{{ csrf_field() }}
<input type="text" class="fname" name="firstname" placeholder="First Name">
<input type="text" class="lname" name="lastname" placeholder="Last Name">
<input type="text" class="address" name="address" placeholder="Address">
<input type="text" class="email" name="email" placeholder="Email">
<textarea id="subject" name="message" placeholder="Message" style="height:200px"></textarea>
<label class="checkbox-label">
<input type="checkbox" class="yard" name="yard"> I Want a Yard Sign
</label>
<br>
<label class="checkbox-label">
<input type="checkbox" class="host" name="host"> Host a Meet and Greet
</label>
<br>
<input type="submit" value="Get More Information">
<br>
</form>
</div>
And in my web routes file I have my two routes setup
Route::get('/contact', function () {
return view('contact');
});
Route::post('/contact', function (Request $request) {
Mail::send(new ContactMail($request));
return redirect('/');
});
I created my Mail file
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ContactMail extends Mailable
{
use Queueable, SerializesModels;
public $email;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
$this->email = $request;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject('New Contact Mail')
->from($this->email->email, $this->email->firstname)
->to('test#gmail.com')
->view('email.contactmail');
}
}
And then my simple blade file
{{ $email->content }}
Whenever I submit the form or try and call the route I get a 500 error and I'm not sure why.
I think you forgot to insert Request $request as a parameter on the class constructor and import the Request class.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Http\Request;
class ContactMail extends Mailable
{
use Queueable, SerializesModels;
public $email;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(Request $request)
{
$this->email = $request;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject('New Contact Mail')
->from($this->email->email, $this->email->firstname)
->to('test#gmail.com')
->view('email.contactmail');
}
}
Also, as per your description, you may not have debug on. Change it at .env file:
APP_DEBUG=TRUE

OCTOBERCMS Form and Model

I'm a newbie in OctoberCms and i don't have much knowledge in Laravel also. While self studying I face a request like this it's a Select if record exist query I need to read the database and look for the match and I'm really confuse.
This is my form in form.htm where I design my Form.
use Drufal\DynamicContentManager\Models\MembersVerification;
==
<form data-request="onSend" accept-charset="UTF8" enctype="multipart/form-data">
<div class="form-group">
<label>First Name:</label>
<input type="text" class="form-control" name="first_name" required>
</div>
<div class="form-group">
<label>Middle Name:</label>
<input type="text" class="form-control" name="middle_name">
</div>
<div class="form-group">
<label>Last Name:</label>
<input type="text" class="form-control" name="last_name" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" >Submit</button>
</div>
</form>
and this my model
<?php namespace Drufal\DynamicContentManager\Models;
use Model;
use Input;
/**
* Model
*/
class MembersVerification extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Disable timestamps by default.
* Remove this line if timestamps are defined in the database table.
*/
public $timestamps = false;
/**
* #var array Validation rules
*/
public $rules = [
];
/**
* #var string The database table used by the model.
*/
public $table = 'drufal_dynamiccontentmanager_members';
public function onSend(){
$fn = Input::get('first_name');
$mn = Input::get('middle_name');
$ln = Input::get('last_name');
$membertbl=$table::where('first_name', '=', $fn)->first();
if ($membertbl === null) {
echo"
<script>
alert('Successfully');
</script>
";
}else{
echo"NO RESULT";
}
}
}
Help the newbie please.
I think you missed the DB:: in your database request:
$users = Db::table('users')->where('votes', 100)->first();
Maybe this documentation will help you:
https://octobercms.com/docs/database/query#where-clauses

Refreshing page null record automatically inserting in database

I am using codeigniter MVC pattern . Whenever i am refreshing the page, Null record automatically inserting in database. In database, I set the all column as null except primary key.
Please help me.
View page code(index.php)
<div class="title">
<p><strong>ADD USER</strong></p>
</div>
<?php echo form_open("Welcome/index"); ?>
<input type="text" name="userId" placeholder="USER ID" style="width:500px">
<input type="date" name="date" style="width:500px">
<input type="text" name="firtsname" placeholder="FIRST NAME" style="width:330px">
<input type="text" name="middlename" placeholder="MIDDLE NAME" style="width:330px">
<input type="text" name="lastname" placeholder="LAST NAME" style="width:330px">
<input type="text" name="mobileno" placeholder="ENTER MOBILE NO." style="width:500px">
<input type="text" name="landline" placeholder="LANDLINE No." style="width:500px">
<textarea name="address" placeholder="ENTER ADDRESS"></textarea>
<input type="text" name="city" placeholder="CITY" style="width:500px">
<input type="text" name="locality" placeholder="LOCALITY" style="width:500px">
<input type="text" name="email" placeholder="ENTER EMAIL" style="width:1010px"></br>
<input type="submit" class="submit" name="SUBMIT">
<?php echo form_close(); ?>
</div>
Model page code(model_add_user.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_add_user extends CI_Model {
public function __construct() {
parent:: __construct();
$this->load->database();
}
public function add_user() {
$data = array(
'userId' => $this->input->post('userId'),
'date' => $this->input->post('date'),
'firtsname' => $this->input->post('firtsname'),
'middlename' => $this->input->post('middlename'),
'lastname' => $this->input->post('lastname'),
'mobileno' => $this->input->post('mobileno'),
'landline' => $this->input->post('landline'),
'address' => $this->input->post('address'),
'city' => $this->input->post('city'),
'locality' => $this->input->post('locality'),
'email' => $this->input->post('email'),
);
$this->db->insert('add_user', $data);
}
}
?>
Controller page code(Welcome.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
//$this->load->view('welcome_message');
$this->load->view('index');
$this->load->model('model_add_user');
$this->model_add_user->add_user();
}
}
?>
In your controller make the following change :
public function index() {
if ($this->input->post()) {
$this->load->model('model_add_user');
$this->model_add_user->add_user();
}
$this->load->view('index');
}
Path up to controller name directly hit the index function of your controller. Here in your code you load view and call add_user function.So every time you refresh the page your model call and null insert into your database
You need to create a separate function for your insert data
public function index()
{
//$this->load->view('welcome_message');
$this->load->view('index');
}
function add_user()
{
$this->load->model('model_add_user');
$this->model_add_user->add_user();
}
And change form action to
<?php echo form_open("Welcome/add_user"); ?>
After successful insertion.. Just redirect it to... function / controller it will refresh the page and prevent the form submission again.
public function index() {
//$this->load->view('welcome_message');
$this->form_validation->set_rules('field', 'Field', 'trim|required');
if ($this->form_validation->run()) {
$this->load->model('model_add_user');
$this->model_add_user->add_user();
redirect('welcome')
} else {
$this->load->view('index');
}
}

Categories