Yii2 doesn't work custom standalone validator(inline too) - php

In Yii2 I trying to write a standalone validator, but it's doesn't work. Below my code:
Standalone validator:
namespace app\components;
use yii\validators\Validator;
class UsernameValidator extends Validator {
public function validateAttribute($model, $attribute)
{
$this->addError($model, $attribute, 'Test.');
}
}
Model code:
namespace app\models;
use Yii;
use yii\base\Model;
use app\components\UsernameValidator;
class SignUpForm extends Model {
public $username;
public $password;
public $confirmPassword;
public function rules(){
return [
[['username', 'password', 'confirmPassword'], 'required'],
['password', 'compare', 'compareAttribute' => 'confirmPassword', 'operator' => '=='],
['confirmPassword', 'compare', 'compareAttribute' => 'password', 'operator' => '=='],
['username', UsernameValidator::className(), 'skipOnEmpty' => false],
];
}
}
skipOnempty, skipOnError don't fix that problem. Please, who experienced this? help me fix.

Related

How to validate nested relation in request class in laravel

I have two model that are related.
Models
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Address extends Model
{
use HasFactory;
protected $fillable = [
'city',
'post_code',
'street',
'country'
];
protected $hidden = [
'created_at',
'updated_at',
'addressable_id',
'addressable_type'
];
public function addressable(): MorphTo
{
return $this->morphTo();
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Club extends Model
{
use HasFactory;
protected $fillable = [
'name',
'email',
'phone',
'description',
'user_id'
];
protected $hidden = [
'created_at',
'updated_at'
];
public function address()
{
return $this->morphMany(Address::class, 'addressable');
}
}
For that models i've got Request class with validator rules.
Requests
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreAddressRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'city' => 'required|string|max:125',
'post_code' => 'required|string',
'street' => 'required|string',
'country' => 'required|string| max:125'
];
}
}
<?php
namespace App\Http\Requests;
use App\Traits\FailedValidation;
use Illuminate\Foundation\Http\FormRequest;
class StoreClubRequest extends FormRequest
{
use FailedValidation;
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|string|max:125',
'description' => 'required|string',
'email' => 'email:dns',
'phone' => 'string|max:125',
// 'address' => (new StoreAddressRequest)->rules(), ???
'images' => 'required|image'
];
}
}
Trying to create new club with address.
I'm sending request to API like that:
[
"name":"dfsdf",
"dsfsdf":"sdfsdf",
"address": {
"city": "name",
"post_code":"46-454"
}
]
Address may be added standalone or from other model that are related too.
Now only fields from club request are validated.
How to validate club and address request?

FormRequest message in laravel 8 not show messages

In my laravel 8 app I am trying to create two files CreateUserRequest and updateUserRequest to use personalized validations.
In the method rules I´m calling my model's variable $createRules but when I send my form, this don´t send, don´t show messages, error... Nothing.
I have attached CreateUserRequest, my model and controller's store method:
CreateUserRequest
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\User;
class CreateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return User::$createRules;
}
}
Model
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, Notifiable, HasRoles;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'email',
'password',
'surname',
'experience',
'skills',
'education',
'location',
'profesion',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public static $createRules = [
'name' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users',
'password' => 'required|min:4|max:10|confirmed',
];
public static $updateRules = [
'name' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users',
];
}
Controller's store method
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Models\Role;
use App\Models\User;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Str;
use Intervention\Image\ImageManagerStatic as Image;
use JamesDordoy\LaravelVueDatatable\Http\Resources\DataTableCollectionResource;
use Prettus\Validator\Exceptions\ValidatorException;
public function store(CreateUserRequest $request)
{
try {
$data = array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'password' => password_hash($request->get('password'), PASSWORD_BCRYPT),
'api_token' => Str::random(60),
);
$user = User::create($data);
$roles = $request->get('roles');
// We assign the Client's role
if(empty($roles)) {
$user->assignRole(2);
} else {
foreach($roles as $rol){
$user->assignRole($rol);
}
}
} catch (ValidatorException $e) {
Flash::error($e->getMessage());
}
Flash::success('This is a message!');
return redirect()->route('users.index');
}
In the blade I have #include('flash::message') for getting flashed messages, and this view it´s included in other #include('admin.layouts.alertMessage').
I don´t know what I am doing wrong.
Thanks for help me and sorry for my bad English.
UPDATED
my problem i think that i don´t arrive to my controller. I´m doin echo in my function with one exit and load all my page without message echo
attach my form and my routes:
<form action="{{ route('users.store') }}" method="POST">
#csrf
#include('admin.users.fields')
</form>
routes
/** USERS BLOQ ROUTES */
Route::resource('users', UserController::class);
TBH, i didn't know that you can get validation logic from their Model. My advice is just copy paste your validation logic to the request. Since Request is only for Validation purpose ( CMIIW )
CreateRequest
public function rules()
{
return [
'name' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users',
'password' => 'required|min:4|max:10|confirmed',
];
}
UpdateRequest
public function rules()
{
return [
'name' => 'required|string|max:191',
'email' => 'required|string|email|max:191|unique:users',
];
}
UPDATE
Just combine it to one Requests, give RequiredIf for password request, in case your update route contains update
use Illuminate\Validation\Rule;
...
public function rules()
{
return [
'password' => Rule::requiredIf(str_contains(url()->current(), 'update')) . '|min:4|max:10|confirmed',
]
}

