Removing the validation when Redux-Form Field is disabled - redux-form

I am using Redux form v6.5.0 and having a requirement of
Removing the validation for any Field when the disabled props is passed as true.
I wrote a custom logic to disable the validation inside render() of custom field component, but looks like updateSyncErrors() is not getting called on the form even after updating the values manually. Because of this, syncErrors object is persisting the field validation error.
if (field.disabled) {
field.meta.invalid = false;
field.meta.error = undefined;
field.meta.valid = true;
}
Can we have some straight forward - simple & better approach which tackles this requirement and fixes this issue?

I faced the same situation, I wanted to disable or enable fields based on API response and according to that, I had to enable and disable validations also. I was able to do that in the below way
Input Field (select field)
<Field
formItemLayout={layout}
name="configType"
validate={
!(
response &&
response.data &&
response.data.isEnabled
)
? [required]
: undefined
}
component={ASelectField}
placeholder="configType"
disabled={
response &&
response.data &&
response.data.isEnabled
}
onChange={(e) => changeconfigSelectFields(e)}
onBlur={(e) => {
e.preventDefault();
}}
>
{Object.keys(fields.data).map(
(obj) => {
return <Option key={obj}>{obj}</Option>;
}
)}
</Field>
top of the class I Add below method
const required = value => value ? undefined : 'Required'

You can disable the validation of a specific field by passing disableValidation array with field names in your form configuration object. Then you can check if this array contains the field name and if it doesn't provide the field with validation functions.
I suppose an example would demonstrate this best: https://www.webpackbin.com/bins/-Kf7WYdGZtEypx080L99

Related

How does kendo form validation custom required work?

I would like to add a required field to a Kendo form.
This works with:
validation: { required: true },
If I now want to set required myself in a function, this no longer works.
validation: {
required: function(){
return true;
}
},
I have created an example: https://dojo.telerik.com/epEzewIt/5
If no entry is made in the "MultiSelect" field, the required variant as a function is no longer displayed: "MultiSelect is required" when it is empty.
How can I store required with a function?
If you want to execute some code one time you can use an anonymous function like:
validation: {
required: (function(){
return true; // run your rules here
})()
But, as documentation suggests, those (required, max/min, pattern, etc.) are field attributes that map to HTML rules. So you can't pass a function.
Why not use the kendoValidator on your submit action. This can be used to validate all the fields and set custom messages.
Here are more details on how to use Kendo Validator to create custom validations:
https://docs.telerik.com/kendo-ui/controls/editors/validator/rules
There are a lot of examples there.
if you want to achieve this and to have a event handler, you can use the following code:
validation: {
validated: function (input) {
//here you can place your code
//you need to return true of false
//e.g
if(true){ return true; }
return false;
}
}
I had the same use case, as I was building it dynamically.
As you can see I was able to validate it:

Backpack V4 modifying field before store

In 3.6 version of backpack I can change an attribute value before storing it.
I have this code
If ($request->description == "") {
$request->description="User has not entered any description";
}
$redirect_location = parent::storeCrud($request);
What can I do to get the same in V4? I'm reading this guide but I can't make it to work.
This is what I'm trying in V4
public function store(PedidoRequest $request)
{
Log::debug('testing...');
If ($request->description == "") {
$request->description="User has not entered any description";
}
$redirect_location = $this->traitStore();
return $redirect_location;
}
The Request object in Laravel, Illuminate\Http\Request, doesn't have the ability to set the inputs via the properties like that, no __set method ($request->description = '...' does not set an input named description). You would have to merge the inputs into the request or use the array syntax to do that:
$request->merge(['description' => '...']);
// or
$request['description'] = '...';
But since backpack seems to have abstracted things apparently you aren't controlling anything in your controller methods you could try this:
$this->crud->request->request->add(['description'=> '...']);
Potentially:
$this->request->merge(['description' => '...']);
That would be assuming some trait the Controller uses is using the Fields trait.

Customized validation rule on laravel form request validation

I do have a registration form in my laravel 5.4 application and laravel form request validation is used for server side validation. Some fields in this form are populated dynamically using calculations in javascript which need to be validated against user inputs.
The user input fields in the form are 'quantity', 'rate' and 'discount'.
The populated fields are 'total' and 'bill_amount'.
What i need to validate are :
Check 'total' equal to 'quantity' * 'rate'.
Check 'bill_amount' equal to 'total' - 'rate'
I would prefer laravel form request validation methods for this validation. I have tried to use methods like After Hooks and conditionally adding rule etc. and failed.
In simple words the requirement is : check if a field is equal to product of other two fields, and invalidate if not equal and validate if equal.(using form request validation.)
Thanks in advance!
After a long time I was able to find this solution.
Form request After Hooks can be used to achieve the result:
[I was unable to find this logic before]
public function withValidator($validator)
{
$quanty = $this->request->get("quantity");
$rate = $this->request->get("rate");
$billAmount = $this->request->get("bill_amount");
$validator->after(function ($validator) {
if(($quanty * $rate) != $billAmount) {
$validator->errors()->add('bill_amount', 'Something went wrong with this field!');
}
});
}

In a user's Edit Info form, how do I include the name of the field(s) and user's input value(s) in a response from the model

On a Yii2 project, in a user's Edit Info form (inside a modal):
I'm currently figuring out which fields were changed using the jQuery .change() method, and I'm grabbing their value with jQuery's .val() method.
However, I want to do less with JavaScript and do more with Yii's framework.
I can see in the Yii debugger (after clicking into the AJAX POST request) that Yii is smart enough to know which fields were changed -- it's showing SQL queries that only UPDATE the fields that were changed.
What do I need to change in the controller of this action to have Yii include the name of the field changed -- including it's value -- in the AJAX response? (since my goal is to update the main view with the new values)
public function actionUpdateStudentInfo($id)
{
$model = \app\models\StudentSupportStudentInfo::findOne($id);
if ($model === null) {
throw new NotFoundHttpException('The requested page does not exist.');
}
$model->scenario = true ? "update-email" : "update-studentid";
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->renderAjax('_student_support_alert_success');
}
return $this->renderAjax("_edit_student_info",[
"model" => $model,
]);
}
I'm currently returning a static success view.
You can use $model->dirtyAttributes just after load the data to get a $attrib => $value pair array.
http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#getDirtyAttributes()-detail (this docs says:)
Returns the attribute values that have been modified since they are loaded or saved most recently.
The comparison of new and old values is made for identical values using ===.
public array getDirtyAttributes ( $names = null )
(sorry for formatting, sent by mobile)

