I am trying to add a css class "form-control" with my all input field using Cakephp Helper.
I already created a Helper
class BootstrapFormHelper extends Helper
{
protected $_defaultConfig = [];
public function control($fieldName, array $options = []){
$options['class'] = 'form-control';
return parent::control($fieldName, $options);
}
}
I also called it in appView
public function initialize()
{
$this->loadhelper('BootstrapForm');
}
But no any class added in my form input fields. How can I add a css class with my all input fields ?
You can try it like
class BootstrapFormHelper extends Helper
{
public $helpers = ['Form'];
public function control( $name, $options = [] )
{
if( !isset( $options['class'] ) ) {
$options['class'] = 'form-control';
}
return $this->Form->control( $name, $options );
}
}
After call it in View/appView.php
You can use it in your view like
<?= $this->BootstrapForm->control('username') ?>
output :
<input type="text" name="username" class="form-control" id="username">
Related
I am using first time laravel livewire in my project. I am trying to create one simple filter option and this filter will trigger from a dropdown. My problem is when a dropdown value change its completely hide my component view.
I am not sure where i am making a mistake in this but i want to know the best way to resolve this.
my code is this Livewire component
<?php
namespace App\Http\Livewire\Frontend\Boat;
use App\Models\Product;
use Livewire\Component;
class Listing extends Component
{
public $filter_option;
// public $message;
public $product;
public function mount()
{
$this->product = Product::where([
'type_id' => 1,
'status' => 1,
])->orderBy('name', 'asc')->get();
}
public function filter_speed()
{
$this->product = Product::where([
'type_id' => 1,
'status' => 1,
])->orderBy('name', 'asc')->get();
}
public function render()
{
return view('livewire.frontend.boat.listing');
}
}
My blade is
<select wire:model="filter_option" class="form-control" wire:change='filter'>
<option value="#">Filter</option>
<option value="speed">Speed</option>
</select>
//or
<button wire:click='filter_speed'>Filter by speed</button> <!-- Simple button click -->
In my opinion in your case its not necessary to use Query on mount().
But If in case of Filter u can use this method:
<?php
// Livewire
namespace App\Http\Livewire\Frontend\Boat;
use App\Models\Product;
use App\View\Components\comment;
use Livewire\Component;
class Listing extends Component
{
public $filter_option;
// public $message;
// public $product;
public $orderBy = 'id';
public $orderType = 'desc';
public function filter_speed()
{
$this->orderBy = 'speed';
// $this->emit('filter_speed');
}
public function render()
{
$product = Product::where([
'type_id' => 1,
'status' => 1,
])
->orderBy($this->orderBy, $this->orderType)
->get();
return view('livewire.frontend.boat.listing', compact('product'));
}
}
<button wire:click.prevent='filter_speed'>Filter by speed</button>
Instant of making many Query make one with custom variable
I have 2 fields for postal code main and sub.
<input type="text" name="postal01">
<input type="text" name="postal02">
I would like to have validation for numeric and size of both fields added.
What I want to do is showing validation error for one field as postal_cd, not each field error.
I had tried in a request class extended FormRequest.
class MemberRequest extends FormRequest
{
public function all($keys = null)
{
$result = parent::all($keys);
if($this->filled('postal01') && $this->filled('postal02')) {
$results['postal_code'] = $this->input('postal01') . $this->input('postal02');
}
return $result;
}
However it did not work like I expected.
How can I handle this case?
You can use an After Validation Hook. Add the following to your Form Request:
/**
* Configure the validator instance.
*
* #param \Illuminate\Validation\Validator $validator
* #return void
*/
public function withValidator($validator)
{
$validator->after(function ($validator) {
if($validator->errors()->has('postal01') || $validator->errors()->has('postal02')) {
$validator->errors()->add('postal_cd', 'Please enter a postal code');
}
});
}
... and then to display it on your blade:
{{ $errors->first('postal_cd') }}
That was my typo.
class MemberRequest extends FormRequest
{
public function all($keys = null)
{
$result = parent::all($keys);
if($this->filled('postal01') && $this->filled('postal02')) {
$result['postal_code'] = $this->input('postal01') . $this->input('postal02');
// $results['postal_code'] = $this->input('postal01') . $this->input('postal02');
}
return $result;
}
Returned value should have been $result. But it was typo $results['postal_code'].
It works.
I created a facade that works well, a using service container :
My class:
namespace App\Classes;
class Administration {
public function message_success()
{
$message = "success";
return $message;
}
}
My view:
{{ App::make('administration')->message_success() }}
Resultat: Ok
But when I want to work with a array, I can not retrieve a value in my view with App::make.
My class:
namespace App\Classes;
class Administration {
public function message_test($message, array $type = array())
{
$type = ['message1' => 'valeur1', 'message2' => 'valeur2'];
return $message;
}
}
How to build the syntax in my view?
In your view
{{ App::make('administration')->message_test($message) }}
In your class
namespace App\Classes;
class Administration {
public function message_test($message)
{
$type = ['message1' => 'valeur1', 'message2' => 'valeur2'];
return $type[$message];
}
}
Instead of
namespace App\Classes;
class Administration {
public function message_test($message, array $type = array())
{
$type = ['message1' => 'valeur1', 'message2' => 'valeur2'];
return $message;
}
}
As you can see I dropped the $type parameter, you are defining/overwriting it within your method. Also, to return the value of message1 you need to access $type with the key being message1.
I'm attempting to submit a form with codeigniter and when i submit the form a get a blank page. Where did i go wrong?
View
echo "<form class='order_ctn_parent' method='POST' action='add/insert_orders'>";
//inputs
echo "</form>";
Controller
class Add extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index() {
}
public function insert_orders()
{
$this->load->model('order_model', 'order');
$this->order->insert_orders();
redirect('view_orders', 'location');
}
}
Model
class Order_model extends CI_Model {
public function insert_orders()
{
$DB2 = $this->load->database('orders', TRUE);
$timber_array = $this->input->post("timber_choose");
$products_array = $this->input->post("product_choose");
$qty_array = $this->input->post("quantity");
$price_array = $this->input->post("price");
$qty_array = $this->input->post("quantity");
$loop = 0;
foreach($products_array as $product) {
$price = str_replace("£","",$price_array[$loop]);
$data = array(
'product_code' => "",
'timber_type' => $timber_array[$loop],
'product' => $product,
'quantity' => $qty_array[$loop],
'price' => $price
);
$DB2->insert('timber_order_products', $data);
$loop++;
}
}
}
Try something like this
?>//end of php
<form class='order_ctn_parent' method='POST' action='<?php echo base_url()?>add/insert_orders'>
</form>
and load model in __construct(), like this
public function __construct()
{
parent::__construct();
$this->load->model('order_model', 'order');
}
and no need of check $DB2 = $this->load->database('orders', TRUE);
and Codeigniter insert should be Like This
Please check your url after submitting . i am sure your form action is not working . give proper controller and its function url into Form action . example is here .
<?php echo site_url('add/insert_orders);?>
where add will be your controller and insert_orders will be function of add controller.
I have a base controller with a method to return a twitter feed to my view.
I want to move this in the view from the page view to the default blade to reduce redundancy as it will be appearing site wide. How do I pass data from the base controller to blade?
I can send it to my view from the page controller like so:
public function get_index()
{
..................
$this->layout->nest('content', 'home.index', array(
'tweets' => $this->get_tweet()
));
}
and in the view, output it like this:
if ($tweets)
{
foreach ($tweets as $tweet)
{
..............
I want to do all this from within default.blade.php and my Base_Contoller:
<?php
class Base_Controller extends Controller {
/**
* Catch-all method for requests that can't be matched.
*
* #param string $method
* #param array $parameters
* #return Response
*/
public function __call($method, $parameters)
{
return Response::error('404');
}
public function get_tweet()
{
...........
return $tweets;
}
}
How is this possible?
//////////////////////UPDATE/////////////////////////////
application/models/tweets.php
<?php
class Tweets {
public static function get($count = 3)
{
Autoloader::map(array(
'tmhOAuth' => path('app').
'libraries/tmhOAuth-master/tmhOAuth.php',
'tmhUtilities' => path('app').
'libraries/tmhOAuth-master/tmhUtilities.php'
));
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'xxx',
'consumer_secret' => 'xxx',
'user_token' => 'xxxxx',
'user_secret' => 'xxxxx',
'curl_ssl_verifypeer' => false
));
$code = $tmhOAuth->request('GET',
$tmhOAuth->url('1.1/statuses/user_timeline'), array(
'screen_name' => 'xxx',
'count' => $count
));
$response = $tmhOAuth->response['response'];
$tweets = json_decode($response, true);
return $tweets;
}
}
application/views/widgets/tweets.blade.php
#foreach ($tweets)
test
#endforeach
application/views/layouts/default.blade.php
....
{{ $tweets }}
....
application/composers.php
<?php
View::composer('widgets.tweets', function($view)
{
$view->tweets = Tweets::get();
});
View::composer('layouts.default', function($view)
{
$view->nest('tweets', 'widgets.tweets');
});
application/controllers/base.php
<?php
class Base_Controller extends Controller {
/**
* Catch-all method for requests that can't be matched.
*
* #param string $method
* #param array $parameters
* #return Response
*/
public $layout = 'layouts.default';
public function __call($method, $parameters)
{
return Response::error('404');
}
}
application/controllers/home.php
<?php
class Home_Controller extends Base_Controller {
public $layout = 'layouts.default';
public $restful = true;
public function get_index()
{
Asset::add('modernizr', 'js/thirdparty/modernizr.js');
Asset::add('jquery',
'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
Asset::add('scripts', 'js/scripts.js');
$this->layout->title = 'title';
$this->layout->nest('content', 'home.index', array(
//'data' => $some_data
));
}
}
Is giving me an
Undefined variable: tweets
error
Step 1 - Make a view just for your tweets, let's call it widgets/tweets.blade.php, that will accept your $tweets data. This makes it very easy to cache the tweets view in the future if you want a little more performance. We also want a model that will generate the tweet data for you.
Step 2 - Pass the tweet data into your tweets view, let's use a View Composer for this so the logic is kept with (but outside) the view.
Step 3 - Create your default layout, let's call this layout/default.blade.php. This will accept $content and $tweets. We'll nest the tweets view with another View Composer. You can nest the $content in your controller actions.
Step 4 - Set the $layout on your Base_Controller.
Step 5 - Profit!
Note - If these are your first view composers then you'll need to include them in application/start.php
// application/models/tweets.php
class Tweets {
public static function get($count = 5)
{
// get your tweets and return them
}
}
// application/views/widgets/tweets.blade.php
#foreach ($tweets)
{{-- do something with your tweets --}}
#endforeach
// application/views/layouts/default.blade.php
<section class="main">{{ isset($content) ? $content : '' }}</section>
<aside class="widget widget-tweets">{{ $tweets }}</aside>
// application/composers.php
View::composer('widgets.tweets', function($view)
{
$view->tweets = Tweets::get();
});
View::composer('layouts.default', function($view)
{
$view->nest('tweets', 'widgets.tweets');
});
// application/start.php (at the bottom)
include path('app').'composers.php';
// application/controllers/base.php
class Base_Controller extends Controller {
public $layout = 'layouts.default';
}
// application/controllers/home.php
class Home_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
$this->layout->nest('content', 'home.welcome');
}
}
View::share('key', 'value');
in you view use (Blade syntax)
{{$key}}
or (PHP syntax)
echo $key;