I've been getting weird behaviour since I added the active checkbox to this form, the Submit button can only be clicked outside of the the text, and the checked status of the active field is not registering when the form is submitted, it always shows as true. I checked to see if active was a reserved word because the behaviour is so funky, but to no avail. Am I being a noob?
<div class="add-form">
<?= $this->Form->create($member) ?>
<legend><?= __('Edit Member') ?></legend>
<?php
echo $this->Form->control('membership_no');
echo $this->Form->control('password');
echo $this->Form->control('email');
echo $this->Form->control('company_id', ['options' => $companies]);
echo $this->Form->control('terms_agreed');
echo $this->Form->control('change_password');
echo $this->Form->control('active');
?>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
in case it's of any import here is the migration where I added the active column
<?php
use Migrations\AbstractMigration;
class AddActiveToMembers extends AbstractMigration
{
public function change()
{
$table = $this->table('members');
$table->addColumn('active', 'boolean', [
'default' => true,
'null' => false,
]);
$table->update();
}
}
Try with other field name once.
'default'=>true may be returning value '1' ...Remove and check the output.
That is
<?php
use Migrations\AbstractMigration;
class AddActiveToMembers extends AbstractMigration
{
public function change()
{
$table = $this->table('members');
$table->addColumn('active_col', 'boolean', [
'default' => true,
'null' => false,
]);
$table->update();
}
}
OR
<?php
use Migrations\AbstractMigration;
class AddActiveToMembers extends AbstractMigration
{
public function change()
{
$table = $this->table('members');
$table->addColumn('active', 'boolean', [
'null' => false,
]);
$table->update();
}
}
Related
When I click on the Signup button I receive the following error: Array to string conversion.
I think that the error occurs when I call fileInput() method but i don't know how to solve it.
This is the partial code of the view
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($modelUpload, 'imageFile')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
While this is the code for the controller:
<?php
class SiteController extends Controller {
/**
* Signs user up.
*
* #return mixed
*/
public function actionSignup() {
$model = new SignupForm();
$modelUpload = new UploadForm();
if ($model->load(Yii::$app->request->post()) && $modelUpload->load(Yii::$app->request->post())) {
$modelUpload->imageFile = UploadedFile::getInstances($modelUpload, 'imageFile');
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user) && $modelUpload->upload()) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
'modelUpload' => $modelUpload,
]);
}
}
This is the code of the model. It's the same of the official documentation.
<?php
class UploadForm extends Model {
/**
* #var UploadedFile
*/
public $imageFile;
public function rules() {
return [
[['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
];
}
public function upload() {
if ($this->validate()) {
$this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
return true;
} else {
return false;
}
}
}
?>
Errors:
Instant Solution
Change your line inside the actionSignup() from below
UploadedFile::getInstances($modelUpload, 'imageFile');
to
UploadedFile::getInstance($modelUpload, 'imageFile');
Reason
It's only a single file you are uploading not multiple files so getInstances() should be getInstance()
About
getInstance : Returns an uploaded file for the given model
attribute. The file should be uploaded using
[[\yii\widgets\ActiveField::fileInput()]]
getInstances: Returns all uploaded files for the given model
attribute.
if you want to upload multiple files - having'maxFiles' > 1 in your model's FileValidator rules - change your attribute name from:
<?= $form->field($modelUpload, 'imageFile')->fileInput() ?>
to
<?= $form->field($modelUpload, 'imageFile[]')->fileInput() ?>
read this:
https://www.yiiframework.com/doc/guide/2.0/en/input-file-upload#uploading-multiple-files
I'm reviewing a basic contact form which is not associated with any model. I would like some advice on the best way to leverage Cake's automatic view rendering of field errors for this situation.
Controller
Performs validation through a custom Validator.
public function index()
{
if ($this->request->is('post')) {
// Validate the form
$validator = new EnquiryValidator();
$data = $this->request->data();
$errors = $validator->errors($data);
if (empty($errors)) {
// Send email, etc.
// ...
// Refresh page on success
}
// Show error
$this->Flash->error('Unable to send email');
}
}
View
<?= $this->Form->create(); ?>
<?= $this->Form->input('name', [
'autofocus' => 'autofocus',
'placeholder' => 'Your name',
'required'
]);
?>
<?= $this->Form->input('email', [
'placeholder' => 'Your email address',
'required'
]);
?>
<?= $this->Form->input('subject', [
'placeholder' => 'What would you like to discuss?',
'required'
]);
?>
<?= $this->Form->input('message', [
'label' => 'Query',
'placeholder' => 'How can we help?',
'cols' => '30',
'rows' => '10',
'required'
]);
?>
<div class="text-right">
<?= $this->Form->button('Send'); ?>
</div>
<?= $this->Form->end(); ?>
Currently the form will not show any errors next to the input fields. I assume it's because there is no entity associated with the form or something like that, but I'm not sure.
What is the best solution? Can the validation be performed in a better way to automatically provide field errors in the view?
Modelless forms
Use a modelless form. It can be used to validate data and perform actions, similar to tables and entities, and the form helper supports it just like entities, ie, you simply pass the modelless form instance to the FormHelper::create() call.
Here's the example from the docs, modified a little to match your case:
src/Form/EnquiryForm.php
namespace App\Form;
use App\...\EnquiryValidator;
use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;
class EnquiryForm extends Form
{
protected function _buildSchema(Schema $schema)
{
return $schema
->addField('name', 'string')
->addField('email', ['type' => 'string'])
->addField('subject', ['type' => 'string'])
->addField('message', ['type' => 'text']);
}
protected function _buildValidator(Validator $validator)
{
return new EnquiryValidator();
}
protected function _execute(array $data)
{
// Send email, etc.
return true;
}
}
in your controller
use App\Form\EnquiryForm;
// ...
public function index()
{
$enquiry = new EnquiryForm();
if ($this->request->is('post')) {
if ($enquiry->execute($this->request->data)) {
$this->Flash->success('Everything is fine.');
// ...
} else {
$this->Flash->error('Unable to send email.');
}
}
$this->set('enquiry', $enquiry);
}
in your view template
<?= $this->Form->create($enquiry); ?>
See also
Cookbook > Modelless Forms
I would like to create a yii2 model without a database. Instead, the data is generated dynamically and not stored, just displayed to the user as a json. Basically, I would just like a get a simple, basic example of a non-database Model working but I can't find any documentation on it.
So how would I write a model without a database? I have extended \yii\base\Model but I get the following error message:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<name>PHP Fatal Error</name>
<message>Call to undefined method my\app\models\Test::find()</message>
<code>1</code>
<type>yii\base\ErrorException</type>
<file>/my/app/vendor/yiisoft/yii2/rest/IndexAction.php</file>
<line>61</line>
<stack-trace>
<item>#0 [internal function]: yii\base\ErrorHandler->handleFatalError()</item>
<item>#1 {main}</item>
</stack-trace>
</response>
To implement find(), I must return a database query object.
My Model is completely blank, I'm just looking for a simple example to understand the principal.
<?php
namespace my\app\models;
class Test extends \yii\base\Model{
}
This is a Model from one of my projects. This Model is not connected with any database.
<?php
/**
* Created by PhpStorm.
* User: Abhimanyu
* Date: 18-02-2015
* Time: 22:07
*/
namespace backend\models;
use yii\base\Model;
class BasicSettingForm extends Model
{
public $appName;
public $appBackendTheme;
public $appFrontendTheme;
public $cacheClass;
public $appTour;
public function rules()
{
return [
// Application Name
['appName', 'required'],
['appName', 'string', 'max' => 150],
// Application Backend Theme
['appBackendTheme', 'required'],
// Application Frontend Theme
['appFrontendTheme', 'required'],
// Cache Class
['cacheClass', 'required'],
['cacheClass', 'string', 'max' => 128],
// Application Tour
['appTour', 'boolean']
];
}
public function attributeLabels()
{
return [
'appName' => 'Application Name',
'appFrontendTheme' => 'Frontend Theme',
'appBackendTheme' => 'Backend Theme',
'cacheClass' => 'Cache Class',
'appTour' => 'Show introduction tour for new users'
];
}
}
Use this Model like any other.
e.g. view.php:
<?php
/**
* Created by PhpStorm.
* User: Abhimanyu
* Date: 18-02-2015
* Time: 16:47
*/
use abhimanyu\installer\helpers\enums\Configuration as Enum;
use yii\caching\DbCache;
use yii\caching\FileCache;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/** #var $this \yii\web\View */
/** #var $model \backend\models\BasicSettingForm */
/** #var $themes */
$this->title = 'Basic Settings - ' . Yii::$app->name;
?>
<div class="panel panel-default">
<div class="panel-heading">Basic Settings</div>
<div class="panel-body">
<?= $this->render('/alert') ?>
<?php $form = ActiveForm::begin([
'id' => 'basic-setting-form',
'enableAjaxValidation' => FALSE,
]); ?>
<h4>Application Settings</h4>
<div class="form-group">
<?= $form->field($model, 'appName')->textInput([
'value' => Yii::$app->config->get(
Enum::APP_NAME, 'Starter Kit'),
'autofocus' => TRUE,
'autocomplete' => 'off'
])
?>
</div>
<hr/>
<h4>Theme Settings</h4>
<div class="form-group">
<?= $form->field($model, 'appBackendTheme')->dropDownList($themes, [
'class' => 'form-control',
'options' => [
Yii::$app->config->get(Enum::APP_BACKEND_THEME, 'yeti') => ['selected ' => TRUE]
]
]) ?>
</div>
<div class="form-group">
<?= $form->field($model, 'appFrontendTheme')->dropDownList($themes, [
'class' => 'form-control',
'options' => [
Yii::$app->config->get(Enum::APP_FRONTEND_THEME, 'readable') => ['selected ' => TRUE]
]
]) ?>
</div>
<hr/>
<h4>Cache Setting</h4>
<div class="form-group">
<?= $form->field($model, 'cacheClass')->dropDownList(
[
FileCache::className() => 'File Cache',
DbCache::className() => 'Db Cache'
],
[
'class' => 'form-control',
'options' => [
Yii::$app->config->get(Enum::CACHE_CLASS, FileCache::className()) => ['selected ' => TRUE]
]
]) ?>
</div>
<hr/>
<h4>Introduction Tour</h4>
<div class="form-group">
<div class="checkbox">
<?= $form->field($model, 'appTour')->checkbox() ?>
</div>
</div>
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
<?php $form::end(); ?>
</div>
The reason for using a model is to perform some kind of logic on data that you are getting from somewhere. A model can be used to perform data validation, return properties of the model and their labels, and allows for massive assignment. If you don't need these features for your data model, then don't use a model!
If you are not requiring data validation (i.e. you are not changing any data via forms or other external source), and you are not requiring access to behaviors or events, then you probably need to just use yii\base\Object. This will give you access to getters and setters for properties of the object, which seems to be all you need.
So your class ends up looking like this. I've included getting data from another model, in case that's what you want to do;
<?php
namespace my\app\models;
use \path\to\some\other\model\to\use\OtherModels;
class Test extends \yii\base\Object{
public function getProperty1(){
return "Whatever you want property1 to be";
}
public function getProperty2(){
return "Whatever you want property2 to be";
}
public function getOtherModels(){
return OtherModels::findAll();
}
}
You would then simply use it like this;
$test = new Test;
echo $test->property1;
foreach ($test->otherModels as $otherModel){
\\Do something
}
The function you have tried to use, find(),is only relevant to a database and so won't be available to your class if you've extended yii\base\Model, yii\base\Component or yii\base\Object, unless you want to define such a function yourself.
A lightweight way to create models without database backend is to use a DynamicModel:
DynamicModel is a model class primarily used to support ad hoc data validation.
Just write in your controller:
$model = new DynamicModel(compact('name', 'email'));
$model->addRule(['name', 'email'], 'string', ['max' => 128])
->addRule('email', 'email')
->validate();
and then pass $model to your view.
A full example can be found in http://www.yiiframework.com/wiki/759/create-form-with-dynamicmodel/.
This is perfect for user input for calling APIs, creating forms on the fly, etc.
As has been pointed out in the comments and other answers, your model needs to extend \yii\db\BaseActiveRecord. That said you can store your json as a nosql database such as MongoDb or in a key-value cache such as Redis instead. Both have Yii implementions: \yii\mongodb\ActiveRecord and \yii\redis\ActiveRecord
I am trying to insert record using Cakephp.My model name is something like User.php.
And My working controller name is SignupsController.I want to insert record using this two but I cant.I am give my some codes below :
View :
<?php echo $this->Form->create('Signups',array('action' => 'registration'));?>
<div class="row-fluid">
<div class="span5">
<label class="">*First Name</label>
<?php echo $this->Form->input('first_name', array('type' => 'text','label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
</div>
<div class="span5">
<label class="">*Last Name</label>
<?php echo $this->Form->input('last_name', array('type' => 'text', 'label' => false, 'class' => 'input-xlarge validate[required]', 'div' => false)); ?>
</div>
</div>
<?php echo $this->Form->end(); ?>
My controller code is given below :
class SignupsController extends AppController {
var $name = 'Signups';
var $uses=array("User");
public function registration()
{
$this->layout="reserved";
$this->Club->create();
if (isset($_POST['registration'])) {
echo "This is";
echo "<pre>";print_r($this->request->data);echo"</pre>";
$this->User->save($this->request->data);
//$this->Session->setFlash(__('Promoter registration has been done successfully'));
//$this->redirect('registration');
//$this->redirect(array('action' => 'registration'));
}
}
}
My model name is different which's name is User.php
I want to insert the record using this above code.Any idea how to insert?
you can do this by loading the users model in current controller just write the following line
$this->loadModel('Name of the Model').
then
$this->nameofmodel->save()
As you are unable to understand see this
Controller::loadModel(string $modelClass, mixed $id)¶
The loadModel() function comes handy when you need to use a model which is not the controller’s default model or its associated model:
$this->loadModel('Article');
$recentArticles = $this->Article->find(
'all',
array('limit' => 5, 'order' => 'Article.created DESC')
);
$this->loadModel('User', 2);
$user = $this->User->read();
Above pasted code is taken from CookBook of Cakephp, if you still do not understand just read it it has complete detailed explanation you can also see this to understand
you can use it with $uses variable in SignupController
class SingupController extends AppController
{
public $uses = array('User');
//rest of stuff
}
Or, if you want, you can load it on-demand inside a method:
$this->loadModel('User'); //now model is loaded inside controller and used via $this->User
EDIT: Your data array has to include the name of the model you're saving. So, replace:
$this->Form->create('Signups',array('action' => 'registration')
with:
$this->Form->create('User',array('url' => array('controller' => 'signups', 'action' => 'registration'));
this is action url: http://localhost/carsdirectory/users/dashboard.
dashboad.ctp (i have select filed and in this select field i m fetching data from that filed car_type and table name car_types)
<?php echo $this->Form->create('User', array('type' => 'file', 'action' => 'dashboard')); ?>
<label class="ls-details-label">Type</label>
<div class="ls-details-box">
<?php
foreach ($car_types as $car_type)
{
$car_type_new[$car_type['Car_type']['id']]=
$car_type['Car_type']['car_type'];
}
echo $this->Form->input('car_type',
array( 'label'=>false,
'options'=>$car_type_new,
'empty'=>' Select ',
'class'=>'styledselect_form_1'));
?>
</div>
<?php echo $this->Form->end(array('label' => 'Submit',
'name' => 'Submit',
'div' => array('class' => 'ls-submit')));?>
users_controller.php (controller)
class UsersController extends AppController{
var $name = "Users";
public function dashboard(){
$this->loadModel('Car_type'); // your Model name => Car_type
$this->set('car_types', $this->Car_type->find('all'));
if(!empty($this->data))
{
$this->loadModel('Car');
if($this->Car->save($this->data))
{
$this->Session->setFlash('Detail has Been Saved');
$this->redirect(array('action'=>'dashboard'));
}
else
{
$this->Session->setFlash('Detail could not save');
}
}
}
car.php (model)
<?php
class Car extends appModel{
var $name = "Car";
}
?>
i want to inset data car_type_id field in (table name cars) , but i m not able to do it
so plz help me
thanks in advance, vikas tyagi
You may try this:
echo $this->Form->input('Car.car_type_id', array(...));