Codeigniter Grocery Crud update field?

$crud->set_rules('user_password', 'Password', 'trim|required|matches[konfirmpass]');
$crud->set_rules('konfirmpass', 'Konfirmasi Password', 'trim|required');
$crud->callback_edit_field('user_password',array($this,'_user_edit'));
$crud->callback_add_field('user_password',array($this,'_user_edit'));
callback function:
function _user_edit(){
return '<input type="password" name="user_password"/> Confirmation password* : <input type="password" name="konfirmpass"/>';
}
My question is how to update if only "password" not blank?
I've installed CI 2.0.3 and GC 1.1.4 to test because at a glance your code looked fine. As it turns out, it is and your code works. I modified the out of the box employees_management method in the examples controller with GC. Added a user_password column to the database and added your code to the controller.
The code both ensures that the password fields match and that they're not empty when submit.
Empty results in "The Password field is required"
Mismatched results in "The Password field does not match the konfirmpass field."
Perhaps if this isn't working for you, you should post your entire method instead of just the rules and callbacks so we can see if there are any other problems.
Edit
To edit the field, only if the password has been edited you need to add
$crud->callback_before_update( array( $this,'update_password' ) );
function update_password( $post ) {
if( empty( $post['user_password'] ) ) {
unset($post['user_password'], $post['konfirmpass']);
}
return $post;
}
This however may mean that you need to remove the validation for empty password depending on which order the callbacks run (if they're before or after the form validation runs). If they run before the form validation, you'll need to also need to run a call to callback_before_insert() and add your validation rules within the two callbacks. Insert obviously will need the required rule, and update won't.
Edit 2, Clarification of Edit 1
Having looked into it, the validation runs before the callbacks, so you can't set validation rules in the callback functions. To achieve this you'll need to use a function called getState() which allows you to add logic based on the action being performed by the CRUD.
In this case, we only want to make the password field required when we are adding a row, and not required when updating.
So in addition to the above callback update_password(), you will need to wrap your form validation rules in a state check.
if( $crud->getState() == 'insert_validation' ) {
$crud->set_rules('user_password', 'Password', 'trim|required|matches[konfirmpass]');
$crud->set_rules('konfirmpass', 'Konfirmasi Password', 'trim|required');
}
This will add the validation options if the CRUD is inserting.

Resources