Yii can't save data - unknown method save()

Hello I am new to Yii framework and I am trying to save data to database. It won't work I don't know where is the problem.
Controller:
namespace app\controllers;
use app\models\Client;
use Yii;
use yii\web\Controller;
class ClientController extends Controller {
/**
* Displays Client_Register.
*
* #return string
*/
public function actionAdd() {
$model = new Client();
if ($model->load(Yii::$app->request->post())) {
if ($model->save()) {
return $this->refresh();
}
}
return $this->render('add', ['model' => $model,]);
}
}
View:
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'name')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'lastname') ?>
<?= $form->field($model, 'birthday') ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-primary', 'name' => 'add-button']) ?>
</div>
<?php ActiveForm::end(); ?>
Model:
namespace app\models;
use yii\base\Model;
/**
* Client is the model behind the client form.
*/
class Client extends Model {
public $id;
public $name;
public $lastname;
public $birthday;
public static function tableName() {
return 'clients';
}
/**
* #return array the validation rules.
*/
public function rules() {
return [
[['name', 'lastname', 'birthday',], 'required'],
];
}
public function attributeLabels() {
return [
'id' => 'Id',
'name' => 'Name',
'lastname' => 'Last Name',
];
}
}
I have already created the database with migrations. But I don't why does this error happen. Should I include some save method in model or how can I solve this issue. I looked at other examples too. They are identical as my code. Do you have any idea where is the problem?
Your Client class extends Model, which does not support saving data in database, thus save() method is not defined. If you want to work with database record, your model should extend ActiveRecord:
class Client extends ActiveRecord {
public static function tableName() {
return 'clients';
}
public function rules() {
return [
[['name', 'lastname', 'birthday'], 'required'],
];
}
public function attributeLabels() {
return [
'id' => 'Id',
'name' => 'Name',
'lastname' => 'Last Name',
];
}
}

Can't draw related model data, get invalid foreach argument

Attempting to recast a Yii 1.1 application in Yii2 advanced template, and stumbling on the display of related model data in a view.
Have searched for answers regarding this but as far as I can tell, I'm following Yii2 protocols for gathering the data, and other extended examples I've seen have all been related to e.g. GridView
Relevant Model code:
frontend/config/main.php
return [
...
'components' => [
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules'=> [
'home' => 'page/home',
'<alias:about>' => 'page/page',
'page/<alias>' => 'page/page'
],
],
common/models/Page.php
namespace common\models;
use Yii;
/**
* This is the model class for table "page".
...
* The followings are the available model relations:
* #property Content[] $contents
*/
class Page extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'page';
}
...
public function getContents()
{
return $this->hasMany(Content::className(), ['pageId' => 'id']);
}
...
models/common/Content.php
namespace common\models;
use Yii;
/**
* This is the model class for table "content".
...
* The followings are the available model relations:
* #property Page $page
*/
class Content extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'content';
}
...
public function rules()
{
return [
[['name', 'slug', 'pageId', 'content'], 'required'],
[['pageId'], 'integer'],
[['content'], 'string'],
[['name', 'slug'], 'string', 'max' => 255],
[['pageId'], 'exist', 'skipOnError' => true, 'targetClass' => Page::className(), 'targetAttribute' => ['pageId' => 'id']],
];
}
...
public function getPage()
{
return $this->hasOne(Page::className(), ['id' => 'pageId']);
}
frontend/controllers/PageController.php
class PageController extends Controller
{
...
public function actionPage($alias)
{
$model=$this->loadContent($alias);
if ($alias == "home")
{
$news=$this->loadRecentNews();
}
$this->render('default',[
'model'=>$model,
'news'=>$news
]);
}
...
public function loadContent($page)
{
$pageModel = Page::find()->where(['slug'=>$page])->with('contents')->all();
if($pageModel===null)
throw new CHttpException(404,'The requested Page could not be found.');
return $pageModel;
}
public function loadRecentNews()
{
$newsModel = News::find()->orderBy('create_time DESC')->limit(2)->all();
return $newsModel;
}
frontend/views/page/default.php (portion that fails)
<?php foreach ($model->contents as $content) { ?>
<h3><?php echo $content->name; ?></h3>
<?php echo $content->content; ?>
<?php } ?>

Issue with an ActiveRecord Class rules with yii2

I just started learning Yii 2 yesterday and I have a problem that I don't understand. It's working well with this code, but if I uncomment the 2 lines
I have this error:
[...]a rule must specify both attribute names and validator type.
<?php
namespace app\models\customer;
use yii\db\ActiveRecord;
class CustomerRecord extends ActiveRecord
{
public static function tableName()
{
return 'customer';
}
public function rules()
{
return [
//['name' => 'string'],
//['name' => 'required'],
['birth_date', 'date', 'format' => 'd-m-Y'] ,
['birth_date', 'required'] ,
['notes', 'safe'] ,
];
}
}
I made some researches before posting here.
You list single attribute or array of attributes for the rule, then name of validator, then validator parameters so it should be:
['name', 'string'],
['name', 'required'],

Categories