FluentValidation properties conditions - asp.net-mvc

I have two properties that must be validated, but once at a time. If one is null then validate the other, so I was trying like this :
RuleFor(a => a.CNPJ).Must(a => CNPHelper.CheckCNPJ(a)).When(a => !string.IsNullOrEmpty(a.CPF));
RuleFor(a => a.CPF).Must(a => CNPHelper.CheckCPF(a)).When(a => !string.IsNullOrEmpty(a.CNPJ));
But the must is mandatory, how to change it ?

Can you please try DependentRules extension.
As from docs (https://fluentvalidation.net/start#conditions) reference, you can achieve it as
RuleFor(x => x.Surname).NotNull().DependentRules(() => {
RuleFor(x => x.Forename).NotNull();
});

Related

Why wash_out can't use identical data types in different soap actions?

I use wash_out from the master branch.
Why I can't use identical data types in different soap actions?
Sample:
soap_action "get_groups",
:args => {:page => :integer},
:return => {:data => [{:id => :integer, :name => :string}], :total => :integer}
soap_action "get_items",
:args => {:page => :integer},
:return => {:data => [{:id => :integer, :name => :string}], :total => :integer}
Also I tried wrap it in WashOut::Type but it not help.
Error:
ActionView::Template::Error (Duplicate use of `data` type name. Consider using classified types.)
I found a solution for myself.
WashOut can't work with nested objects.
Every hash must be replaced with WashOut::Type.
It should look like this:
{:data => [SomeType], :total => :integer}

Elastica multi_field setup

I'm trying to use multi_field syntax of elasticsearch in combination with Elastica. I create an index and an mapping which looks like this:
$mapping->setProperties(array(
'id' => array('type' => 'string', 'include_in_all' => true),
'title' => array('type' => 'string', 'include_in_all' => true),
'publisher' => array('type' => 'multi_field', 'include_in_all' => TRUE, 'fields' =>
array('publisherName' => array('type' => 'string', 'index' => 'analyzed'),
'untouched' => array('type' => 'string', 'index' => 'not_analyzed')
)
));
So far, so good. I can run queries against the title field.
But when I try to query the field "publisher" in http://example.com:9200/_plugin/head/ I'm not able to select the field publisher or to create a structured query. I looks, that the field publisher is not in the index.
But I can build facets on publisher.untouched which works very well. Whats wrong in my mapping? I need to search for the publisher.
See the docs on multi_field mapping. Looks like you need to set a default field by changing 'publisherName' to just 'publisher'.

Unknown record property / related component with multi refs to one table

in model definition
$this->hasMany('Photo', array(
'refClass' => 'StyleLangHasPhoto',
'local' => 'style_lang_id',
'foreign' => 'photo_id'));
$this->hasMany('Photo as Preview', array(
'refClass' => 'StyleLangHasPreviewPhoto',
'local' => 'style_lang_id',
'foreign' => 'photo_id'));
but getter getPreview and even $this->_get('Preview') throws Unknown record property / related component "Preview" on "StyleLang"
i've try to make like this
$this->hasMany('Photo as Slide', array(
'refClass' => 'StyleLangHasPhoto',
'local' => 'style_lang_id',
'foreign' => 'photo_id'));
$this->hasMany('Photo as Preview', array(
'refClass' => 'StyleLangHasPreviewPhoto',
'local' => 'style_lang_id',
'foreign' => 'photo_id'));
but nothing changes
what i'm doing wrong?
UDP1: getPhoto, setPhoto, setPreview works good
problem was solved
foreignAlias: in relations for Photo and Preview must be different
thanx

Symfony 1.4 can't get submitted files

must be some thing simply resolving, but as a beginner, i've already spend 2 days trying to find out. I'm having a simple form ("enctype="multipart/form-data") with few inputs of text and file. I can't get what's wrong, i can get user's text data through getParameter(), but cannot get file getFiles(). i'm on ubuntu 11.04 php5.3
code:
form class:
public function configure()
{
$this->setWidgets(array(
'vendor' => new sfWidgetFormInputText(array(), array('class' => 'form-text')),
'image' => new sfWidgetFormInputFile(array(), array('class' => 'form-text')),
'city' => new sfWidgetFormInputText(),
'email' => new sfWidgetFormInputText(array('default' => 'simple#sample.org')),
'contacts' => new sfWidgetFormTextarea(),
'description' => new sfWidgetFormTextarea(),
));
$this->setValidators(array(
'vendor' => new sfValidatorString(array('required' => false)),
'image' => new sfValidatorFile(array(
'required' => false,
'path' => sfConfig::get('sf_upload_dir') . '/images',
'mime_types' => 'web_images')),
'city' => new sfValidatorString(array('required' => false)),
'email' => new sfValidatorString(array('required' => false)),
'contacts' => new sfValidatorString(array('required' => false)),
'description' => new sfValidatorString(array('required' => false)),
));
$this->widgetSchema->setNameFormat('pv[%s]');
}
action:
public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST));
return $this->renderText(var_dump($request->getParameter('pv'))); //this array include vars from form
//return $this->renderText(var_dump($request->getFiles('pv'))); //empty array
}
You should provide the actual code you are trying with. You could also look further into the forms component, which has a section on file uploads with example code here (bottom of page):
http://www.symfony-project.org/forms/1_2/en/02-Form-Validation
This part of the docs is still relevant for symfony 1.4.
Does your form widget configuration look anything like the following?
$this->setWidget('attachment', new sfWidgetFormInputFileEditable(array(
'file_src' => '/upload/' . $this->getObject()->getAttachment(),
'edit_mode' => true,
'with_delete' => true,
)));
$this->setValidator('attachment', new sfValidatorFile(array(
'path' => sfConfig::get('sf_upload_dir'),
'required' => false
)));
$this->setValidator('attachment_delete', new sfValidatorPass());
Also, check if you have permissions on your upload folder:
chmod 777 web/upload/
Are you binding the files as well as the form parameters, like so?
$this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
you must add the enctype="multipart/..." attribute to your form

Rails 3 - Custom JSON with Conditions

I'm trying to DRY up my code and I have two methods that do almost the same thing but add an extra fields for one instance. It is formatting JSON in Rails and trying to see if there is a way where I can perform an if statement within the result.collect similar to below. Any help is much appreciated
results.collect{ |result| { :type => defined?(result.type) ? result.type : "Total",
:jan => result.jan, :feb => result.feb, :mar => result.mar, :apr => result.apr,
:may => result.may, :jun => result.jun, :jul => result.jul, :aug => result.aug,
:sep => result.sep, :oct => result.oct, :nov => result.nov, :dec => result.dec } }
in this statement
:type => defined?(result.type) ? result.type : "Total"
I want to be able perform a check if result.type exists in the query then list it else just put "Total"
Any ideas how to perform this? Thanks everyone
I ended up writing in the method to check if with_type or not. If true then I added the type select statement in and if false then added
Select 'Total' as Type
I was hoping to figure out how to modify the json as in the question but modifying the composed_scope worked too.

Resources