I want to change scenario in view page by jQuery.
There is a checkbox in my form, I want inputbox be required in checked checkbox.
my rule:
public function rules() {
return [
.
.
.
['pass' , 'required', 'on'=> 'checked']
]
}
my view page:
<?=$form->field($model, 'check')->checkbox()?>
<?=$form->field($model, 'Pass')->textInput(['maxlength' => 20])?>
Try to set up your rule like this:
public function rules() {
$checkBoxID = Html::getInputId($this, 'check');
return [
/* other rules */
['pass' , 'required',
'when' => function($model) {
return $model->check;
},
'whenClient'=> "function(attribute, value){
return $('#{$checkBoxID}').prop('checked');
}",
'on' => 'checked'
],
];
}
Read about conditional validation and whenClient validator property. Also consider naming you scenario more informative, e.g. by its purpose, like 'on' => 'sign-up', or 'on' => 'login'. Scenario is useful when you need certain rules to apply in particular cases, but you need to specify this scenario explicitly. Either when you instantiate a model
$model = new MyModel();
$model->scenario = 'sign-up';
and passing it into a view, or before doing any validation after $model->load($data)
Related
I have not so much practical experience with Laravel yet and I wondered what is the best way to deal with similar validation logic and where to put it.
Let's say I have an API resource Controller for Products with a store and an update method like so:
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:100',
'description' => 'nullable|string|max:1000',
'price' =>'required|decimal:0,2|lt:1000'
]);
return Product::create($request->all());
}
public function update(Request $request, Product $product)
{
$request->validate([
'name' => 'string|max:100',
'description' => 'nullable|string|max:1000',
'price' =>'decimal:0,2|lt:1000'
]);
return Product::update($request->all());
}
The only difference between the validation in store and update is that store adds the 'required' rule for 'name' and 'price'. My question is, if I can encapsulate both validations in one Form Request, or how can I avoid code duplication without adding unnecessary code?
With my understanding of Form Requests I would probably create two Form Request classes, StoreProductRequest and UpdateProductRequest, and maybe another helper class that defines the core validation rules. Then each Form request could call for example ProductHelper::getBaseValidationRules() and merge that with their extra requirements. Somehow I find that a bit overkill.
you can create a request for your validations and use them in your controllers
for example
php artisan make:request YOUR_REQUEST_NAME
then inside your request you can add your validations like this
public function rules()
{
return [
'name' => 'required|string|max:100',
'description' => 'nullable|string|max:1000',
'price' => 'required|decimal:0,2|lt:1000'
];
}
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
and in your method you can call it like this
public function update(YOUR_REQUEST_NAME $request, Product $product)
{
return Product::update($request->all());
}
for more information you can read this
https://laravel.com/docs/5.0/validation#form-request-validation
in case you want condition in the rules please check this video
https://www.youtube.com/watch?v=epMaClBOlw0&ab_channel=CodeWithDary
Okay based on the suggestions, I came up with the following solution:
I created a Form Request named ProductRequest and implemented the rules method as follows:
public function rules()
{
$rules = [
'name' => ['string', 'max:100'],
'description' => ['nullable', 'string', 'max:1000'],
'price' => ['decimal:0,2', 'lt:1000'],
];
// If the user wants to create a new Instance some fields are mandatory.
if ($this->method() === 'POST') {
$rules['name'][] = 'required';
$rules['price'][] = 'required';
}
return $rules;
}
This is fine for me. Although in a bigger project I probably would create two Form Requests, StoreProductRequest and UpdateProductRequest. They would share and update a base set of rules as I described in the question.
I have a issue, Please take a look below.
I have a edit user profile section where we update user account, i want to check if no image exists in table & user too not provided image then we have to validate the image & show an error. if there is already a image exists in table then no image validation suppose to check. Everything works good except image validation.
i am using from request for validation. below is my validation rules in request file.
public function rules()
{
return [
'user_type' =>'required',
'first_name' =>'required|max:100',
'last_name' =>'required|max:100',
'email' =>'required|email|max:100',
'image' =>'required|image',
'zip_code' =>'required|numeric|min:5',
];
}
I am using Laravel 5.2, Thanks in advance.
Finally i figure out the solution using help of #Amir. below is the complete solutions of this issue.
//add this in user model
public function notHavingImageInDb(){
return (empty($this->image))?true:false;
//return true;
}
//import the User model & Auth class in request class.
use App\User;
use Auth;
//add this in from request
public function rules()
{
$user = User::find(Auth::id());
$rules = [
'user_type' =>'required',
'first_name' =>'required|max:100',
'last_name' =>'required|max:100',
'email' =>'required|email|max:100',
'image' =>'image',
'zip_code' =>'required|numeric|min:5',
];
if ($user->notHavingImageInDb()){
$rules['image'] = 'required|image';
}
return $rules;
}
Now all done.
In the above code we check if the column has some value or not in validation, if yes then model function return true, this make the if condition active in request class, else, if condition remain inactive. works like charm.
Thanks
take a look at Conditionally Adding Rules:
so something like:
$v->sometimes(['image'], 'required|image', function($input) use ($user) {
return $user->notHavingImageInDb();
});
you can also use plain logic like:
public function rules()
{
$roles = [
'user_type' =>'required',
'first_name' =>'required|max:100',
'last_name' =>'required|max:100',
'email' =>'required|email|max:100',
'image' =>'image',
'zip_code' =>'required|numeric|min:5',
];
if ($user->notHavingImageInDb())
$rules['image'] = 'required|image';
return $rules
}
You can create another request class and name it UserUpdateRequest with artisan
like
php artisan make:request UserUpdateRequest
in this request class you just have to remove 'required' it will be like that
'image' => 'image'
and set UserUpdateResuest as parameter to update method like
public function update(UserUpdateRequest $userUpdateRequest, $id){
//
}
I have a model like so
class Person extends yii\db\ActiveRecord
{
public $desk_no;
public $mobl_no;
public $faxx_no;
}
I want to add a validation rule which would read something like this in English
One of the *_no attributes is required; I don't care which one.
How do I go about that in yii2?
A bit of a hack but you can use the required validator with conditional validation:
public function rules() {
$oneOfUs = ['desk_no', 'mobl_no', 'faxx_no'];
return [
... //Your other rules here
[$oneOfUs, 'required', 'when' => function($model, $attribute) use ($oneOfUs) {
foreach (array_diff($oneOfUs, [$attribute]) as $f) {
return !!($model->$f);
}
return false;
}]
];
}
The above code works since the condition function will return true if any attribute except the current one is set. This will in turn run therequired validator on the current attribute.
you could use such validation for example
['desk_no', 'required', 'when' => function($model) {
return is_null($model->mobl_no)&&is_null($model->faxx_no);
}, message => "One of the *_no attributes is required; I don't care which one." ],
but if this logic can change, i prefer custom validator
I am trying to validate an update user profile form, whereby the validation should check that the email doesn't exist already, but disregard if the users existing email remains.
However, this continues to return validation error message 'This email has already been taken'.
I'm really unsure where I'm going wrong. Otherwise, the update form works and updates perfectly.
HTML
{{ Form::text('email', Input::old('email', $user->email), array('id' => 'email', 'placeholder' => 'email', 'class' => 'form-control')) }}
Route
Route::post('users/edit/{user}', array('before' => 'admin', 'uses' => 'UserController#update'));
User Model
'email' => 'unique:users,email,{{{ $id }}}'
Your rule is written correctly in order to ignore a specific id, however, you'll need to update the value of {{{ $id }}} in your unique rule before attempting the validation.
I'm not necessarily a big fan of this method, but assuming your rules are a static attribute on the User object, you can create a static method that will hydrate and return the rules with the correct values.
class User extends Eloquent {
public static $rules = array(
'email' => 'unique:users,email,%1$s'
);
public static function getRules($id = 'NULL') {
$rules = self::$rules;
$rules['email'] = sprintf($rules['email'], $id);
return $rules;
}
}
You can accomplish this with the sometimes function of the validator
Something like:
$validator->sometimes('email', 'unique:users,email', function ($input) {
return $input->email == Input::get('email');
});
See http://laravel.com/docs/4.2/validation#conditionally-adding-rules for more info
I am trying to write a rule that validates if attribute_a or attribute_b is set;
one of the following attributes must be set : licitatii_publice or licitatiile_atribuite
The following code does not work;
<?php
namespace common\models;
use yii\base\Model;
class AbonamentValidare extends Model {
public $licitatii_publice;
public $licitatiile_atribuite;
public $zone;
public $judete;
public $tari;
public static $targetAttribute = [];
public function rules() {
return [
[['zone'], 'required'],
[['licitatii_publice', 'licitatiile_atribuite', 'tari', 'judete'], 'safe'],
['licitatii_publice', 'validate_tip_licitatie', 'targetAttribute' => ['licitatii_publice', 'licitatiile_atribuite']],
];
}
function validate_tip_licitatie($attribute, $param) {
print_r($attribute);
$this->addError($attribute, 'eroarea');
}
public function attributeLabels() {
return array(
'licitatii_publice' => 'lp',
'licitatiile_atribite' => 'la',
'tari' => 'tari',
'judete' => 'judete',
'zone' => 'zone',
);
}
public function save() {
return false;
}
}
?>
Just wanted to update this answer for Yii2 case.
In Yii2 the validators have a skipOnEmpty attribute which is by default set to true. This implies custom validators will not be called if the field is empty, which might not be the required behavior, especially in this case where either of one attribute is mandatory. In order to fix this issue we need to set the skipOnEmpty to false as shown below.
[['licitatii_publice, licitatiile_atribuite'], 'validate_tip_licitatie', 'skipOnEmpty'=> false],
Well what I have done in a case like this is to create the validator like this:
................
return [
[['zone'], 'required'],
[['licitatii_publice', 'licitatiile_atribuite', 'tari', 'judete'], 'safe'],
[['licitatii_publice, licitatiile_atribuite'], 'validate_tip_licitatie'],
];
............
function validate_tip_licitatie($attribute, $param) {
if(!$this->licitatii_publice && $this->licitatiile_atribuite)
$this->addError($attribute, 'eroarea');
}
In this way you show both fields with an error.
However I have done this in Yii1, but from what I read Yii2 should be the same. The logic is the same.
If you want to show error only for 1 attribute you can always just use
return [
[['zone'], 'required'],
[['licitatii_publice', 'licitatiile_atribuite', 'tari', 'judete'], 'safe'],
[['licitatii_publice'], 'validate_tip_licitatie'],
];
What you are trying to do is more fancy :), I get that. If you really want to use targetAttribute you might have to do it like this
https://github.com/yiisoft/yii2/blob/master/framework/validators/ExistValidator.php
Just build your own validator class.
Well. After reading about the exist validator i believe that is exactly what you need. It has examples on how to use it.