I have a problem with a sfWidgetFormInputCheckbox. It do not save false in the database when the checkbox is unchecked.
In my schame my field is a boolean, and i don't have bug in the value displayed.
Set the default value like this : $this->setDefault('status', false); don't work either.
My widget :
$this->widgetSchema['SUSPENSION_TEMP'] = new sfWidgetFormInputCheckbox();
$this->validatorSchema['SUSPENSION_TEMP'] = new sfValidatorBoolean(array('required' => false));
The default :
$this->setDefault('SUSPENSION_TEMP', false);
Any ideas ?
Edit:
It save 1 when the checkbox is checked. And do not change the database value when you unchecked and save. So once you checked once, value is always 1 in database (true for my code).
Edit2:
I have two clues to add. If the field is an integer in the schema.xml, both values work, bu we got a poblem on value displayed (checkbox checked qih the 0 value). Here, he field is a boolean.
The setter receive two kinds of value, 'on' when checkbox is checked and true when it's not. I tried to set default value to false in form, and to overide he setter. Both don't work.
I mean the setter work, but for an unknown reason the basic seer is called right after with a wrong value.
Edit3:
Ok, i understand part of the problem. The setter is not called, when the checkbox is unchecked. I thought it was because, m code displayed a var_dump(). It was because later i copy the old version of this object in archive purpose.
But symfony should detect that the field has been rendered and it doesn't. I don't know why. I know why in html, but obviously symfony should be able to tell himself "hey, i add a checkbox here".
Is there an elegant way o do this, or should i checked it by hand ?
The problem came from my form handling. My legacy code, did no render all fields (ex : some stamp field, updated by a behavior). It was using fromArray, then save. This update only the fiels rendered.
The traditionnal symfony form, give null to all non-rendered fiels in order to chcuk for required validation.
So you need to check by yourself and set to 0, when you are using a fromArray then save. Still, i find the form->save really dumb, because you need to send useless(in this case) or confidentials data to your client in order to use it.
Related
I am trying to make a Text field visible when a boolean is toggled true WITHOUT having to install a dependency. Does anyone know how to implement this? Is there a way to watch the request for changes to a particular field and conditionally render based on that?
Boolean::make('Blocked', 'isBlocked')
->hideFromIndex(),
Textarea::make('Reason', 'blockedReason')
->withMeta([
'extraAttributes' => [
'placeholder' => 'Make it less than 255 characters',
]
])
->rules('required', 'max:255')
->rows(3)
->hideFromIndex(),
Bad solution
One solution you could use is to update the resource with the boolean to true or false. Once the page is reloaded depending on the boolean the text field is displayed.
This, however, would invalidate Nova's dynamism, I strongly advise against it.
Probably a bad solution
Another solution could be to create an event emitter in the change of the boolean value. Once the event is fired change the readonly value of the text field.
Probably the best choice
You haven't specified the reason why you can't use a package, but if you need to use the nova-dependecy-container package.
I currently have a validation rule that prevents user from making changes to a record when its status is completed. Users are only allowed to make changes if the status is either draft or registered.
AND(
TEXT(Current_Status__c) <> "Draft",
TEXT(Current_Status__c) <> "Registered"
)
There is a new requirement to allow user to update only a specific picklist value field even if the record status is completed. If i remove the validation rule, user will be able to change any fields on the page layout which won't work.
Object setting for the profile is read, create, edit. This object is a child object to Opportunity, OWD is controlled by parent.
Any recommendation on how to solve this issue ?
Thanks in advance.
We can rewrite your rule as ISPICKVAL(Current_Status__c, 'Completed') for example, looks bit cleaner. Your call though, you can keep as is.
So what you'd need is something like ISPICKVAL(Current_Status__c, 'Completed') && !ISCHANGED(Some_Picklist__c). It should let the edit through if you're modifying that picklist.
The problem is it won't check if that's the only change. Usercan cheat, modify 10 fields and they'll "piggyback" and save OK as long as one of them is that picklist.
It's pain to write validation like ISPICKVAL(Current_Status__c, 'Completed') && !ISCHANGED(Some_Picklist__c) && (ISCHANGED(Field1__c) || ISCHANGED(Field2__c) || ISCHANGED(Field3__c)). You'd have to add all editable fields to it, change it every time you make new one. And eventually you'll hit length limits.
I know 3 options for this if it's a concern for you:
Ask a developer to rewrite your logic to Apex trigger, it could then go dynamic through all fields (using "describe" calls to learn field names or stuff like getPopulatedFieldsAsMap.
Another trick is to allow editing completed records only through a quick action, not normal edit page. In that action you could set some hidden checkbox in the field prepopulating step and your validation would let the save through only if that checkbox is set. But then you need to deactivate it somehow anyway or the bypass will get permamently enabled.
If you don't have too many record types on the object a common trick is to change the record type on completion (workflow, process builder etc). And have another page layout with all fields locked down except that special picklist. It works good enough for UI changes but won't protect if you have code and integrations writing to the object too.
I'm currently building a Laravel 5.7 app where I have multiple boolean columns that indicate if some facilities are available for a building (model), eg, toilet yes/no. This works fine, but I was wondering what happens when I add more of these boolean columns later when I deploy the app.
Say I add a boolean column 'lights,' I could give it a default value of 0, but not NULL. So now all my existing buildings will say that there are no 'lights' (because the value is 0), where in reality it should be something like 'don't know' or 'undefined' Like a third state.
Should I use ENUM columns with yes/no/undefined instead? What are best practices for this scenario?
What I would do, is create separate table, with object_id, and facility_id. Now, you can have dynamic facilites table, and connect them with object. Connection will only have what it needs, so not every object "light" or something else.
You can certainly create them them as nullable()! It is a common practice, IMO.
As far as best practices go, it depends on how your application should be used. Do you want a user to notice that the state has not been selected one way or the other yet? Maybe you are displaying a prompt to configure the ones that are null. On the other hand, it may be safer to assume that the options should default to false in order to be rendered properly. If this is the case, maybe a notification can go out to your users to update this property.
This worked to me
$table->boolean('lights')->nullable();
Yes Yo are Right this could be a problem some times
But the Boolean CAN BE USED SUCH AS TRUE (OR) FALSE ie) 0 (OR) 1
where in reality it should be something like 'don't know' or 'undefined' Like a third state.
So in this Situation use Can use Enum
For Example Your table will have ups_availablity
Scenario One :
If you want to add NotAvailable by default just pass the value inside default method
$table->enum('ups_availablity', ['Available', 'NotAvailable'])->default('NotAvailable');
Scenario Two:
If you want to add Null add nullable method
$table->enum('ups_availablity', ['Available', 'NotAvailable'])->nullable();
If You have any clarification Kindly Comment Below
Hope its helps
Gah.. I have spent way to long on this, but I believe I have found the problem.
Essentially I have a hidden field which is populated when a user clicks on an image.
It is required that the user has clicked the image but I do not want the generic form error message for a 'required' check with the CI form validation class.
As such I quickly made a image_required function in my extended form validation class, and set a rule such that this rule was applied to the hidden field.
function image_required($str)
{
$CI =& get_instance();
$CI->form_validation->set_message('image_required','Please click the image above.');
if($str != '')
{
return TRUE;
}
else
{
return FALSE;
}
}
If the hidden field was blank no error was being called.
I am led to believe now that this is because CI says this field is empty yet it is not 'required', therefore we will ignore all the other validation rules for the field. Is this correct?
If so how can i go about requiring this field be set but having a custom error message?
bangs head
Thanks
If you look at the source code (v2.1.3) for the '_execute' routine (system/libraries/Form_validation.php) you will see on line 486
// If the field is blank, but NOT required, no further tests are necessary
So you are correct, it needs to be required and then it will process your rule.
In order to fix it so you can have a non-required blank field that still processes rules, you should override the '_execute' method by creating a file called 'MY_Form_validation.php' in the application/libraries folder (I think, you might need to check exactly how you extend an existing library) and then copy the '_execute' method and alter the code to continue on a non-required but blank entry.
I do love CI, but I have to say this does not allow the flexibility required. It is perfectly reasonable to have a field that cannot be empty, but is NOT required. As in, you wouldn't enforce "user MUST enter a value", but they cannot submit a blank. I think someone got confused between EMPTY and REQUIRED.
1) REQUIRED: User MUST put a value in the field and it cannot be empty (i.e. '')
2) EMPTY: User does not HAVE to enter a value, BUT, if they do, it's cannot be empty. This not the same as REQUIRED... Looks like I'll be using a callback again.
REQUIRED incorporates two logical steps (1->Must enter a value, and 2->Cannot be empty) these two steps should be separated logically to allow either / or.
In constraint terms it would be either, REQUIRED, NOT NULL. Or NOT REQUIRED, NOT NULL.
I have some checkboxes and their name maps to a column in my activerecord model. Problem is, when the checboxes are selected, they appear in the params array in Sinatra, which works fine. But when they are deselected, the params never contains :checkbox => false. It only contains :checkbox => true. So the user can go from deselected to selected, but never vice versa as the params hash for :checkbox => false is never passed.
I feel like I am missing something fundamental here. Please help.
Thanks
A control in a HTML form only gets submitted to the server if it is “successful’. For checkboxes this means that it is checked. An unchecked checkbox doesn’t get submitted. This means that in Sinatra the value of params[:the_checkbox] is either the value of the checkbox specified in the HTML (if you don’t specify a value this will be the default which is the string 'on') if it is checked, or will be nil since nothing will have been submitted.
The obvious solution is to explicitly check for nil, and then assume that the checkbox is unchecked in that case:
checkbox_value = params[:the_checkbox].nil? ? false : true
Another option is to make use of the fact that the name/value pairs of the form data are sent to the server in the order that they appear in the document, and that when Sinatra sees a repeated name when parsing the data it will override the earlier value with the later one. This means that you can do something like this in the HTML:
<input type='hidden' name='the_checkbox' value='false' />
<input type='checkbox' name='the_checkbox' value='true' />
Now if the checkbox isn’t checked it won’t get submitted, but the hidden input will, and if it is checked it will get submitted, but will appear after the hidden input with the same name. The result is that params[:the_checkbox] will be the string 'true' if it has been checked, and the string 'false' if it hasn’t. (Note that you may still have to convert the strings to booleans, depending on what you’re doing with the submitted data).
You could do something like this :
post '/your-route' do
params[:checkbox] ||= false
# now do whatever you want with params[:checkbox]
end
What this does is assign false to params[:checkbox] unless it is already defined and set to true.