I am trying to implement a "Save as Draft" functioning in my project using Yii. I have a form with 2 buttons :- Submit and Save . On clicking the Submit button , after validating all the fields including required fields the form data is saved into the database. It works perfect. On clicking Save button I need to save the form data without default validations into the database. How can I implement this disabling of validation in a controller action ??
All advices are acceptable..
Thanks
Turning off validation rules all together is easy:
$model->save(false);
This will not do any validation and will just try and save your model (may still fail on the database side).
But if you want to run some validation ,you might want to look into Scenarios here. They allow you to specify a different set of rules depending on which scenario you initialize the model with. That way you can only turn on/off whole sets of validation rules.
$model = new Thingy();
$model->save(); // All default validation rules
$model = new Thingy('draft');
$model->save(); //Applies all default & "draft" validation rules
Your question says in Controller but as far as I know, in controller we have filters which do for example check the permissions. That can be overridden as explained in this section of the guide. If you meant the validation that is done in model, then you can use scenarios (bypass validation by binding rules to scenario and no validation in other scenarios).
Check this thread that discuss a like problem
If I have misunderstood your question please comment here so that I update the answer accordingly!
Related
I am using formvalidation.io for our form validation.
Works great if we want to validate the form but we have functionality where we want to allow a user to save a draft of the form. In this scenario we don't want to do validate any part of the form. We don't want to disable each validator we want to disable the entire validation process. I tried using formnovalidate and it worked in Chrome but would not work in IE. What is the best method to do this for all browsers? Any suggestions?
I'm not sure if it's still relevant to you, but in case someone else stumbles across it (like I did with a slightly different problem);
We have included the 'novalidate' attribute in the formvalidation initial setup, eg:
$('form').each(function() {
if (!$(this).is('[novalidate]')) {
$(this).formValidation({
//form validation config
});
}
});
I am trying to expand on the Aurelia Contact Manager Tutorial. Specifically: adding email validation to the contact-details.html view. I have followed the examples in the Validation: Basics documentation and on first pass it worked as expected: Launch application, select a contact from the contact-list module, then update the email to something invalid by removing the '#', then tab away. The validation rule fires and the error message is displayed.
However, if after launching the application I select a first contact followed by a second, hence triggering a second activation of the contact-details module, then the validation rule does not fire.
I have tried a validationController.reset() on activate of the contact-detail and while this will remove any 'old' error messages, the on blur validation will still not fire.
I have tried the two different methods of creating the validation controller (using NewInstance.of(ValidationController) vs ValidationControllerFactory) but both yield the same result.
If, after navigating to a second contact and 'breaking' the validation, I then refresh the browser and reload the page then the validation works again. Until I choose another contact from the list which will then break it again.
I am new to Aurelia and JavaScript frameworks in general and I'm not sure if this is a bug or there is something extra required to handle re-routing to the same page.
That's a good question. There are a couple of things that may be catching you out. I've created a Gist which includes the necessary file modifications to get this working:
https://gist.github.com/freshcutdevelopment/170c2386f243e7095e276811dab52299
Gotchas
Because the view-model you're using for validation is not the backing view-model for the contact-detail.html view file you'll need a separate class which you'll apply the validation rules to. Although it sounds like you've already nailed this part, I'll include it for completeness. You can create this class like so:
export class Contact {
email= '';
}
You can then apply the validation rules to this class as follows:
ValidationRules
.ensure(a => a.email).required().email()
.on(Contact);
The last possible missing puzzle piece here is that you'll need to hook into the screen activation life-cycle hook deactivate() and reset the validation context. This will force the BootstrapValidationRenderer to remove the validation styles from your view.
deactivate(){
this.controller.reset();
}
Validation Workflow
The steps are as follows:
Inject the controller
Add the validation renderer to the controller
Create the validation model (only needed if the model you want to validate is not the view-model that backs your view)
Apply the validation rules to the model
Determine when to re-set and execute the validation (in this case on the deactivate life-cycle hook.
Apply the validation binding behavior to the view
Is there any way or how to start off to make a default log in form on drupal 7 applicable for normal users only? Administrators cannot log in in that log in form (I'll make a custom log in form for administrators, but that wasn't the issue here)
It was like,
http://mysite.com/user/
You see a log in form, ONLY normal users can log-in in that form.
Is there any way to do this any hints or clues where to start off first?
I've been here
https://drupal.org/node/1894620
Studied this
https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_access/7
It doesn't seem right, or I just don't manage to applied it well.
I'm lost. Any help would be appreciated though.
I think the only solution is to copy the whole User-Login form (within its all submit- and validation-functions) in your own custom module.
You can start by looking up the form-array from the login-form.....
function mymodule_form_alter(&$form, $form_state, $form_id)
{
if ($form_id == 'user_login')
{
// The login form here...
print_r($form);
}
}
.... and copying this into a new hook_form(), implemented by your module.
Now you can alter the submit and validation of the form in the copy.
In the new validation function you can check for the correct User-Role.
I have a yii form with a file field and a normal text field for supplying an external image url. I managed to get a normal server validation working which checks that only one of the two form fields is filled out (because you can either provide a local file for uploading OR an external picture URL). Important to notice is, that the attribute value of the "file field" seems only to be available after the $_POST var is set, meaning that a normal custom validation rule in the Model Class won't work since the $_POST value of the file field is only available after submission (in the $_FILES var).
But if you look at my provided picture below, I want also a ClientSide Validaton, so that the user gets immediate feedback that it's not allowed to fill out both fields. But how do I accomplish that? I'm sitting on this problem for about 2 days, half of the time on searching for solutions...I am new to Yii.
http://www.prism-informatics.com/images/demo.png
Best wishes,
S
I am not completely sure what your problems is as you didnt provide any code but i assume you forgot :
public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
I'm using a CMultiFileUpload control in one of my forms like this:
$this->widget('CMultiFileUpload', array(
'name' => 'neueAnhaenge',
));
When input validation for some other form element fails and the input form is rendered again, a previous selection in this control is gone (as expected).
How do I repopulate this control, what do I have to do in my controller, is there a way to prepopulate this?
Thanks in advance.
For file fields it is rather impossible to reset the values assigned to it after it has been sent to the server.
One way to solve this would be to get the uploaded files, store them temporarily on the server and modify the form so it sends a reference to the file on the server.
A much better way would be to use Ajax or Client side validation of form fields to ensure no validation error occures when form has been sent. You can enable these options for CActiveForm: $enableClientValidation and $enableAjaxValidation.